blob: ae303106ad3dd53ab6dcbf2e1cc5213ffc86c9e4 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
13 * - Put it in the options array below (copy an existing entry).
14 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
16 * - Add a PV_XX entry to the enum below.
17 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
19 * - For a buffer option, add some code to buf_copy_options().
20 * - For a buffer string option, add code to check_buf_options().
21 * - If it's a numeric option, add any necessary bounds checks to do_set().
22 * - If it's a list of flags, add some code in do_set(), search for WW_ALL.
23 * - When adding an option with expansion (P_EXPAND), but with a different
24 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020025 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 * options.txt, and any other related places.
27 * - Add an entry in runtime/optwin.vim.
28 * When making changes:
29 * - Adjust the help for the option in doc/option.txt.
30 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
31 * comment at the help for the 'compatible' option.
32 */
33
34#define IN_OPTION_C
35#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020036#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010039static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020040static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010041static char_u *option_expand(int opt_idx, char_u *val);
42static void didset_options(void);
43static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000044#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000046#else
47# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
48#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010049static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
50static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020051static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020053static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010054static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010055static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
57static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020058static int istermoption(struct vimoption *p);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static char_u *get_varp_scope(struct vimoption *p, int opt_flags);
60static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020061static void check_win_options(win_T *win);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static void option_value2string(struct vimoption *, int opt_flags);
63static void check_winopt(winopt_T *wop);
64static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010065static void paste_option_changed(void);
66static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/*
69 * Initialize the options, first part.
70 *
71 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010072 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000073 */
74 void
Bram Moolenaar07268702018-03-01 21:57:32 +010075set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
77 char_u *p;
78 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000079 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81#ifdef FEAT_LANGMAP
82 langmap_init();
83#endif
84
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010085 // Be Vi compatible by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 p_cp = TRUE;
87
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010088 // Use POSIX compatibility when $VIM_POSIX is set.
Bram Moolenaar4399ef42005-02-12 14:29:27 +000089 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +000090 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +000091 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020092 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000093 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000094
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 /*
96 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +000097 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 */
Bram Moolenaar7c626922005-02-07 22:01:03 +000099 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100100#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100102 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000104 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200105#if defined(MSWIN)
106 {
107 // For MS-Windows put the path in quotes instead of escaping spaces.
108 char_u *cmd;
109 size_t len;
110
111 if (vim_strchr(p, ' ') != NULL)
112 {
113 len = STRLEN(p) + 3; // two quotes and a trailing NUL
114 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200115 if (cmd != NULL)
116 {
117 vim_snprintf((char *)cmd, len, "\"%s\"", p);
118 set_string_default("sh", cmd);
119 vim_free(cmd);
120 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200121 }
122 else
123 set_string_default("sh", p);
124 }
125#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100126 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_WILDIGN
130 /*
131 * Set the default for 'backupskip' to include environment variables for
132 * temp files.
133 */
134 {
135# ifdef UNIX
136 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
137# else
138 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
139# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000140 int len;
141 garray_T ga;
142 int mustfree;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200143 char_u *item;
144
145 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
147 ga_init2(&ga, 1, 100);
K.Takataeeec2542021-06-02 13:28:16 +0200148 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000150 mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# ifdef UNIX
152 if (*names[n] == NUL)
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200153# ifdef MACOS_X
154 p = (char_u *)"/private/tmp";
155# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 p = (char_u *)"/tmp";
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 else
159# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000160 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (p != NULL && *p != NUL)
162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100163 // First time count the NUL, otherwise count the ','.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000164 len = (int)STRLEN(p) + 3;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200165 item = alloc(len);
166 STRCPY(item, p);
167 add_pathsep(item);
168 STRCAT(item, "*");
169 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
170 == NULL
171 && ga_grow(&ga, len) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 if (ga.ga_len > 0)
174 STRCAT(ga.ga_data, ",");
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200175 STRCAT(ga.ga_data, item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 ga.ga_len += len;
177 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200178 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 if (mustfree)
181 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 }
183 if (ga.ga_data != NULL)
184 {
185 set_string_default("bsk", ga.ga_data);
186 vim_free(ga.ga_data);
187 }
188 }
189#endif
190
191 /*
192 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
193 */
194 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000195 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000197#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
198 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
199#endif
200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifdef HAVE_AVAIL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100202 // Use amount of memory available at this moment.
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200203 n = (mch_avail_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#else
205# ifdef HAVE_TOTAL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100206 // Use amount of memory available to Vim.
Bram Moolenaar914572a2007-05-01 11:37:47 +0000207 n = (mch_total_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208# else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000209 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210# endif
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000213 opt_idx = findoption((char_u *)"maxmem");
214 if (opt_idx >= 0)
215 {
216#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100217 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
218 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000219#endif
220 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
221 }
222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 }
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#ifdef FEAT_SEARCHPATH
226 {
227 char_u *cdpath;
228 char_u *buf;
229 int i;
230 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000231 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100233 // Initialize the 'cdpath' option's default value.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000234 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 if (cdpath != NULL)
236 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200237 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 if (buf != NULL)
239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100240 buf[0] = ','; // start with ",", current dir first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 j = 1;
242 for (i = 0; cdpath[i] != NUL; ++i)
243 {
244 if (vim_ispathlistsep(cdpath[i]))
245 buf[j++] = ',';
246 else
247 {
248 if (cdpath[i] == ' ' || cdpath[i] == ',')
249 buf[j++] = '\\';
250 buf[j++] = cdpath[i];
251 }
252 }
253 buf[j] = NUL;
254 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000255 if (opt_idx >= 0)
256 {
257 options[opt_idx].def_val[VI_DEFAULT] = buf;
258 options[opt_idx].flags |= P_DEF_ALLOCED;
259 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200260 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100261 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000263 if (mustfree)
264 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 }
267#endif
268
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100269#if defined(FEAT_POSTSCRIPT) && (defined(MSWIN) || defined(VMS) || defined(EBCDIC) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100270 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100272# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 (char_u *)"cp1252"
274# else
275# ifdef VMS
276 (char_u *)"dec-mcs"
277# else
278# ifdef EBCDIC
279 (char_u *)"ebcdic-uk"
280# else
281# ifdef MAC
282 (char_u *)"mac-roman"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100283# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 (char_u *)"hp-roman8"
285# endif
286# endif
287# endif
288# endif
289 );
290#endif
291
292#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100293 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100295# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000296 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297# else
298# ifdef VMS
299 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
300
301# else
302 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
303# endif
304# endif
305 );
306#endif
307
308 /*
309 * Set all the options (except the terminal options) to their default
310 * value. Also set the global value for local options.
311 */
312 set_options_default(0);
313
Bram Moolenaar07268702018-03-01 21:57:32 +0100314#ifdef CLEAN_RUNTIMEPATH
315 if (clean_arg)
316 {
317 opt_idx = findoption((char_u *)"runtimepath");
318 if (opt_idx >= 0)
319 {
320 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
321 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
322 }
323 opt_idx = findoption((char_u *)"packpath");
324 if (opt_idx >= 0)
325 {
326 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
327 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
328 }
329 }
330#endif
331
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#ifdef FEAT_GUI
333 if (found_reverse_arg)
334 set_option_value((char_u *)"bg", 0L, (char_u *)"dark", 0);
335#endif
336
337 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100338 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100339 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 check_buf_options(curbuf);
341 check_win_options(curwin);
342 check_options();
343
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100344 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 didset_options();
346
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000347#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100348 // Use the current chartab for the generic chartab. This is not in
349 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000350 init_spell_chartab();
351#endif
352
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /*
354 * Expand environment variables and things like "~" for the defaults.
355 * If option_expand() returns non-NULL the variable is expanded. This can
356 * only happen for non-indirect options.
357 * Also set the default to the expanded value, so ":set" does not list
358 * them.
359 * Don't set the P_ALLOCED flag, because we don't want to free the
360 * default.
361 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200362 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
364 if ((options[opt_idx].flags & P_GETTEXT)
365 && options[opt_idx].var != NULL)
366 p = (char_u *)_(*(char **)options[opt_idx].var);
367 else
368 p = option_expand(opt_idx, NULL);
369 if (p != NULL && (p = vim_strsave(p)) != NULL)
370 {
371 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100372 // VIMEXP
373 // Defaults for all expanded options are currently the same for Vi
374 // and Vim. When this changes, add some code here! Also need to
375 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (options[opt_idx].flags & P_DEF_ALLOCED)
377 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
378 options[opt_idx].def_val[VI_DEFAULT] = p;
379 options[opt_idx].flags |= P_DEF_ALLOCED;
380 }
381 }
382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386 // Detect use of mlterm.
387 // Mlterm is a terminal emulator akin to xterm that has some special
388 // abilities (bidi namely).
389 // NOTE: mlterm's author is being asked to 'set' a variable
390 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 if (mch_getenv((char_u *)"MLTERM") != NULL)
392 set_option_value((char_u *)"tbidi", 1L, NULL, 0);
393#endif
394
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200395 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Bram Moolenaar4f974752019-02-17 17:44:42 +0100397# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 /*
399 * If $LANG isn't set, try to get a good value for it. This makes the
400 * right language be used automatically. Don't do this for English.
401 */
402 if (mch_getenv((char_u *)"LANG") == NULL)
403 {
404 char buf[20];
405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100406 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
407 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
408 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
410 (LPTSTR)buf, 20);
411 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
415 STRCPY(buf, "zh_TW");
416 else if (STRNICMP(buf, "chs", 3) == 0
417 || STRNICMP(buf, "zhc", 3) == 0)
418 STRCPY(buf, "zh_CN");
419 else if (STRNICMP(buf, "jp", 2) == 0)
420 STRCPY(buf, "ja");
421 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100423 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000426# else
Bram Moolenaar9d47f172006-03-15 23:03:01 +0000427# ifdef MACOS_CONVERT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100428 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000429 mac_lang_init();
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431# endif
432
K.Takataf883d902021-05-30 18:04:19 +0200433# ifdef MSWIN
434 // MS-Windows has builtin support for conversion to and from Unicode, using
435 // "utf-8" for 'encoding' should work best for most users.
436 p = vim_strsave((char_u *)ENC_DFLT);
437# else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100438 // enc_locale() will try to find the encoding of the current locale.
K.Takataf883d902021-05-30 18:04:19 +0200439 // This works best for properly configured systems, old and new.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 p = enc_locale();
K.Takataf883d902021-05-30 18:04:19 +0200441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 if (p != NULL)
443 {
444 char_u *save_enc;
445
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100446 // Try setting 'encoding' and check if the value is valid.
K.Takataf883d902021-05-30 18:04:19 +0200447 // If not, go back to the default encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 save_enc = p_enc;
449 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000450 if (STRCMP(p_enc, "gb18030") == 0)
451 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100452 // We don't support "gb18030", but "cp936" is a good substitute
453 // for practical purposes, thus use that. It's not an alias to
454 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000455 p_enc = vim_strsave((char_u *)"cp936");
456 vim_free(p);
457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 if (mb_init() == NULL)
459 {
460 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000461 if (opt_idx >= 0)
462 {
463 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
464 options[opt_idx].flags |= P_DEF_ALLOCED;
465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466
Bram Moolenaard0573012017-10-28 21:11:06 +0200467#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100468 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000469 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100470 // Adjust the default for 'isprint' and 'iskeyword' to match
471 // latin1. Also set the defaults for when 'nocompatible' is
472 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000473 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000474 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000475 set_string_option_direct((char_u *)"isk", -1,
476 ISK_LATIN1, OPT_FREE, SID_NONE);
477 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000478 if (opt_idx >= 0)
479 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000480 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000481 if (opt_idx >= 0)
482 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000483 (void)init_chartab();
484 }
485#endif
486
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200487#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100488 // Win32 console: When GetACP() returns a different value from
489 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200490 if (
491# ifdef VIMDLL
492 (!gui.in_use && !gui.starting) &&
493# endif
494 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 {
496 char buf[50];
497
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100498 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
499 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100500 if (GetConsoleCP() == 0)
501 sprintf(buf, "cp%ld", (long)GetACP());
502 else
503 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 p_tenc = vim_strsave((char_u *)buf);
505 if (p_tenc != NULL)
506 {
507 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000508 if (opt_idx >= 0)
509 {
510 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
511 options[opt_idx].flags |= P_DEF_ALLOCED;
512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 convert_setup(&input_conv, p_tenc, p_enc);
514 convert_setup(&output_conv, p_enc, p_tenc);
515 }
516 else
517 p_tenc = empty_option;
518 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100519#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100520#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100521 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000522 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 }
525 else
526 {
527 vim_free(p_enc);
528 p_enc = save_enc;
529 }
530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100533 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 set_helplang_default(get_mess_lang());
535#endif
536}
537
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200538static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
539
540/*
541 * Set the "fileencodings" option to the default value for when 'encoding' is
542 * utf-8.
543 */
544 void
545set_fencs_unicode()
546{
547 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
548 OPT_FREE, 0);
549}
550
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551/*
552 * Set an option to its default value.
553 * This does not take care of side effects!
554 */
555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100556set_option_default(
557 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100558 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
559 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100561 char_u *varp; // pointer to variable for current option
562 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000564 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
566
567 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
568 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100569 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
571 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
572 if (flags & P_STRING)
573 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200574 // 'fencs' default value depends on 'encoding'
575 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
576 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100577 // Use set_string_option_direct() for local options to handle
578 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200579 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200580 set_string_option_direct(NULL, opt_idx,
581 options[opt_idx].def_val[dvi], opt_flags, 0);
582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200584 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
585 free_string_option(*(char_u **)(varp));
586 *(char_u **)varp = options[opt_idx].def_val[dvi];
587 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 }
589 }
590 else if (flags & P_NUM)
591 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000592 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 win_comp_scroll(curwin);
594 else
595 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100596 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
597
598 if ((long *)varp == &curwin->w_p_so
599 || (long *)varp == &curwin->w_p_siso)
600 // 'scrolloff' and 'sidescrolloff' local values have a
601 // different default value than the global default.
602 *(long *)varp = -1;
603 else
604 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (both)
607 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100608 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100613 // the cast to long is required for Manx C, long_i is needed for
614 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000615 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000616#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100617 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000618 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
619 *(int *)varp = FALSE;
620#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100621 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 if (both)
623 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
624 *(int *)varp;
625 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000626
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100627 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000628 flagsp = insecure_flag(opt_idx, opt_flags);
629 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 }
631
632#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200633 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634#endif
635}
636
637/*
638 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200639 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 */
641 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100642set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100643 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644{
645 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000647 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
Bram Moolenaardac13472019-09-16 21:06:21 +0200649 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200650 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200651 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100652 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200653# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200654 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200655 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200656# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100657 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 set_option_default(i, opt_flags, p_cp);
659
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100660 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000661 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200663#ifdef FEAT_CINDENT
664 parse_cino(curbuf);
665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666}
667
668/*
669 * Set the Vi-default value of a string option.
670 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100671 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100673 static void
674set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 char_u *p;
677 int opt_idx;
678
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100679 if (escape && vim_strchr(val, ' ') != NULL)
680 p = vim_strsave_escaped(val, (char_u *)" ");
681 else
682 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100683 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
685 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000686 if (opt_idx >= 0)
687 {
688 if (options[opt_idx].flags & P_DEF_ALLOCED)
689 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
690 options[opt_idx].def_val[VI_DEFAULT] = p;
691 options[opt_idx].flags |= P_DEF_ALLOCED;
692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
694}
695
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100696 void
697set_string_default(char *name, char_u *val)
698{
699 set_string_default_esc(name, val, FALSE);
700}
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200703 * For an option value that contains comma separated items, find "newval" in
704 * "origval". Return NULL if not found.
705 */
706 static char_u *
707find_dup_item(char_u *origval, char_u *newval, long_u flags)
708{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200709 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200710 size_t newlen;
711 char_u *s;
712
713 if (origval == NULL)
714 return NULL;
715
716 newlen = STRLEN(newval);
717 for (s = origval; *s != NUL; ++s)
718 {
719 if ((!(flags & P_COMMA)
720 || s == origval
721 || (s[-1] == ',' && !(bs & 1)))
722 && STRNCMP(s, newval, newlen) == 0
723 && (!(flags & P_COMMA)
724 || s[newlen] == ','
725 || s[newlen] == NUL))
726 return s;
727 // Count backslashes. Only a comma with an even number of backslashes
728 // or a single backslash preceded by a comma before it is recognized as
729 // a separator.
730 if ((s > origval + 1
731 && s[-1] == '\\'
732 && s[-2] != ',')
733 || (s == origval + 1
734 && s[-1] == '\\'))
735 ++bs;
736 else
737 bs = 0;
738 }
739 return NULL;
740}
741
742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 * Set the Vi-default value of a number option.
744 * Used for 'lines' and 'columns'.
745 */
746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100747set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000749 int opt_idx;
750
751 opt_idx = findoption((char_u *)name);
752 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000753 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754}
755
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200756/*
757 * Set all window-local and buffer-local options to the Vim default.
758 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200759 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200760 */
761 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200762set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200763{
764 win_T *save_curwin = curwin;
765 int i;
766
767 curwin = wp;
768 curbuf = curwin->w_buffer;
769 block_autocmds();
770
Bram Moolenaardac13472019-09-16 21:06:21 +0200771 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200772 {
773 struct vimoption *p = &(options[i]);
774 char_u *varp = get_varp_scope(p, OPT_LOCAL);
775
776 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200777 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200778 && !(options[i].flags & P_NODEFAULT)
779 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200780 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200781 }
782
783 unblock_autocmds();
784 curwin = save_curwin;
785 curbuf = curwin->w_buffer;
786}
787
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000788#if defined(EXITFREE) || defined(PROTO)
789/*
790 * Free all options.
791 */
792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100793free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000794{
795 int i;
796
Bram Moolenaardac13472019-09-16 21:06:21 +0200797 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000798 {
799 if (options[i].indir == PV_NONE)
800 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100801 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100802 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000803 free_string_option(*(char_u **)options[i].var);
804 if (options[i].flags & P_DEF_ALLOCED)
805 free_string_option(options[i].def_val[VI_DEFAULT]);
806 }
807 else if (options[i].var != VAR_WIN
808 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100809 // buffer-local option: free global value
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000810 free_string_option(*(char_u **)options[i].var);
811 }
812}
813#endif
814
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816/*
817 * Initialize the options, part two: After getting Rows and Columns and
818 * setting 'term'.
819 */
820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100821set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000823 int idx;
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100826 * 'scroll' defaults to half the window height. The stored default is zero,
827 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000829 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000830 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000831 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 comp_col();
833
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000834 /*
835 * 'window' is only for backwards compatibility with Vi.
836 * Default is Rows - 1.
837 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000838 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000839 p_window = Rows - 1;
840 set_number_default("window", Rows - 1);
841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100842 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100843#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000844 /*
845 * If 'background' wasn't set by the user, try guessing the value,
846 * depending on the terminal name. Only need to check for terminals
847 * with a dark background, that can handle color.
848 */
849 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000850 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
851 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000853 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 // don't mark it as set, when starting the GUI it may be
855 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000856 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000859
860#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000862#endif
863#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000865#endif
866#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100867 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869}
870
871/*
872 * Initialize the options, part three: After reading the .vimrc
873 */
874 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100875set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100877#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878/*
879 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
880 * This is done after other initializations, where 'shell' might have been
881 * set, but only if they have not been set before.
882 */
883 char_u *p;
884 int idx_srr;
885 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100886# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 int idx_sp;
888 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
891 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000892 if (idx_srr < 0)
893 do_srr = FALSE;
894 else
895 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100896# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000898 if (idx_sp < 0)
899 do_sp = FALSE;
900 else
901 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100902# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200903 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (p != NULL)
905 {
906 /*
907 * Default for p_sp is "| tee", for p_srr is ">".
908 * For known shells it is changed here to include stderr.
909 */
910 if ( fnamecmp(p, "csh") == 0
911 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100912# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 || fnamecmp(p, "csh.exe") == 0
914 || fnamecmp(p, "tcsh.exe") == 0
915# endif
916 )
917 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100918# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (do_sp)
920 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100921# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100923# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
927 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (do_srr)
930 {
931 p_srr = (char_u *)">&";
932 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
933 }
934 }
Mike Williams12795022021-06-28 20:53:58 +0200935# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200936 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
937 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200938 else if ( fnamecmp(p, "powershell") == 0
939 || fnamecmp(p, "powershell.exe") == 0
940 )
941 {
942# if defined(FEAT_QUICKFIX)
943 if (do_sp)
944 {
945 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
946 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
947 }
948# endif
949 if (do_srr)
950 {
951 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
952 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
953 }
954 }
955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 else
Natanael Copa56318362021-05-06 18:46:35 +0200957 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if ( fnamecmp(p, "sh") == 0
959 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200960 || fnamecmp(p, "mksh") == 0
961 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000963 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200965 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200966 || fnamecmp(p, "ash") == 0
967 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200968 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100969# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 || fnamecmp(p, "cmd") == 0
971 || fnamecmp(p, "sh.exe") == 0
972 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200973 || fnamecmp(p, "mksh.exe") == 0
974 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000976 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 || fnamecmp(p, "bash.exe") == 0
978 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200979 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200980 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100982 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100984# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (do_sp)
986 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100987# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100989# else
Mike Williamsa3d1b292021-06-30 20:56:00 +0200990 if (fnamecmp(p, "pwsh") == 0)
991 p_sp = (char_u *)">%s 2>&1";
992 else
993 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
996 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (do_srr)
999 {
1000 p_srr = (char_u *)">%s 2>&1";
1001 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1002 }
1003 }
1004 vim_free(p);
1005 }
1006#endif
1007
Bram Moolenaar4f974752019-02-17 17:44:42 +01001008#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001010 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1011 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001013 * set, but only if they have not been set before.
1014 * Default values depend on shell (cmd.exe is default shell):
1015 *
1016 * p_shcf p_sxq
1017 * cmd.exe - "/c" "("
1018 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001019 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001020 * "sh" like shells - "-c" "\""
1021 *
1022 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 */
Mike Williams12795022021-06-28 20:53:58 +02001024 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1025 {
1026 int idx_opt;
1027
1028 idx_opt = findoption((char_u *)"shcf");
1029 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1030 {
1031 p_shcf = (char_u*)"-Command";
1032 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1033 }
1034
1035 idx_opt = findoption((char_u *)"sxq");
1036 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1037 {
1038 p_sxq = (char_u*)"\"";
1039 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1040 }
1041 }
1042 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 int idx3;
1045
1046 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001047 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
1049 p_shcf = (char_u *)"-c";
1050 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1051 }
1052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001053 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001055 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
1057 p_sxq = (char_u *)"\"";
1058 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001061 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1062 {
1063 int idx3;
1064
1065 /*
1066 * cmd.exe on Windows will strip the first and last double quote given
1067 * on the command line, e.g. most of the time things like:
1068 * cmd /c "my path/to/echo" "my args to echo"
1069 * become:
1070 * my path/to/echo" "my args to echo
1071 * when executed.
1072 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001073 * To avoid this, set shellxquote to surround the command in
1074 * parenthesis. This appears to make most commands work, without
1075 * breaking commands that worked previously, such as
1076 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001077 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001078 idx3 = findoption((char_u *)"sxq");
1079 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1080 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001081 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001082 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1083 }
1084
Bram Moolenaara64ba222012-02-12 23:23:31 +01001085 idx3 = findoption((char_u *)"shcf");
1086 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1087 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001088 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001089 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1090 }
1091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#endif
1093
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001094 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001095 {
1096 int idx_ffs = findoption((char_u *)"ffs");
1097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001098 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001099 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1100 set_fileformat(default_fileformat(), OPT_LOCAL);
1101 }
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103#ifdef FEAT_TITLE
1104 set_title_defaults();
1105#endif
1106}
1107
1108#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1109/*
1110 * When 'helplang' is still at its default value, set it to "lang".
1111 * Only the first two characters of "lang" are used.
1112 */
1113 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001114set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
1116 int idx;
1117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001118 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 return;
1120 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001121 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
1123 if (options[idx].flags & P_ALLOCED)
1124 free_string_option(p_hlg);
1125 p_hlg = vim_strsave(lang);
1126 if (p_hlg == NULL)
1127 p_hlg = empty_option;
1128 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001129 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001130 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001131 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1132 {
1133 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1134 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1135 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001136 // any C like setting, such as C.UTF-8, becomes "en"
1137 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1138 {
1139 p_hlg[0] = 'e';
1140 p_hlg[1] = 'n';
1141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 options[idx].flags |= P_ALLOCED;
1145 }
1146}
1147#endif
1148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149#ifdef FEAT_TITLE
1150/*
1151 * 'title' and 'icon' only default to true if they have not been set or reset
1152 * in .vimrc and we can read the old value.
1153 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1154 * they can be reset. This reduces startup time when using X on a remote
1155 * machine.
1156 */
1157 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001158set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159{
1160 int idx1;
1161 long val;
1162
1163 /*
1164 * If GUI is (going to be) used, we can always set the window title and
1165 * icon name. Saves a bit of time, because the X11 display server does
1166 * not need to be contacted.
1167 */
1168 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001169 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 {
1171#ifdef FEAT_GUI
1172 if (gui.starting || gui.in_use)
1173 val = TRUE;
1174 else
1175#endif
1176 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001177 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 p_title = val;
1179 }
1180 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001181 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
1183#ifdef FEAT_GUI
1184 if (gui.starting || gui.in_use)
1185 val = TRUE;
1186 else
1187#endif
1188 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001189 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 p_icon = val;
1191 }
1192}
1193#endif
1194
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001195 void
1196ex_set(exarg_T *eap)
1197{
1198 int flags = 0;
1199
1200 if (eap->cmdidx == CMD_setlocal)
1201 flags = OPT_LOCAL;
1202 else if (eap->cmdidx == CMD_setglobal)
1203 flags = OPT_GLOBAL;
1204#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001205 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001206 ex_options(eap);
1207 else
1208#endif
1209 {
1210 if (eap->forceit)
1211 flags |= OPT_ONECOLUMN;
1212 (void)do_set(eap->arg, flags);
1213 }
1214}
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216/*
1217 * Parse 'arg' for option settings.
1218 *
1219 * 'arg' may be IObuff, but only when no errors can be present and option
1220 * does not need to be expanded with option_expand().
1221 * "opt_flags":
1222 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001223 * OPT_GLOBAL for ":setglobal"
1224 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001226 * OPT_WINONLY to only set window-local options
1227 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 *
1229 * returns FAIL if an error is detected, OK otherwise
1230 */
1231 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001232do_set(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001233 char_u *arg, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001234 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235{
1236 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001237 char *errmsg;
1238 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1241 int nextchar; // next non-white char after option name
1242 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 int len;
1244 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001245 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001247 long_u flags; // flags for current option
1248 char_u *varp = NULL; // pointer to variable for current option
1249 int did_show = FALSE; // already showed one value
1250 int adding; // "opt+=arg"
1251 int prepending; // "opt^=arg"
1252 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 int cp_val = 0;
1254 char_u key_name[2];
1255
1256 if (*arg == NUL)
1257 {
1258 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001259 did_show = TRUE;
1260 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001263 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
1265 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001266 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001268 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1269 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 {
1271 /*
1272 * ":set all" show all options.
1273 * ":set all&" set all options to their default value.
1274 */
1275 arg += 3;
1276 if (*arg == '&')
1277 {
1278 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001279 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001281 didset_options();
1282 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001283 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
1285 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001288 did_show = TRUE;
1289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001291 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {
1293 showoptions(2, opt_flags);
1294 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001295 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 arg += 7;
1297 }
1298 else
1299 {
1300 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001301 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
1303 prefix = 0;
1304 arg += 2;
1305 }
1306 else if (STRNCMP(arg, "inv", 3) == 0)
1307 {
1308 prefix = 2;
1309 arg += 3;
1310 }
1311
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001312 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 key = 0;
1314 if (*arg == '<')
1315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001317 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1319 len = 5;
1320 else
1321 {
1322 len = 1;
1323 while (arg[len] != NUL && arg[len] != '>')
1324 ++len;
1325 }
1326 if (arg[len] != '>')
1327 {
1328 errmsg = e_invarg;
1329 goto skip;
1330 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001331 arg[len] = NUL; // put NUL after name
1332 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001334 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001336 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338 else
1339 {
1340 len = 0;
1341 /*
1342 * The two characters after "t_" may not be alphanumeric.
1343 */
1344 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1345 len = 4;
1346 else
1347 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1348 ++len;
1349 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001350 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001352 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001354 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001357 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 afterchar = arg[len];
1359
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001360 if (!in_vim9script())
1361 // skip white space, allow ":set ai ?", ":set hlsearch !"
1362 while (VIM_ISWHITE(arg[len]))
1363 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364
1365 adding = FALSE;
1366 prepending = FALSE;
1367 removing = FALSE;
1368 if (arg[len] != NUL && arg[len + 1] == '=')
1369 {
1370 if (arg[len] == '+')
1371 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001372 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 ++len;
1374 }
1375 else if (arg[len] == '^')
1376 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001377 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 ++len;
1379 }
1380 else if (arg[len] == '-')
1381 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001382 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 ++len;
1384 }
1385 }
1386 nextchar = arg[len];
1387
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001388 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001390 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 goto skip;
1392 }
1393
1394 if (opt_idx >= 0)
1395 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001396 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001398 // Only give an error message when requesting the value of
1399 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1401 && (!(options[opt_idx].flags & P_BOOL)
1402 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001403 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 goto skip;
1405 }
1406
1407 flags = options[opt_idx].flags;
1408 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1409 }
1410 else
1411 {
1412 flags = P_STRING;
1413 if (key < 0)
1414 {
1415 key_name[0] = KEY2TERMCAP0(key);
1416 key_name[1] = KEY2TERMCAP1(key);
1417 }
1418 else
1419 {
1420 key_name[0] = KS_KEY;
1421 key_name[1] = (key & 0xff);
1422 }
1423 }
1424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001425 // Skip all options that are not window-local (used when showing
1426 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001427 if ((opt_flags & OPT_WINONLY)
1428 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1429 goto skip;
1430
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001431 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001432 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1433 && options[opt_idx].var == VAR_WIN)
1434 goto skip;
1435
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001436 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001437 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001439 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001440 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001441 errmsg = N_("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001442 goto skip;
1443 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001444 if ((flags & P_MLE) && !p_mle)
1445 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001446 errmsg = N_("E992: Not allowed in a modeline when 'modelineexpr' is off");
Bram Moolenaar110289e2019-05-23 15:38:06 +02001447 goto skip;
1448 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001449#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001450 // In diff mode some options are overruled. This avoids that
1451 // 'foldmethod' becomes "marker" instead of "diff" and that
1452 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001453 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001454 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001455 && (
1456#ifdef FEAT_FOLDING
1457 options[opt_idx].indir == PV_FDM ||
1458#endif
1459 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001460 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463
1464#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001465 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001466 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001468 errmsg = e_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 goto skip;
1470 }
1471#endif
1472
1473 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1474 {
1475 arg += len;
1476 cp_val = p_cp;
1477 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1478 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001479 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
1481 cp_val = FALSE;
1482 arg += 3;
1483 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001484 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 {
1486 cp_val = TRUE;
1487 arg += 2;
1488 }
1489 }
1490 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001491 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 {
1493 errmsg = e_trailing;
1494 goto skip;
1495 }
1496 }
1497
1498 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001499 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001500 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
1502 if (nextchar == '?'
1503 || (prefix == 1
1504 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1505 && !(flags & P_BOOL)))
1506 {
1507 /*
1508 * print value
1509 */
1510 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001511 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 else
1513 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001514 gotocmdline(TRUE); // cursor at status line
1515 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
1517 if (opt_idx >= 0)
1518 {
1519 showoneopt(&options[opt_idx], opt_flags);
1520#ifdef FEAT_EVAL
1521 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001522 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001523 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001524 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001525 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001526 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001527 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001528 (int)options[opt_idx].indir & PV_MASK]);
1529 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001530 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001531 (int)options[opt_idx].indir & PV_MASK]);
1532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533#endif
1534 }
1535 else
1536 {
1537 char_u *p;
1538
1539 p = find_termcode(key_name);
1540 if (p == NULL)
1541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001542 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 goto skip;
1544 }
1545 else
1546 (void)show_one_termcode(key_name, p, TRUE);
1547 }
1548 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001549 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 errmsg = e_trailing;
1551 }
1552 else
1553 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001554 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001555 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001556
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001557 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 {
1559 if (nextchar == '=' || nextchar == ':')
1560 {
1561 errmsg = e_invarg;
1562 goto skip;
1563 }
1564
1565 /*
1566 * ":set opt!": invert
1567 * ":set opt&": reset to default value
1568 * ":set opt<": reset to global value
1569 */
1570 if (nextchar == '!')
1571 value = *(int *)(varp) ^ 1;
1572 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001573 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 ((flags & P_VI_DEF) || cp_val)
1575 ? VI_DEFAULT : VIM_DEFAULT];
1576 else if (nextchar == '<')
1577 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001578 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 if ((int *)varp == &curbuf->b_p_ar
1580 && opt_flags == OPT_LOCAL)
1581 value = -1;
1582 else
1583 value = *(int *)get_varp_scope(&(options[opt_idx]),
1584 OPT_GLOBAL);
1585 }
1586 else
1587 {
1588 /*
1589 * ":set invopt": invert
1590 * ":set opt" or ":set noopt": set or reset
1591 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001592 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
1594 errmsg = e_trailing;
1595 goto skip;
1596 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001597 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 value = *(int *)(varp) ^ 1;
1599 else
1600 value = prefix;
1601 }
1602
1603 errmsg = set_bool_option(opt_idx, varp, (int)value,
1604 opt_flags);
1605 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001606 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
1608 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1609 || prefix != 1)
1610 {
1611 errmsg = e_invarg;
1612 goto skip;
1613 }
1614
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001615 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
1617 /*
1618 * Different ways to set a number option:
1619 * & set to default value
1620 * < set to global value
1621 * <xx> accept special key codes for 'wildchar'
1622 * c accept any non-digit for 'wildchar'
1623 * [-]0-9 set number
1624 * other error
1625 */
1626 ++arg;
1627 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001628 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 ((flags & P_VI_DEF) || cp_val)
1630 ? VI_DEFAULT : VIM_DEFAULT];
1631 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001632 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001633 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1634 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001635 if ((long *)varp == &curbuf->b_p_ul
1636 && opt_flags == OPT_LOCAL)
1637 value = NO_LOCAL_UNDOLEVEL;
1638 else
1639 value = *(long *)get_varp_scope(
1640 &(options[opt_idx]), OPT_GLOBAL);
1641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 else if (((long *)varp == &p_wc
1643 || (long *)varp == &p_wcm)
1644 && (*arg == '<'
1645 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001646 || (*arg != NUL
1647 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 && !VIM_ISDIGIT(*arg))))
1649 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001650 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (value == 0 && (long *)varp != &p_wcm)
1652 {
1653 errmsg = e_invarg;
1654 goto skip;
1655 }
1656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1658 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001659 // Allow negative (for 'undolevels'), octal and
1660 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001661 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001662 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001663 if (i == 0 || (arg[i] != NUL
1664 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001666 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 goto skip;
1668 }
1669 }
1670 else
1671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001672 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 goto skip;
1674 }
1675
1676 if (adding)
1677 value = *(long *)varp + value;
1678 if (prepending)
1679 value = *(long *)varp * value;
1680 if (removing)
1681 value = *(long *)varp - value;
1682 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001683 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001685 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001687 char_u *save_arg = NULL;
1688 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001689 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001690 char_u *newval;
1691 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001692 char_u *origval_l = NULL;
1693 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001694#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001695 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001696 char_u *saved_origval_l = NULL;
1697 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001698 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001699#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001700 unsigned newlen;
1701 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001702 int new_value_alloced; // new string option
1703 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001705 // When using ":set opt=val" for a global option
1706 // with a local value the local value will be
1707 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001709 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 varp = options[opt_idx].var;
1711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001712 // The old value is kept until we are sure that the
1713 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001715
Bram Moolenaard7c96872019-06-15 17:12:48 +02001716 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1717 {
1718 origval_l = *(char_u **)get_varp_scope(
1719 &(options[opt_idx]), OPT_LOCAL);
1720 origval_g = *(char_u **)get_varp_scope(
1721 &(options[opt_idx]), OPT_GLOBAL);
1722
1723 // A global-local string option might have an empty
1724 // option as value to indicate that the global
1725 // value should be used.
1726 if (((int)options[opt_idx].indir & PV_BOTH)
1727 && origval_l == empty_option)
1728 origval_l = origval_g;
1729 }
1730
1731 // When setting the local value of a global
1732 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001733 if (((int)options[opt_idx].indir & PV_BOTH)
1734 && (opt_flags & OPT_LOCAL))
1735 origval = *(char_u **)get_varp(
1736 &options[opt_idx]);
1737 else
1738 origval = oldval;
1739
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001740 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 {
1742 newval = options[opt_idx].def_val[
1743 ((flags & P_VI_DEF) || cp_val)
1744 ? VI_DEFAULT : VIM_DEFAULT];
1745 if ((char_u **)varp == &p_bg)
1746 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001747 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#ifdef FEAT_GUI
1749 if (gui.in_use)
1750 newval = gui_bg_default();
1751 else
1752#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001753 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001755 else if ((char_u **)varp == &p_fencs && enc_utf8)
1756 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001758 // expand environment variables and ~ (since the
1759 // default value was already expanded, only
1760 // required when an environment variable was set
1761 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if (newval == NULL)
1763 newval = empty_option;
1764 else
1765 {
1766 s = option_expand(opt_idx, newval);
1767 if (s == NULL)
1768 s = newval;
1769 newval = vim_strsave(s);
1770 }
1771 new_value_alloced = TRUE;
1772 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001773 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 {
1775 newval = vim_strsave(*(char_u **)get_varp_scope(
1776 &(options[opt_idx]), OPT_GLOBAL));
1777 new_value_alloced = TRUE;
1778 }
1779 else
1780 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001781 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 /*
1784 * Set 'keywordprg' to ":help" if an empty
1785 * value was passed to :set by the user.
1786 * Misuse errbuf[] for the resulting string.
1787 */
1788 if (varp == (char_u *)&p_kp
1789 && (*arg == NUL || *arg == ' '))
1790 {
1791 STRCPY(errbuf, ":help");
1792 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001793 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 }
1795 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001796 * Convert 'backspace' number to string, for
1797 * adding, prepending and removing string.
1798 */
1799 else if (varp == (char_u *)&p_bs
1800 && VIM_ISDIGIT(**(char_u **)varp))
1801 {
1802 i = getdigits((char_u **)varp);
1803 switch (i)
1804 {
1805 case 0:
1806 *(char_u **)varp = empty_option;
1807 break;
1808 case 1:
1809 *(char_u **)varp = vim_strsave(
1810 (char_u *)"indent,eol");
1811 break;
1812 case 2:
1813 *(char_u **)varp = vim_strsave(
1814 (char_u *)"indent,eol,start");
1815 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001816 case 3:
1817 *(char_u **)varp = vim_strsave(
1818 (char_u *)"indent,eol,nostop");
1819 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001820 }
1821 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001822 if (origval == oldval)
1823 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001824 if (origval_l == oldval)
1825 origval_l = *(char_u **)varp;
1826 if (origval_g == oldval)
1827 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001828 oldval = *(char_u **)varp;
1829 }
1830 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 * Convert 'whichwrap' number to string, for
1832 * backwards compatibility with Vim 3.0.
1833 * Misuse errbuf[] for the resulting string.
1834 */
1835 else if (varp == (char_u *)&p_ww
1836 && VIM_ISDIGIT(*arg))
1837 {
1838 *errbuf = NUL;
1839 i = getdigits(&arg);
1840 if (i & 1)
1841 STRCAT(errbuf, "b,");
1842 if (i & 2)
1843 STRCAT(errbuf, "s,");
1844 if (i & 4)
1845 STRCAT(errbuf, "h,l,");
1846 if (i & 8)
1847 STRCAT(errbuf, "<,>,");
1848 if (i & 16)
1849 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001850 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 errbuf[STRLEN(errbuf) - 1] = NUL;
1852 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001853 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 }
1855 /*
1856 * Remove '>' before 'dir' and 'bdir', for
1857 * backwards compatibility with version 3.0
1858 */
1859 else if ( *arg == '>'
1860 && (varp == (char_u *)&p_dir
1861 || varp == (char_u *)&p_bdir))
1862 {
1863 ++arg;
1864 }
1865
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 /*
1867 * Copy the new string into allocated memory.
1868 * Can't use set_string_option_direct(), because
1869 * we need to remove the backslashes.
1870 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001871 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 newlen = (unsigned)STRLEN(arg) + 1;
1873 if (adding || prepending || removing)
1874 newlen += (unsigned)STRLEN(origval) + 1;
1875 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001876 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 break;
1878 s = newval;
1879
1880 /*
1881 * Copy the string, skip over escaped chars.
1882 * For MS-DOS and WIN32 backslashes before normal
1883 * file name characters are not removed, and keep
1884 * backslash at start, for "\\machine\path", but
1885 * do remove it for "\\\\machine\\path".
1886 * The reverse is found in ExpandOldSetting().
1887 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001888 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
1890 if (*arg == '\\' && arg[1] != NUL
1891#ifdef BACKSLASH_IN_FILENAME
1892 && !((flags & P_EXPAND)
1893 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001894 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 && (arg[1] != '\\'
1896 || (s == newval
1897 && arg[2] != '\\')))
1898#endif
1899 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001900 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001902 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001904 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 mch_memmove(s, arg, (size_t)i);
1906 arg += i;
1907 s += i;
1908 }
1909 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 *s++ = *arg++;
1911 }
1912 *s = NUL;
1913
1914 /*
1915 * Expand environment variables and ~.
1916 * Don't do it when adding without inserting a
1917 * comma.
1918 */
1919 if (!(adding || prepending || removing)
1920 || (flags & P_COMMA))
1921 {
1922 s = option_expand(opt_idx, newval);
1923 if (s != NULL)
1924 {
1925 vim_free(newval);
1926 newlen = (unsigned)STRLEN(s) + 1;
1927 if (adding || prepending || removing)
1928 newlen += (unsigned)STRLEN(origval) + 1;
1929 newval = alloc(newlen);
1930 if (newval == NULL)
1931 break;
1932 STRCPY(newval, s);
1933 }
1934 }
1935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001936 // locate newval[] in origval[] when removing it
1937 // and when adding to avoid duplicates
1938 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 if (removing || (flags & P_NODUP))
1940 {
1941 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001942 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001944 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001945 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
1947 prepending = FALSE;
1948 adding = FALSE;
1949 STRCPY(newval, origval);
1950 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001951
1952 // if no duplicate, move pointer to end of
1953 // original value
1954 if (s == NULL)
1955 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001958 // concatenate the two strings; add a ',' if
1959 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 if (adding || prepending)
1961 {
1962 comma = ((flags & P_COMMA) && *origval != NUL
1963 && *newval != NUL);
1964 if (adding)
1965 {
1966 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001967 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001968 if (comma && i > 1
1969 && (flags & P_ONECOMMA) == P_ONECOMMA
1970 && origval[i - 1] == ','
1971 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001972 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 mch_memmove(newval + i + comma, newval,
1974 STRLEN(newval) + 1);
1975 mch_memmove(newval, origval, (size_t)i);
1976 }
1977 else
1978 {
1979 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001980 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982 if (comma)
1983 newval[i] = ',';
1984 }
1985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001986 // Remove newval[] from origval[]. (Note: "i" has
1987 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (removing)
1989 {
1990 STRCPY(newval, origval);
1991 if (*s)
1992 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001993 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (flags & P_COMMA)
1995 {
1996 if (s == origval)
1997 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001998 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 if (s[i] == ',')
2000 ++i;
2001 }
2002 else
2003 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002004 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 --s;
2006 ++i;
2007 }
2008 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002009 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011 }
2012
2013 if (flags & P_FLAGLIST)
2014 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002015 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002016 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002018 // if options have P_FLAGLIST and
2019 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002020 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002022 if (*s != ',' && *(s + 1) == ','
2023 && vim_strchr(s + 2, *s) != NULL)
2024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002025 // Remove the duplicated value and
2026 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002027 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002028 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002031 else
2032 {
2033 if ((!(flags & P_COMMA) || *s != ',')
2034 && vim_strchr(s + 1, *s) != NULL)
2035 {
2036 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002037 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002038 }
2039 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002040 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 }
2043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002044 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 arg = save_arg;
2046 new_value_alloced = TRUE;
2047 }
2048
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002049 /*
2050 * Set the new value.
2051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 *(char_u **)(varp) = newval;
2053
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002054#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002055 if (!starting
2056# ifdef FEAT_CRYPT
2057 && options[opt_idx].indir != PV_KEY
2058# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002059 && origval != NULL && newval != NULL)
2060 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002061 // origval may be freed by
2062 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002063 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002064 // newval (and varp) may become invalid if the
2065 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002066 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002067 if (origval_l != NULL)
2068 saved_origval_l = vim_strsave(origval_l);
2069 if (origval_g != NULL)
2070 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002071 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002072#endif
2073
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002074 {
2075 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002076 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002077
2078 // When an option is set in the sandbox, from a
2079 // modeline or in secure mode, then deal with side
2080 // effects in secure mode. Also when the value was
2081 // set with the P_INSECURE flag and is not
2082 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002083 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002084#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002085 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002086#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002087 || (!value_is_replaced && (*p & P_INSECURE)))
2088 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002089
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002090 // Handle side effects, and set the global value
2091 // for ":set" on local options. Note: when setting
2092 // 'syntax' or 'filetype' autocommands may be
2093 // triggered that can cause havoc.
2094 errmsg = did_set_string_option(
2095 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002096 new_value_alloced, oldval, errbuf,
2097 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002098
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002099 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002102#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002103 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002104 trigger_optionsset_string(
2105 opt_idx, opt_flags, saved_origval,
2106 saved_origval_l, saved_origval_g,
2107 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002108 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002109 vim_free(saved_origval_l);
2110 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002111 vim_free(saved_newval);
2112#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002113 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 if (errmsg != NULL)
2115 goto skip;
2116 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002117 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
2119 char_u *p;
2120
2121 if (nextchar == '&')
2122 {
2123 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002124 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 }
2126 else
2127 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002128 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002129 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 if (*p == '\\' && p[1] != NUL)
2131 ++p;
2132 nextchar = *p;
2133 *p = NUL;
2134 add_termcode(key_name, arg, FALSE);
2135 *p = nextchar;
2136 }
2137 if (full_screen)
2138 ttest(FALSE);
2139 redraw_all_later(CLEAR);
2140 }
2141 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002142
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002144 did_set_option(
2145 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 }
2147
2148skip:
2149 /*
2150 * Advance to next argument.
2151 * - skip until a blank found, taking care of backslashes
2152 * - skip blanks
2153 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2154 */
2155 for (i = 0; i < 2 ; ++i)
2156 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002157 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (*arg++ == '\\' && *arg != NUL)
2159 ++arg;
2160 arg = skipwhite(arg);
2161 if (*arg != '=')
2162 break;
2163 }
2164 }
2165
2166 if (errmsg != NULL)
2167 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002168 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002169 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (i + (arg - startarg) < IOSIZE)
2171 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002172 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 STRCAT(IObuff, ": ");
2174 mch_memmove(IObuff + i, startarg, (arg - startarg));
2175 IObuff[i + (arg - startarg)] = NUL;
2176 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002177 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 trans_characters(IObuff, IOSIZE);
2179
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002180 ++no_wait_return; // wait_return done later
2181 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 --no_wait_return;
2183
2184 return FAIL;
2185 }
2186
2187 arg = skipwhite(arg);
2188 }
2189
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002190theend:
2191 if (silent_mode && did_show)
2192 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002193 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002194 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002195 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002196 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002197 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002198 out_flush();
2199 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002200 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002201 }
2202
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 return OK;
2204}
2205
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002206/*
2207 * Call this when an option has been given a new value through a user command.
2208 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2209 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002210 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002211did_set_option(
2212 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002213 int opt_flags, // possibly with OPT_MODELINE
2214 int new_value, // value was replaced completely
2215 int value_checked) // value was checked to be safe, no need to set the
2216 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002217{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002218 long_u *p;
2219
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002220 options[opt_idx].flags |= P_WAS_SET;
2221
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002222 // When an option is set in the sandbox, from a modeline or in secure mode
2223 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2224 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002225 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002226 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002227#ifdef HAVE_SANDBOX
2228 || sandbox != 0
2229#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002230 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002231 *p = *p | P_INSECURE;
2232 else if (new_value)
2233 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002234}
2235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236/*
2237 * Convert a key name or string into a key value.
2238 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002239 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002241 int
2242string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243{
2244 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002245 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 if (*arg == '^')
2247 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002248 if (multi_byte)
2249 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 return *arg;
2251}
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253#ifdef FEAT_TITLE
2254/*
2255 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2256 * maketitle() to create and display it.
2257 * When switching the title or icon off, call mch_restore_title() to get
2258 * the old value back.
2259 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002260 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002261did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262{
2263 if (starting != NO_SCREEN
2264#ifdef FEAT_GUI
2265 && !gui.starting
2266#endif
2267 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269}
2270#endif
2271
2272/*
2273 * set_options_bin - called when 'bin' changes value.
2274 */
2275 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002276set_options_bin(
2277 int oldval,
2278 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002279 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
2281 /*
2282 * The option values that are changed when 'bin' changes are
2283 * copied when 'bin is set and restored when 'bin' is reset.
2284 */
2285 if (newval)
2286 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002287 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 if (!(opt_flags & OPT_GLOBAL))
2290 {
2291 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2292 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2293 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2294 curbuf->b_p_et_nobin = curbuf->b_p_et;
2295 }
2296 if (!(opt_flags & OPT_LOCAL))
2297 {
2298 p_tw_nobin = p_tw;
2299 p_wm_nobin = p_wm;
2300 p_ml_nobin = p_ml;
2301 p_et_nobin = p_et;
2302 }
2303 }
2304
2305 if (!(opt_flags & OPT_GLOBAL))
2306 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002307 curbuf->b_p_tw = 0; // no automatic line wrap
2308 curbuf->b_p_wm = 0; // no automatic line wrap
2309 curbuf->b_p_ml = 0; // no modelines
2310 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312 if (!(opt_flags & OPT_LOCAL))
2313 {
2314 p_tw = 0;
2315 p_wm = 0;
2316 p_ml = FALSE;
2317 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002318 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 }
2320 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002321 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
2323 if (!(opt_flags & OPT_GLOBAL))
2324 {
2325 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2326 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2327 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2328 curbuf->b_p_et = curbuf->b_p_et_nobin;
2329 }
2330 if (!(opt_flags & OPT_LOCAL))
2331 {
2332 p_tw = p_tw_nobin;
2333 p_wm = p_wm_nobin;
2334 p_ml = p_ml_nobin;
2335 p_et = p_et_nobin;
2336 }
2337 }
2338}
2339
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340/*
2341 * Expand environment variables for some string options.
2342 * These string options cannot be indirect!
2343 * If "val" is NULL expand the current value of the option.
2344 * Return pointer to NameBuff, or NULL when not expanded.
2345 */
2346 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002347option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002349 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2351 return NULL;
2352
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002353 // If val is longer than MAXPATHL no meaningful expansion can be done,
2354 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 if (val != NULL && STRLEN(val) > MAXPATHL)
2356 return NULL;
2357
2358 if (val == NULL)
2359 val = *(char_u **)options[opt_idx].var;
2360
2361 /*
2362 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2363 * Escape spaces when expanding 'tags', they are used to separate file
2364 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002365 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 */
2367 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002368 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002369#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002370 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2371#endif
2372 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002373 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 return NULL;
2375
2376 return NameBuff;
2377}
2378
2379/*
2380 * After setting various option values: recompute variables that depend on
2381 * option values.
2382 */
2383 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002384didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002386 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 (void)init_chartab();
2388
Bram Moolenaardac13472019-09-16 21:06:21 +02002389 didset_string_options();
2390
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002391#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002392 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002393 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002394 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002395 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002398 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 (void)check_cedit();
2400#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002401#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002402 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002403 fill_breakat_flags();
2404#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002405 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002406}
2407
2408/*
2409 * More side effects of setting options.
2410 */
2411 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002412didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002413{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002414 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002415 (void)highlight_changed();
2416
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002417 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002418 check_opt_wim();
2419
Bram Moolenaareed9d462021-02-15 20:38:25 +01002420 // Parse default for 'listchars'.
2421 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002423 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002424 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002425
2426#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002427 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002428 (void)check_clipboard_option();
2429#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002430#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002431 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002432 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002433 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002434 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436}
2437
2438/*
2439 * Check for string options that are NULL (normally only termcap options).
2440 */
2441 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002442check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443{
2444 int opt_idx;
2445
2446 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2447 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2448 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2449}
2450
2451/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002452 * Return the option index found by a pointer into term_strings[].
2453 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002455 int
2456get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002458 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2461 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002462 return opt_idx;
2463 return -1; // cannot happen: didn't find it!
2464}
2465
2466/*
2467 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2468 * Return the option index or -1 if not found.
2469 */
2470 int
2471set_term_option_alloced(char_u **p)
2472{
2473 int opt_idx = get_term_opt_idx(p);
2474
2475 if (opt_idx >= 0)
2476 options[opt_idx].flags |= P_ALLOCED;
2477 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478}
2479
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002480#if defined(FEAT_EVAL) || defined(PROTO)
2481/*
2482 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2483 * Return FALSE when it wasn't.
2484 * Return -1 for an unknown option.
2485 */
2486 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002487was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002488{
2489 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002490 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002491
2492 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002493 {
2494 flagp = insecure_flag(idx, opt_flags);
2495 return (*flagp & P_INSECURE) != 0;
2496 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002497 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002498 return -1;
2499}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002500
2501/*
2502 * Get a pointer to the flags used for the P_INSECURE flag of option
2503 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002504 * NOTE: Caller must make sure that "curwin" is set to the window from which
2505 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002506 */
2507 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002508insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002509{
2510 if (opt_flags & OPT_LOCAL)
2511 switch ((int)options[opt_idx].indir)
2512 {
2513#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002514 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002515#endif
2516#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002517# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002518 case PV_FDE: return &curwin->w_p_fde_flags;
2519 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002520# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002521# ifdef FEAT_BEVAL
2522 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2523# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002524# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002525 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002526# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002527 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002528# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002529 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002530# endif
2531#endif
2532 }
2533
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002534 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002535 return &options[opt_idx].flags;
2536}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002537#endif
2538
Bram Moolenaardac13472019-09-16 21:06:21 +02002539#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002540/*
2541 * Redraw the window title and/or tab page text later.
2542 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002543void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002544{
2545 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002546 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002547}
2548#endif
2549
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002551 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2552 * characters or characters in "allowed".
2553 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002554 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002555valid_name(char_u *val, char *allowed)
2556{
2557 char_u *s;
2558
2559 for (s = val; *s != NUL; ++s)
2560 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2561 return FALSE;
2562 return TRUE;
2563}
2564
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002565#if defined(FEAT_EVAL) || defined(PROTO)
2566/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002567 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002568 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002569 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002570 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002571set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002572{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002573 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2574 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002575 sctx_T new_script_ctx = script_ctx;
2576
Bram Moolenaar51258742020-05-03 17:19:33 +02002577 // Modeline already has the line number set.
2578 if (!(opt_flags & OPT_MODELINE))
2579 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002580
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002581 // Remember where the option was set. For local options need to do that
2582 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002583 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002584 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002585 if (both || (opt_flags & OPT_LOCAL))
2586 {
2587 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002588 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002589 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002590 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002591 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002592}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002593
2594/*
2595 * Set the script_ctx for a termcap option.
2596 * "name" must be the two character code, e.g. "RV".
2597 * When "name" is NULL use "opt_idx".
2598 */
2599 void
2600set_term_option_sctx_idx(char *name, int opt_idx)
2601{
2602 char_u buf[5];
2603 int idx;
2604
2605 if (name == NULL)
2606 idx = opt_idx;
2607 else
2608 {
2609 buf[0] = 't';
2610 buf[1] = '_';
2611 buf[2] = name[0];
2612 buf[3] = name[1];
2613 buf[4] = 0;
2614 idx = findoption(buf);
2615 }
2616 if (idx >= 0)
2617 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2618}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002619#endif
2620
Bram Moolenaarf4140482020-02-15 23:06:45 +01002621#if defined(FEAT_EVAL)
2622/*
2623 * Apply the OptionSet autocommand.
2624 */
2625 static void
2626apply_optionset_autocmd(
2627 int opt_idx,
2628 long opt_flags,
2629 long oldval,
2630 long oldval_g,
2631 long newval,
2632 char *errmsg)
2633{
2634 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2635
2636 // Don't do this while starting up, failure or recursively.
2637 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2638 return;
2639
2640 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2641 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2642 oldval_g);
2643 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2644 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2645 (opt_flags & OPT_LOCAL) ? "local" : "global");
2646 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2647 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2648 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2649 if (opt_flags & OPT_LOCAL)
2650 {
2651 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2652 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2653 }
2654 if (opt_flags & OPT_GLOBAL)
2655 {
2656 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2657 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2658 }
2659 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2660 {
2661 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2662 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2663 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2664 }
2665 if (opt_flags & OPT_MODELINE)
2666 {
2667 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2668 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2669 }
2670 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2671 NULL, FALSE, NULL);
2672 reset_v_option_vars();
2673}
2674#endif
2675
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676/*
2677 * Set the value of a boolean option, and take care of side effects.
2678 * Returns NULL for success, or an error message for an error.
2679 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002680 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002681set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002682 int opt_idx, // index in options[] table
2683 char_u *varp, // pointer to the option variable
2684 int value, // new value
2685 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002688#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002689 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002692 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if ((secure
2694#ifdef HAVE_SANDBOX
2695 || sandbox != 0
2696#endif
2697 ) && (options[opt_idx].flags & P_SECURE))
2698 return e_secure;
2699
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002700#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002701 // Save the global value before changing anything. This is needed as for
2702 // a global-only option setting the "local value" in fact sets the global
2703 // value (since there is only one value).
2704 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2705 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2706 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002707#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002708
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002711 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002712 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713#endif
2714
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002715#ifdef FEAT_GUI
2716 need_mouse_correct = TRUE;
2717#endif
2718
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002719 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2721 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2722
2723 /*
2724 * Handle side effects of changing a bool option.
2725 */
2726
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002727 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
Bram Moolenaar920694c2016-08-21 17:45:02 +02002731#ifdef FEAT_LANGMAP
2732 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002734 p_lnr = !p_lrm;
2735 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002737 p_lrm = !p_lnr;
2738#endif
2739
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002740#ifdef FEAT_SYN_HL
2741 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2742 reset_cursorline();
2743#endif
2744
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002745#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002747 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2748 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002749 // Only take action when the option was set. When reset we do not
2750 // delete the undo file, the option may be set again without making
2751 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002752 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002753 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002754 char_u hash[UNDO_HASH_SIZE];
2755 buf_T *save_curbuf = curbuf;
2756
Bram Moolenaar29323592016-07-24 22:04:11 +02002757 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002758 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002759 // When 'undofile' is set globally: for every buffer, otherwise
2760 // only for the current buffer: Try to read in the undofile,
2761 // if one exists, the buffer wasn't changed and the buffer was
2762 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002763 if ((curbuf == save_curbuf
2764 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2765 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2766 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002767#ifdef FEAT_CRYPT
2768 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2769 continue;
2770#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002771 u_compute_hash(hash);
2772 u_read_undo(NULL, hash, curbuf->b_fname);
2773 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002774 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002775 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002776 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002777 }
2778#endif
2779
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 else if ((int *)varp == &curbuf->b_p_ro)
2781 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002782 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2784 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002785
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002786 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002787 if (curbuf->b_p_ro)
2788 curbuf->b_did_warn = FALSE;
2789
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002791 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792#endif
2793 }
2794
Bram Moolenaar9c449722010-07-20 18:44:27 +02002795#ifdef FEAT_GUI
2796 else if ((int *)varp == &p_mh)
2797 {
2798 if (!p_mh)
2799 gui_mch_mousehide(FALSE);
2800 }
2801#endif
2802
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002803 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002805 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002806# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002807 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002808 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2809 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002810 {
2811 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002812 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002813 }
2814# endif
2815# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002816 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002817# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002818 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002819#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002822 {
2823 redraw_titles();
2824 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002825 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002826 else if ((int *)varp == &curbuf->b_p_fixeol)
2827 {
2828 redraw_titles();
2829 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002830 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002831 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002832 {
2833 redraw_titles();
2834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835#endif
2836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002837 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 else if ((int *)varp == &curbuf->b_p_bin)
2839 {
2840 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2841#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002842 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843#endif
2844 }
2845
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002846 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2848 {
2849 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2850 NULL, NULL, TRUE, curbuf);
2851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 else if ((int *)varp == &curbuf->b_p_swf)
2855 {
2856 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002857 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2860 // buf->b_p_swf
2861 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
2863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002864 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 else if ((int *)varp == &p_terse)
2866 {
2867 char_u *p;
2868
2869 p = vim_strchr(p_shm, SHM_SEARCH);
2870
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002871 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (p_terse && p == NULL)
2873 {
2874 STRCPY(IObuff, p_shm);
2875 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002876 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002880 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
2882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002883 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 else if ((int *)varp == &p_paste)
2885 {
2886 paste_option_changed();
2887 }
2888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 else if ((int *)varp == &p_im)
2891 {
2892 if (p_im)
2893 {
2894 if ((State & INSERT) == 0)
2895 need_start_insertmode = TRUE;
2896 stop_insert_mode = FALSE;
2897 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002898 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002899 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 {
2901 need_start_insertmode = FALSE;
2902 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002903 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 restart_edit = 0;
2906 }
2907 }
2908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 else if ((int *)varp == &p_ic && p_hls)
2911 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002912 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
2914
2915#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 else if ((int *)varp == &p_hls)
2918 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002919 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
2921#endif
2922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2924 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 else if ((int *)varp == &curwin->w_p_scb)
2926 {
2927 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002928 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002930 curwin->w_scbind_pos = curwin->w_topline;
2931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933
Bram Moolenaar4033c552017-09-16 20:54:51 +02002934#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002935 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 else if ((int *)varp == &curwin->w_p_pvw)
2937 {
2938 if (curwin->w_p_pvw)
2939 {
2940 win_T *win;
2941
Bram Moolenaar29323592016-07-24 22:04:11 +02002942 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 if (win->w_p_pvw && win != curwin)
2944 {
2945 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002946 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948 }
2949 }
2950#endif
2951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 else if ((int *)varp == &curbuf->b_p_tx)
2954 {
2955 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2956 }
2957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002960 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 set_string_option_direct((char_u *)"ffs", -1,
2962 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002963 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
2966 /*
2967 * When 'lisp' option changes include/exclude '-' in
2968 * keyword characters.
2969 */
2970#ifdef FEAT_LISP
2971 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2972 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002973 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 }
2975#endif
2976
2977#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002978 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002979 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002981 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
2983#endif
2984
2985 else if ((int *)varp == &curbuf->b_changed)
2986 {
2987 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002990 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
2994
2995#ifdef BACKSLASH_IN_FILENAME
2996 else if ((int *)varp == &p_ssl)
2997 {
2998 if (p_ssl)
2999 {
3000 psepc = '/';
3001 psepcN = '\\';
3002 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004 else
3005 {
3006 psepc = '\\';
3007 psepcN = '/';
3008 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 }
3010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003011 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 buflist_slash_adjust();
3013 alist_slash_adjust();
3014# ifdef FEAT_EVAL
3015 scriptnames_slash_adjust();
3016# endif
3017 }
3018#endif
3019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003020 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 else if ((int *)varp == &curwin->w_p_wrap)
3022 {
3023 if (curwin->w_p_wrap)
3024 curwin->w_leftcol = 0;
3025 }
3026
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 else if ((int *)varp == &p_ea)
3028 {
3029 if (p_ea && !old_value)
3030 win_equal(curwin, FALSE, 0);
3031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
3033 else if ((int *)varp == &p_wiv)
3034 {
3035 /*
3036 * When 'weirdinvert' changed, set/reset 't_xs'.
3037 * Then set 'weirdinvert' according to value of 't_xs'.
3038 */
3039 if (p_wiv && !old_value)
3040 T_XS = (char_u *)"y";
3041 else if (!p_wiv && old_value)
3042 T_XS = empty_option;
3043 p_wiv = (*T_XS != NUL);
3044 }
3045
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003046#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 else if ((int *)varp == &p_beval)
3048 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003049 if (!balloonEvalForTerm)
3050 {
3051 if (p_beval && !old_value)
3052 gui_mch_enable_beval_area(balloonEval);
3053 else if (!p_beval && old_value)
3054 gui_mch_disable_beval_area(balloonEval);
3055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003057#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003058#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003059 else if ((int *)varp == &p_bevalterm)
3060 {
3061 mch_bevalterm_changed();
3062 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003065#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 else if ((int *)varp == &p_acd)
3067 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003069 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 }
3071#endif
3072
3073#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003074 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 else if ((int *)varp == &curwin->w_p_diff)
3076 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003077 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003078 diff_buf_adjust(curwin);
3079# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 if (foldmethodIsDiff(curwin))
3081 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003082# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 }
3084#endif
3085
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003086#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003087 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 else if ((int *)varp == &p_imdisable)
3089 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003090 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 if (p_imdisable)
3092 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003093 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 // When the option is set from an autocommand, it may need to take
3095 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003096 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 }
3098#endif
3099
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003100#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003101 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003102 else if ((int *)varp == &curwin->w_p_spell)
3103 {
3104 if (curwin->w_p_spell)
3105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003106 char *errmsg = did_set_spelllang(curwin);
3107
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003108 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003109 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003110 }
3111 }
3112#endif
3113
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114#ifdef FEAT_ARABIC
3115 if ((int *)varp == &curwin->w_p_arab)
3116 {
3117 if (curwin->w_p_arab)
3118 {
3119 /*
3120 * 'arabic' is set, handle various sub-settings.
3121 */
3122 if (!p_tbidi)
3123 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003124 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 if (!curwin->w_p_rl)
3126 {
3127 curwin->w_p_rl = TRUE;
3128 changed_window_setting();
3129 }
3130
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003131 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 if (!p_arshape)
3133 {
3134 p_arshape = TRUE;
3135 redraw_later_clear();
3136 }
3137 }
3138
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003139 // Arabic requires a utf-8 encoding, inform the user if its not
3140 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003142 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003143 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3144
Bram Moolenaar8820b482017-03-16 17:23:31 +01003145 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003146 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003147#ifdef FEAT_EVAL
3148 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3149#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003152 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003156 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3158 OPT_LOCAL);
3159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 }
3161 else
3162 {
3163 /*
3164 * 'arabic' is reset, handle various sub-settings.
3165 */
3166 if (!p_tbidi)
3167 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003168 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 if (curwin->w_p_rl)
3170 {
3171 curwin->w_p_rl = FALSE;
3172 changed_window_setting();
3173 }
3174
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003175 // 'arabicshape' isn't reset, it is a global option and
3176 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 }
3178
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003179 // 'delcombine' isn't reset, it is a global option and another
3180 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181
3182# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003183 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 curbuf->b_p_iminsert = B_IMODE_NONE;
3185 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3186# endif
3187 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003188 }
3189
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003190#endif
3191
Bram Moolenaar44826212019-08-22 21:23:20 +02003192#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3193 else if (((int *)varp == &curwin->w_p_nu
3194 || (int *)varp == &curwin->w_p_rnu)
3195 && gui.in_use
3196 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3197 && curbuf->b_signlist != NULL)
3198 {
3199 // If the 'number' or 'relativenumber' options are modified and
3200 // 'signcolumn' is set to 'number', then clear the screen for a full
3201 // refresh. Otherwise the sign icons are not displayed properly in the
3202 // number column. If the 'number' option is set and only the
3203 // 'relativenumber' option is toggled, then don't refresh the screen
3204 // (optimization).
3205 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3206 redraw_all_later(CLEAR);
3207 }
3208#endif
3209
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003210#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003211 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003212 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003213 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003214# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003215 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003216 if (
3217# ifdef VIMDLL
3218 !gui.in_use && !gui.starting &&
3219# endif
3220 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003221 {
3222 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003223 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003224 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003225 if (is_term_win32())
3226 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003227# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003228# ifdef FEAT_GUI
3229 if (!gui.in_use && !gui.starting)
3230# endif
3231 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003232# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003233 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003234 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003235 {
3236 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003237 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003238 init_highlight(TRUE, FALSE);
3239 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003240# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003241 }
3242#endif
3243
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 /*
3245 * End of handling side effects for bool options.
3246 */
3247
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003248 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003249
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 options[opt_idx].flags |= P_WAS_SET;
3251
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003252#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003253 apply_optionset_autocmd(opt_idx, opt_flags,
3254 (long)(old_value ? TRUE : FALSE),
3255 (long)(old_global_value ? TRUE : FALSE),
3256 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003257#endif
3258
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003259 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003260 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003261 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003262 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003263
3264 if ((opt_flags & OPT_NO_REDRAW) == 0)
3265 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266
3267 return NULL;
3268}
3269
3270/*
3271 * Set the value of a number option, and take care of side effects.
3272 * Returns NULL for success, or an error message for an error.
3273 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003274 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003275set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003276 int opt_idx, // index in options[] table
3277 char_u *varp, // pointer to the option variable
3278 long value, // new value
3279 char *errbuf, // buffer for error messages
3280 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003281 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3282 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003284 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003286#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003287 long old_global_value = 0; // only used when setting a local and
3288 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003289#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003290 long old_Rows = Rows; // remember old Rows
3291 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 long *pp = (long *)varp;
3293
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003294 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003295 if ((secure
3296#ifdef HAVE_SANDBOX
3297 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003299 ) && (options[opt_idx].flags & P_SECURE))
3300 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003302#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003303 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003304 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003305 // value (since there is only one value).
3306 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003307 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3308 OPT_GLOBAL);
3309#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003310
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 *pp = value;
3312#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003313 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003314 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003316#ifdef FEAT_GUI
3317 need_mouse_correct = TRUE;
3318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319
Bram Moolenaar14f24742012-08-08 18:01:05 +02003320 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 {
3322 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003323#ifdef FEAT_VARTABS
3324 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3325 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3326 ? tabstop_first(curbuf->b_p_vts_array)
3327 : curbuf->b_p_ts;
3328#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 }
3332
3333 /*
3334 * Number options that need some action when changed
3335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 if (pp == &p_wh || pp == &p_hh)
3337 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003338 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 if (p_wh < 1)
3340 {
3341 errmsg = e_positive;
3342 p_wh = 1;
3343 }
3344 if (p_wmh > p_wh)
3345 {
3346 errmsg = e_winheight;
3347 p_wh = p_wmh;
3348 }
3349 if (p_hh < 0)
3350 {
3351 errmsg = e_positive;
3352 p_hh = 0;
3353 }
3354
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003355 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003356 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
3358 if (pp == &p_wh && curwin->w_height < p_wh)
3359 win_setheight((int)p_wh);
3360 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3361 win_setheight((int)p_hh);
3362 }
3363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 else if (pp == &p_wmh)
3365 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003366 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 if (p_wmh < 0)
3368 {
3369 errmsg = e_positive;
3370 p_wmh = 0;
3371 }
3372 if (p_wmh > p_wh)
3373 {
3374 errmsg = e_winheight;
3375 p_wmh = p_wh;
3376 }
3377 win_setminheight();
3378 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003379 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003381 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (p_wiw < 1)
3383 {
3384 errmsg = e_positive;
3385 p_wiw = 1;
3386 }
3387 if (p_wmw > p_wiw)
3388 {
3389 errmsg = e_winwidth;
3390 p_wiw = p_wmw;
3391 }
3392
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003393 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003394 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 win_setwidth((int)p_wiw);
3396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 else if (pp == &p_wmw)
3398 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003399 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 if (p_wmw < 0)
3401 {
3402 errmsg = e_positive;
3403 p_wmw = 0;
3404 }
3405 if (p_wmw > p_wiw)
3406 {
3407 errmsg = e_winwidth;
3408 p_wmw = p_wiw;
3409 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003410 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003413 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 else if (pp == &p_ls)
3415 {
3416 last_status(FALSE);
3417 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003419 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003420 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003421 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003422 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425#ifdef FEAT_GUI
3426 else if (pp == &p_linespace)
3427 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003428 // Recompute gui.char_height and resize the Vim window to keep the
3429 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003430 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003431 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
3433#endif
3434
3435#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003436 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 else if (pp == &curwin->w_p_fdl)
3438 {
3439 if (curwin->w_p_fdl < 0)
3440 curwin->w_p_fdl = 0;
3441 newFoldLevel();
3442 }
3443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003444 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 else if (pp == &curwin->w_p_fml)
3446 {
3447 foldUpdateAll(curwin);
3448 }
3449
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003450 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 else if (pp == &curwin->w_p_fdn)
3452 {
3453 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3454 foldUpdateAll(curwin);
3455 }
3456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003457 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 else if (pp == &curwin->w_p_fdc)
3459 {
3460 if (curwin->w_p_fdc < 0)
3461 {
3462 errmsg = e_positive;
3463 curwin->w_p_fdc = 0;
3464 }
3465 else if (curwin->w_p_fdc > 12)
3466 {
3467 errmsg = e_invarg;
3468 curwin->w_p_fdc = 12;
3469 }
3470 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003471#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003473#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003474 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3476 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003477# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (foldmethodIsIndent(curwin))
3479 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003480# endif
3481# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003482 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3483 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003484 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3485 parse_cino(curbuf);
3486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003490 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003491 else if (pp == &p_mco)
3492 {
3493 if (p_mco > MAX_MCO)
3494 p_mco = MAX_MCO;
3495 else if (p_mco < 0)
3496 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003497 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003498 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 else if (pp == &curbuf->b_p_iminsert)
3501 {
3502 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3503 {
3504 errmsg = e_invarg;
3505 curbuf->b_p_iminsert = B_IMODE_NONE;
3506 }
3507 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003508 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003510#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003511 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 status_redraw_curbuf();
3513#endif
3514 }
3515
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003516#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003517 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003518 else if (pp == &p_imst)
3519 {
3520 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3521 errmsg = e_invarg;
3522 }
3523#endif
3524
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003525 else if (pp == &p_window)
3526 {
3527 if (p_window < 1)
3528 p_window = 1;
3529 else if (p_window >= Rows)
3530 p_window = Rows - 1;
3531 }
3532
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 else if (pp == &curbuf->b_p_imsearch)
3534 {
3535 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3536 {
3537 errmsg = e_invarg;
3538 curbuf->b_p_imsearch = B_IMODE_NONE;
3539 }
3540 p_imsearch = curbuf->b_p_imsearch;
3541 }
3542
3543#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003544 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 else if (pp == &p_titlelen)
3546 {
3547 if (p_titlelen < 0)
3548 {
3549 errmsg = e_positive;
3550 p_titlelen = 85;
3551 }
3552 if (starting != NO_SCREEN && old_value != p_titlelen)
3553 need_maketitle = TRUE;
3554 }
3555#endif
3556
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003557 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 else if (pp == &p_ch)
3559 {
3560 if (p_ch < 1)
3561 {
3562 errmsg = e_positive;
3563 p_ch = 1;
3564 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003565 if (p_ch > Rows - min_rows() + 1)
3566 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003568 // Only compute the new window layout when startup has been
3569 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 if (p_ch != old_value && full_screen
3571#ifdef FEAT_GUI
3572 && !gui.starting
3573#endif
3574 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003575 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003578 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 else if (pp == &p_uc)
3580 {
3581 if (p_uc < 0)
3582 {
3583 errmsg = e_positive;
3584 p_uc = 100;
3585 }
3586 if (p_uc && !old_value)
3587 ml_open_files();
3588 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003590 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003591 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003592 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003593 {
3594 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003595 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003596 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003597 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003598 {
3599 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003600 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003601 }
3602 }
3603#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003604#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003605 else if (pp == &p_mzq)
3606 mzvim_reset_timer();
3607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003609#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003610 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003611 else if (pp == &p_pyx)
3612 {
3613 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3614 errmsg = e_invarg;
3615 }
3616#endif
3617
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003618 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 else if (pp == &p_ul)
3620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003621 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003623 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 p_ul = value;
3625 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003626 else if (pp == &curbuf->b_p_ul)
3627 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003628 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003629 curbuf->b_p_ul = old_value;
3630 u_sync(TRUE);
3631 curbuf->b_p_ul = value;
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003634#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003635 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003636 else if (pp == &curwin->w_p_nuw)
3637 {
3638 if (curwin->w_p_nuw < 1)
3639 {
3640 errmsg = e_positive;
3641 curwin->w_p_nuw = 1;
3642 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003643 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003644 {
3645 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003646 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003647 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003648 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003649 }
3650#endif
3651
Bram Moolenaar1a384422010-07-14 19:53:30 +02003652 else if (pp == &curbuf->b_p_tw)
3653 {
3654 if (curbuf->b_p_tw < 0)
3655 {
3656 errmsg = e_positive;
3657 curbuf->b_p_tw = 0;
3658 }
3659#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003660 {
3661 win_T *wp;
3662 tabpage_T *tp;
3663
3664 FOR_ALL_TAB_WINDOWS(tp, wp)
3665 check_colorcolumn(wp);
3666 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003667#endif
3668 }
3669
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 /*
3671 * Check the bounds for numeric options here
3672 */
3673 if (Rows < min_rows() && full_screen)
3674 {
3675 if (errbuf != NULL)
3676 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003677 vim_snprintf((char *)errbuf, errbuflen,
3678 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 errmsg = errbuf;
3680 }
3681 Rows = min_rows();
3682 }
3683 if (Columns < MIN_COLUMNS && full_screen)
3684 {
3685 if (errbuf != NULL)
3686 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003687 vim_snprintf((char *)errbuf, errbuflen,
3688 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 errmsg = errbuf;
3690 }
3691 Columns = MIN_COLUMNS;
3692 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003693 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 /*
3696 * If the screen (shell) height has been changed, assume it is the
3697 * physical screenheight.
3698 */
3699 if (old_Rows != Rows || old_Columns != Columns)
3700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003701 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 if (updating_screen)
3703 *pp = old_value;
3704 else if (full_screen
3705#ifdef FEAT_GUI
3706 && !gui.starting
3707#endif
3708 )
3709 set_shellsize((int)Columns, (int)Rows, TRUE);
3710 else
3711 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003712 // Postpone the resizing; check the size and cmdline position for
3713 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 check_shellsize();
3715 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3716 cmdline_row = Rows - p_ch;
3717 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003718 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003719 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 if (curbuf->b_p_ts <= 0)
3723 {
3724 errmsg = e_positive;
3725 curbuf->b_p_ts = 8;
3726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 if (p_tm < 0)
3728 {
3729 errmsg = e_positive;
3730 p_tm = 0;
3731 }
3732 if ((curwin->w_p_scr <= 0
3733 || (curwin->w_p_scr > curwin->w_height
3734 && curwin->w_height > 0))
3735 && full_screen)
3736 {
3737 if (pp == &(curwin->w_p_scr))
3738 {
3739 if (curwin->w_p_scr != 0)
3740 errmsg = e_scroll;
3741 win_comp_scroll(curwin);
3742 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003743 // If 'scroll' became invalid because of a side effect silently adjust
3744 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 else if (curwin->w_p_scr <= 0)
3746 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003747 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 curwin->w_p_scr = curwin->w_height;
3749 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003750 if (p_hi < 0)
3751 {
3752 errmsg = e_positive;
3753 p_hi = 0;
3754 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003755 else if (p_hi > 10000)
3756 {
3757 errmsg = e_invarg;
3758 p_hi = 10000;
3759 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003760 if (p_re < 0 || p_re > 2)
3761 {
3762 errmsg = e_invarg;
3763 p_re = 0;
3764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 if (p_report < 0)
3766 {
3767 errmsg = e_positive;
3768 p_report = 1;
3769 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003770 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003772 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 p_sj = Rows / 2;
3774 else
3775 {
3776 errmsg = e_scroll;
3777 p_sj = 1;
3778 }
3779 }
3780 if (p_so < 0 && full_screen)
3781 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003782 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 p_so = 0;
3784 }
3785 if (p_siso < 0 && full_screen)
3786 {
3787 errmsg = e_positive;
3788 p_siso = 0;
3789 }
3790#ifdef FEAT_CMDWIN
3791 if (p_cwh < 1)
3792 {
3793 errmsg = e_positive;
3794 p_cwh = 1;
3795 }
3796#endif
3797 if (p_ut < 0)
3798 {
3799 errmsg = e_positive;
3800 p_ut = 2000;
3801 }
3802 if (p_ss < 0)
3803 {
3804 errmsg = e_positive;
3805 p_ss = 0;
3806 }
3807
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003808 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3810 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3811
3812 options[opt_idx].flags |= P_WAS_SET;
3813
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003814#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003815 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3816 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003817#endif
3818
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003819 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003820 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003821 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003822 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003823 if ((opt_flags & OPT_NO_REDRAW) == 0)
3824 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
3826 return errmsg;
3827}
3828
3829/*
3830 * Called after an option changed: check if something needs to be redrawn.
3831 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003832 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003833check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003835 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003836 int doclear = (flags & P_RCLR) == P_RCLR;
3837 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003839 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
3842 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3843 changed_window_setting();
3844 if (flags & P_RBUF)
3845 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003846 if (flags & P_RWINONLY)
3847 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003848 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 redraw_all_later(CLEAR);
3850 else if (all)
3851 redraw_all_later(NOT_VALID);
3852}
3853
3854/*
3855 * Find index for option 'arg'.
3856 * Return -1 if not found.
3857 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003858 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003859findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860{
3861 int opt_idx;
3862 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003863 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 int is_term_opt;
3865
3866 /*
3867 * For first call: Initialize the quick-access table.
3868 * It contains the index for the first option that starts with a certain
3869 * letter. There are 26 letters, plus the first "t_" option.
3870 */
3871 if (quick_tab[1] == 0)
3872 {
3873 p = options[0].fullname;
3874 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3875 {
3876 if (s[0] != p[0])
3877 {
3878 if (s[0] == 't' && s[1] == '_')
3879 quick_tab[26] = opt_idx;
3880 else
3881 quick_tab[CharOrdLow(s[0])] = opt_idx;
3882 }
3883 p = s;
3884 }
3885 }
3886
3887 /*
3888 * Check for name starting with an illegal character.
3889 */
3890#ifdef EBCDIC
3891 if (!islower(arg[0]))
3892#else
3893 if (arg[0] < 'a' || arg[0] > 'z')
3894#endif
3895 return -1;
3896
3897 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3898 if (is_term_opt)
3899 opt_idx = quick_tab[26];
3900 else
3901 opt_idx = quick_tab[CharOrdLow(arg[0])];
3902 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3903 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003904 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 break;
3906 }
3907 if (s == NULL && !is_term_opt)
3908 {
3909 opt_idx = quick_tab[CharOrdLow(arg[0])];
3910 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3911 {
3912 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003913 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 break;
3915 s = NULL;
3916 }
3917 }
3918 if (s == NULL)
3919 opt_idx = -1;
3920 return opt_idx;
3921}
3922
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003923#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924/*
3925 * Get the value for an option.
3926 *
3927 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003928 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003929 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003930 * String option: gov_string, *stringval gets allocated string.
3931 * Hidden Number option: gov_hidden_number.
3932 * Hidden Toggle option: gov_hidden_bool.
3933 * Hidden String option: gov_hidden_string.
3934 * Unknown option: gov_unknown.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003936 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003937get_option_value(
3938 char_u *name,
3939 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003940 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003941 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
3943 int opt_idx;
3944 char_u *varp;
3945
3946 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003947 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003948 {
3949 int key;
3950
3951 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003952 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003953 {
3954 char_u key_name[2];
3955 char_u *p;
3956
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003957 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003958 if (key < 0)
3959 {
3960 key_name[0] = KEY2TERMCAP0(key);
3961 key_name[1] = KEY2TERMCAP1(key);
3962 }
3963 else
3964 {
3965 key_name[0] = KS_KEY;
3966 key_name[1] = (key & 0xff);
3967 }
3968 p = find_termcode(key_name);
3969 if (p != NULL)
3970 {
3971 if (stringval != NULL)
3972 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003973 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003974 }
3975 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003976 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
3979 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3980
3981 if (options[opt_idx].flags & P_STRING)
3982 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003983 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003984 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 if (stringval != NULL)
3986 {
3987#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003988 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003989 if ((char_u **)varp == &curbuf->b_p_key
3990 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 *stringval = vim_strsave((char_u *)"*****");
3992 else
3993#endif
3994 *stringval = vim_strsave(*(char_u **)(varp));
3995 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003996 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
3998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003999 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004000 return (options[opt_idx].flags & P_NUM)
4001 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 if (options[opt_idx].flags & P_NUM)
4003 *numval = *(long *)varp;
4004 else
4005 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004006 // Special case: 'modified' is b_changed, but we also want to consider
4007 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 if ((int *)varp == &curbuf->b_changed)
4009 *numval = curbufIsChanged();
4010 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004011 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004013 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014}
4015#endif
4016
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004017#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004018/*
4019 * Returns the option attributes and its value. Unlike the above function it
4020 * will return either global value or local value of the option depending on
4021 * what was requested, but it will never return global value if it was
4022 * requested to return local one and vice versa. Neither it will return
4023 * buffer-local value if it was requested to return window-local one.
4024 *
4025 * Pretends that option is absent if it is not present in the requested scope
4026 * (i.e. has no global, window-local or buffer-local value depending on
4027 * opt_type). Uses
4028 *
4029 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004030 * 0 hidden or unknown option, also option that does not have requested
4031 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004032 * see SOPT_* in vim.h for other flags
4033 *
4034 * Possible opt_type values: see SREQ_* in vim.h
4035 */
4036 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004037get_option_value_strict(
4038 char_u *name,
4039 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004040 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004041 int opt_type,
4042 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004043{
4044 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004045 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004046 struct vimoption *p;
4047 int r = 0;
4048
4049 opt_idx = findoption(name);
4050 if (opt_idx < 0)
4051 return 0;
4052
4053 p = &(options[opt_idx]);
4054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004055 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004056 if (p->var == NULL)
4057 return 0;
4058
4059 if (p->flags & P_BOOL)
4060 r |= SOPT_BOOL;
4061 else if (p->flags & P_NUM)
4062 r |= SOPT_NUM;
4063 else if (p->flags & P_STRING)
4064 r |= SOPT_STRING;
4065
4066 if (p->indir == PV_NONE)
4067 {
4068 if (opt_type == SREQ_GLOBAL)
4069 r |= SOPT_GLOBAL;
4070 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004071 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004072 }
4073 else
4074 {
4075 if (p->indir & PV_BOTH)
4076 r |= SOPT_GLOBAL;
4077 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004078 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004079
4080 if (p->indir & PV_WIN)
4081 {
4082 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004083 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004084 else
4085 r |= SOPT_WIN;
4086 }
4087 else if (p->indir & PV_BUF)
4088 {
4089 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004090 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004091 else
4092 r |= SOPT_BUF;
4093 }
4094 }
4095
4096 if (stringval == NULL)
4097 return r;
4098
4099 if (opt_type == SREQ_GLOBAL)
4100 varp = p->var;
4101 else
4102 {
4103 if (opt_type == SREQ_BUF)
4104 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004105 // Special case: 'modified' is b_changed, but we also want to
4106 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004107 if (p->indir == PV_MOD)
4108 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004109 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004110 varp = NULL;
4111 }
4112#ifdef FEAT_CRYPT
4113 else if (p->indir == PV_KEY)
4114 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004115 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004116 *stringval = NULL;
4117 varp = NULL;
4118 }
4119#endif
4120 else
4121 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004122 buf_T *save_curbuf = curbuf;
4123
4124 // only getting a pointer, no need to use aucmd_prepbuf()
4125 curbuf = (buf_T *)from;
4126 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004127 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004128 curbuf = save_curbuf;
4129 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004130 }
4131 }
4132 else if (opt_type == SREQ_WIN)
4133 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004134 win_T *save_curwin = curwin;
4135
4136 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004137 curbuf = curwin->w_buffer;
4138 varp = get_varp(p);
4139 curwin = save_curwin;
4140 curbuf = curwin->w_buffer;
4141 }
4142 if (varp == p->var)
4143 return (r | SOPT_UNSET);
4144 }
4145
4146 if (varp != NULL)
4147 {
4148 if (p->flags & P_STRING)
4149 *stringval = vim_strsave(*(char_u **)(varp));
4150 else if (p->flags & P_NUM)
4151 *numval = *(long *) varp;
4152 else
4153 *numval = *(int *)varp;
4154 }
4155
4156 return r;
4157}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004158
4159/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004160 * Iterate over options. First argument is a pointer to a pointer to a
4161 * structure inside options[] array, second is option type like in the above
4162 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004163 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004164 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004165 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004166 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004167 * first argument to NULL.
4168 *
4169 * Returns full option name for current option on each call.
4170 */
4171 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004172option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004173{
4174 struct vimoption *ret = NULL;
4175 do
4176 {
4177 if (*option == NULL)
4178 *option = (void *) options;
4179 else if (((struct vimoption *) (*option))->fullname == NULL)
4180 {
4181 *option = NULL;
4182 return NULL;
4183 }
4184 else
4185 *option = (void *) (((struct vimoption *) (*option)) + 1);
4186
4187 ret = ((struct vimoption *) (*option));
4188
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004189 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004190 if (ret->var == NULL)
4191 {
4192 ret = NULL;
4193 continue;
4194 }
4195
4196 switch (opt_type)
4197 {
4198 case SREQ_GLOBAL:
4199 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4200 ret = NULL;
4201 break;
4202 case SREQ_BUF:
4203 if (!(ret->indir & PV_BUF))
4204 ret = NULL;
4205 break;
4206 case SREQ_WIN:
4207 if (!(ret->indir & PV_WIN))
4208 ret = NULL;
4209 break;
4210 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004211 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004212 return NULL;
4213 }
4214 }
4215 while (ret == NULL);
4216
4217 return (char_u *)ret->fullname;
4218}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004219#endif
4220
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004222 * Return the flags for the option at 'opt_idx'.
4223 */
4224 long_u
4225get_option_flags(int opt_idx)
4226{
4227 return options[opt_idx].flags;
4228}
4229
4230/*
4231 * Set a flag for the option at 'opt_idx'.
4232 */
4233 void
4234set_option_flag(int opt_idx, long_u flag)
4235{
4236 options[opt_idx].flags |= flag;
4237}
4238
4239/*
4240 * Clear a flag for the option at 'opt_idx'.
4241 */
4242 void
4243clear_option_flag(int opt_idx, long_u flag)
4244{
4245 options[opt_idx].flags &= ~flag;
4246}
4247
4248/*
4249 * Returns TRUE if the option at 'opt_idx' is a global option
4250 */
4251 int
4252is_global_option(int opt_idx)
4253{
4254 return options[opt_idx].indir == PV_NONE;
4255}
4256
4257/*
4258 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4259 * local value.
4260 */
4261 int
4262is_global_local_option(int opt_idx)
4263{
4264 return options[opt_idx].indir & PV_BOTH;
4265}
4266
4267/*
4268 * Returns TRUE if the option at 'opt_idx' is a window-local option
4269 */
4270 int
4271is_window_local_option(int opt_idx)
4272{
4273 return options[opt_idx].var == VAR_WIN;
4274}
4275
4276/*
4277 * Returns TRUE if the option at 'opt_idx' is a hidden option
4278 */
4279 int
4280is_hidden_option(int opt_idx)
4281{
4282 return options[opt_idx].var == NULL;
4283}
4284
4285#if defined(FEAT_CRYPT) || defined(PROTO)
4286/*
4287 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4288 */
4289 int
4290is_crypt_key_option(int opt_idx)
4291{
4292 return options[opt_idx].indir == PV_KEY;
4293}
4294#endif
4295
4296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 * Set the value of option "name".
4298 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004299 *
4300 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004302 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004303set_option_value(
4304 char_u *name,
4305 long number,
4306 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004307 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308{
4309 int opt_idx;
4310 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004311 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
4313 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004314 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004315 {
4316 int key;
4317
4318 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004319 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004320 {
4321 char_u key_name[2];
4322
4323 if (key < 0)
4324 {
4325 key_name[0] = KEY2TERMCAP0(key);
4326 key_name[1] = KEY2TERMCAP1(key);
4327 }
4328 else
4329 {
4330 key_name[0] = KS_KEY;
4331 key_name[1] = (key & 0xff);
4332 }
4333 add_termcode(key_name, string, FALSE);
4334 if (full_screen)
4335 ttest(FALSE);
4336 redraw_all_later(CLEAR);
4337 return NULL;
4338 }
4339
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004340 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 else
4343 {
4344 flags = options[opt_idx].flags;
4345#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004346 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004348 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004349 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004350 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004353 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004354 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 else
4356 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004357 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004358 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004360 if (number == 0 && string != NULL)
4361 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004362 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004363
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004364 // Either we are given a string or we are setting option
4365 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004366 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004367 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004368 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004369 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004370 // There's another character after zeros or the string
4371 // is empty. In both cases, we are trying to set a
4372 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004373 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004374 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004375 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004376
4377 }
4378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004380 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004381 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004383 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004384 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
4386 }
4387 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004388 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389}
4390
4391/*
4392 * Get the terminal code for a terminal option.
4393 * Returns NULL when not found.
4394 */
4395 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004396get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397{
4398 int opt_idx;
4399 char_u *varp;
4400
4401 if (tname[0] != 't' || tname[1] != '_' ||
4402 tname[2] == NUL || tname[3] == NUL)
4403 return NULL;
4404 if ((opt_idx = findoption(tname)) >= 0)
4405 {
4406 varp = get_varp(&(options[opt_idx]));
4407 if (varp != NULL)
4408 varp = *(char_u **)(varp);
4409 return varp;
4410 }
4411 return find_termcode(tname + 2);
4412}
4413
4414 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004415get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416{
4417 int i;
4418
4419 i = findoption((char_u *)"hl");
4420 if (i >= 0)
4421 return options[i].def_val[VI_DEFAULT];
4422 return (char_u *)NULL;
4423}
4424
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004425 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004426get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004427{
4428 int i;
4429
4430 i = findoption((char_u *)"enc");
4431 if (i >= 0)
4432 return options[i].def_val[VI_DEFAULT];
4433 return (char_u *)NULL;
4434}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004435
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436/*
4437 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004438 * When "has_lt" is true there is a '<' before "*arg_arg".
4439 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 */
4441 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004442find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004444 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004446 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448 /*
4449 * Don't use get_special_key_code() for t_xx, we don't want it to call
4450 * add_termcap_entry().
4451 */
4452 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4453 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004454 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004456 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004458 key = find_special_key(&arg, &modifiers,
4459 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004460 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 key = 0;
4462 }
4463 return key;
4464}
4465
4466/*
4467 * if 'all' == 0: show changed options
4468 * if 'all' == 1: show all normal options
4469 * if 'all' == 2: show all terminal options
4470 */
4471 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004472showoptions(
4473 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004474 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475{
4476 struct vimoption *p;
4477 int col;
4478 int isterm;
4479 char_u *varp;
4480 struct vimoption **items;
4481 int item_count;
4482 int run;
4483 int row, rows;
4484 int cols;
4485 int i;
4486 int len;
4487
4488#define INC 20
4489#define GAP 3
4490
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004491 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if (items == NULL)
4493 return;
4494
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004495 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004497 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004499 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004501 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004503 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004506 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 * 1. display the short items
4508 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004509 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 */
4511 for (run = 1; run <= 2 && !got_int; ++run)
4512 {
4513 /*
4514 * collect the items in items[]
4515 */
4516 item_count = 0;
4517 for (p = &options[0]; p->fullname != NULL; p++)
4518 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004519 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004520 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004521 continue;
4522
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 varp = NULL;
4524 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004525 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 {
4527 if (p->indir != PV_NONE && !isterm)
4528 varp = get_varp_scope(p, opt_flags);
4529 }
4530 else
4531 varp = get_varp(p);
4532 if (varp != NULL
4533 && ((all == 2 && isterm)
4534 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004535 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004537 if (opt_flags & OPT_ONECOLUMN)
4538 len = Columns;
4539 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004540 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 else
4542 {
4543 option_value2string(p, opt_flags);
4544 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4545 }
4546 if ((len <= INC - GAP && run == 1) ||
4547 (len > INC - GAP && run == 2))
4548 items[item_count++] = p;
4549 }
4550 }
4551
4552 /*
4553 * display the items
4554 */
4555 if (run == 1)
4556 {
4557 cols = (Columns + GAP - 3) / INC;
4558 if (cols == 0)
4559 cols = 1;
4560 rows = (item_count + cols - 1) / cols;
4561 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004562 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 rows = item_count;
4564 for (row = 0; row < rows && !got_int; ++row)
4565 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004566 msg_putchar('\n'); // go to next line
4567 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 break;
4569 col = 0;
4570 for (i = row; i < item_count; i += rows)
4571 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004572 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 showoneopt(items[i], opt_flags);
4574 col += INC;
4575 }
4576 out_flush();
4577 ui_breakcheck();
4578 }
4579 }
4580 vim_free(items);
4581}
4582
4583/*
4584 * Return TRUE if option "p" has its default value.
4585 */
4586 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004587optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588{
4589 int dvi;
4590
4591 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004592 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004593 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004595 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004597 // the cast to long is required for Manx C, long_i is
4598 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004599 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004600 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4602}
4603
4604/*
4605 * showoneopt: show the value of one option
4606 * must not be called with a hidden option!
4607 */
4608 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004609showoneopt(
4610 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004611 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004613 char_u *varp;
4614 int save_silent = silent_mode;
4615
4616 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004617 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
4619 varp = get_varp_scope(p, opt_flags);
4620
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004621 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4623 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004624 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004626 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004628 msg_puts(" ");
4629 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (!(p->flags & P_BOOL))
4631 {
4632 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004633 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 option_value2string(p, opt_flags);
4635 msg_outtrans(NameBuff);
4636 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004637
4638 silent_mode = save_silent;
4639 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640}
4641
4642/*
4643 * Write modified options as ":set" commands to a file.
4644 *
4645 * There are three values for "opt_flags":
4646 * OPT_GLOBAL: Write global option values and fresh values of
4647 * buffer-local options (used for start of a session
4648 * file).
4649 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4650 * curwin (used for a vimrc file).
4651 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4652 * and local values for window-local options of
4653 * curwin. Local values are also written when at the
4654 * default value, because a modeline or autocommand
4655 * may have set them when doing ":edit file" and the
4656 * user has set them back at the default or fresh
4657 * value.
4658 * When "local_only" is TRUE, don't write fresh
4659 * values, only local values (for ":mkview").
4660 * (fresh value = value used for a new buffer or window for a local option).
4661 *
4662 * Return FAIL on error, OK otherwise.
4663 */
4664 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004665makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666{
4667 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004668 char_u *varp; // currently used value
4669 char_u *varp_fresh; // local value
4670 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 char *cmd;
4672 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004673 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674
4675 /*
4676 * The options that don't have a default (terminal name, columns, lines)
4677 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004678 * Do the loop over "options[]" twice: once for options with the
4679 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004681 for (pri = 1; pri >= 0; --pri)
4682 {
4683 for (p = &options[0]; !istermoption(p); p++)
4684 if (!(p->flags & P_NO_MKRC)
4685 && !istermoption(p)
4686 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004688 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4690 continue;
4691
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004692 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4693 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4695 continue;
4696
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004697 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004699 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 continue;
4701
Bram Moolenaard23b7142021-04-17 21:04:34 +02004702 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4703 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004704 continue;
4705
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 round = 2;
4707 if (p->indir != PV_NONE)
4708 {
4709 if (p->var == VAR_WIN)
4710 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004711 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 if (!(opt_flags & OPT_LOCAL))
4713 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004714 // When fresh value of window-local option is not at the
4715 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4717 {
4718 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004719 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
4721 round = 1;
4722 varp_local = varp;
4723 varp = varp_fresh;
4724 }
4725 }
4726 }
4727 }
4728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004729 // Round 1: fresh value for window-local options.
4730 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 for ( ; round <= 2; varp = varp_local, ++round)
4732 {
4733 if (round == 1 || (opt_flags & OPT_GLOBAL))
4734 cmd = "set";
4735 else
4736 cmd = "setlocal";
4737
4738 if (p->flags & P_BOOL)
4739 {
4740 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4741 return FAIL;
4742 }
4743 else if (p->flags & P_NUM)
4744 {
4745 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4746 return FAIL;
4747 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004748 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004750 int do_endif = FALSE;
4751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004752 // Don't set 'syntax' and 'filetype' again if the value is
4753 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004754 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004755#if defined(FEAT_SYN_HL)
4756 p->indir == PV_SYN ||
4757#endif
4758 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
4760 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4761 *(char_u **)(varp)) < 0
4762 || put_eol(fd) < 0)
4763 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004764 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
4766 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004767 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004769 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
4771 if (put_line(fd, "endif") == FAIL)
4772 return FAIL;
4773 }
4774 }
4775 }
4776 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 return OK;
4779}
4780
4781#if defined(FEAT_FOLDING) || defined(PROTO)
4782/*
4783 * Generate set commands for the local fold options only. Used when
4784 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4785 */
4786 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004787makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004789 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004791 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 == FAIL
4793# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004794 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004796 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 == FAIL
4798 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4799 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4800 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4801 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4802 )
4803 return FAIL;
4804
4805 return OK;
4806}
4807#endif
4808
4809 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004810put_setstring(
4811 FILE *fd,
4812 char *cmd,
4813 char *name,
4814 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004815 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816{
4817 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004818 char_u *buf = NULL;
4819 char_u *part = NULL;
4820 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821
4822 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4823 return FAIL;
4824 if (*valuep != NULL)
4825 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004826 // Output 'pastetoggle' as key names. For other
4827 // options some characters have to be escaped with
4828 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 if (valuep == &p_pt)
4830 {
4831 s = *valuep;
4832 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004833 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 return FAIL;
4835 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004836 // expand the option value, replace $HOME by ~
4837 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004839 int size = (int)STRLEN(*valuep) + 1;
4840
4841 // replace home directory in the whole option value into "buf"
4842 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004843 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004844 goto fail;
4845 home_replace(NULL, *valuep, buf, size, FALSE);
4846
4847 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004848 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004849 // can be expanded when read back.
4850 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4851 && vim_strchr(*valuep, ',') != NULL)
4852 {
4853 part = alloc(size);
4854 if (part == NULL)
4855 goto fail;
4856
4857 // write line break to clear the option, e.g. ':set rtp='
4858 if (put_eol(fd) == FAIL)
4859 goto fail;
4860
4861 p = buf;
4862 while (*p != NUL)
4863 {
4864 // for each comma separated option part, append value to
4865 // the option, :set rtp+=value
4866 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4867 goto fail;
4868 (void)copy_option_part(&p, part, size, ",");
4869 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4870 goto fail;
4871 }
4872 vim_free(buf);
4873 vim_free(part);
4874 return OK;
4875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004877 {
4878 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004880 }
4881 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883 else if (put_escstr(fd, *valuep, 2) == FAIL)
4884 return FAIL;
4885 }
4886 if (put_eol(fd) < 0)
4887 return FAIL;
4888 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004889fail:
4890 vim_free(buf);
4891 vim_free(part);
4892 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893}
4894
4895 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004896put_setnum(
4897 FILE *fd,
4898 char *cmd,
4899 char *name,
4900 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901{
4902 long wc;
4903
4904 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4905 return FAIL;
4906 if (wc_use_keyname((char_u *)valuep, &wc))
4907 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004908 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4910 return FAIL;
4911 }
4912 else if (fprintf(fd, "%ld", *valuep) < 0)
4913 return FAIL;
4914 if (put_eol(fd) < 0)
4915 return FAIL;
4916 return OK;
4917}
4918
4919 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004920put_setbool(
4921 FILE *fd,
4922 char *cmd,
4923 char *name,
4924 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004926 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004927 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4929 || put_eol(fd) < 0)
4930 return FAIL;
4931 return OK;
4932}
4933
4934/*
4935 * Clear all the terminal options.
4936 * If the option has been allocated, free the memory.
4937 * Terminal options are never hidden or indirect.
4938 */
4939 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004940clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 /*
4943 * Reset a few things before clearing the old options. This may cause
4944 * outputting a few things that the terminal doesn't understand, but the
4945 * screen will be cleared later, so this is OK.
4946 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004947 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004949 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950#endif
4951#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004952 // When starting the GUI close the display opened for the clipboard.
4953 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (gui.starting)
4955 clear_xterm_clip();
4956#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004957 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004959 free_termoptions();
4960}
4961
4962 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004963free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004964{
4965 struct vimoption *p;
4966
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004967 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if (istermoption(p))
4969 {
4970 if (p->flags & P_ALLOCED)
4971 free_string_option(*(char_u **)(p->var));
4972 if (p->flags & P_DEF_ALLOCED)
4973 free_string_option(p->def_val[VI_DEFAULT]);
4974 *(char_u **)(p->var) = empty_option;
4975 p->def_val[VI_DEFAULT] = empty_option;
4976 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004977#ifdef FEAT_EVAL
4978 // remember where the option was cleared
4979 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982 clear_termcodes();
4983}
4984
4985/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004986 * Free the string for one term option, if it was allocated.
4987 * Set the string to empty_option and clear allocated flag.
4988 * "var" points to the option value.
4989 */
4990 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004991free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004992{
4993 struct vimoption *p;
4994
4995 for (p = &options[0]; p->fullname != NULL; p++)
4996 if (p->var == var)
4997 {
4998 if (p->flags & P_ALLOCED)
4999 free_string_option(*(char_u **)(p->var));
5000 *(char_u **)(p->var) = empty_option;
5001 p->flags &= ~P_ALLOCED;
5002 break;
5003 }
5004}
5005
5006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 * Set the terminal option defaults to the current value.
5008 * Used after setting the terminal name.
5009 */
5010 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005011set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
5013 struct vimoption *p;
5014
5015 for (p = &options[0]; p->fullname != NULL; p++)
5016 {
5017 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5018 {
5019 if (p->flags & P_DEF_ALLOCED)
5020 {
5021 free_string_option(p->def_val[VI_DEFAULT]);
5022 p->flags &= ~P_DEF_ALLOCED;
5023 }
5024 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5025 if (p->flags & P_ALLOCED)
5026 {
5027 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005028 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
5030 }
5031 }
5032}
5033
5034/*
5035 * return TRUE if 'p' starts with 't_'
5036 */
5037 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005038istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039{
5040 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5041}
5042
Bram Moolenaardac13472019-09-16 21:06:21 +02005043/*
5044 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5045 */
5046 int
5047istermoption_idx(int opt_idx)
5048{
5049 return istermoption(&options[opt_idx]);
5050}
5051
Bram Moolenaar113e1072019-01-20 15:30:40 +01005052#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005054 * Unset local option value, similar to ":set opt<".
5055 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005056 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005057unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005058{
5059 struct vimoption *p;
5060 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005061 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005062
5063 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005064 if (opt_idx < 0)
5065 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005066 p = &(options[opt_idx]);
5067
5068 switch ((int)p->indir)
5069 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005070 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005071 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005072 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005073 break;
5074 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005075 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005076 break;
5077 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005078 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005079 break;
5080 case PV_AR:
5081 buf->b_p_ar = -1;
5082 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005083 case PV_BKC:
5084 clear_string_option(&buf->b_p_bkc);
5085 buf->b_bkc_flags = 0;
5086 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005087 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005088 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005089 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005090 case PV_TC:
5091 clear_string_option(&buf->b_p_tc);
5092 buf->b_tc_flags = 0;
5093 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005094 case PV_SISO:
5095 curwin->w_p_siso = -1;
5096 break;
5097 case PV_SO:
5098 curwin->w_p_so = -1;
5099 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005100#ifdef FEAT_FIND_ID
5101 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005102 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005103 break;
5104 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005105 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005106 break;
5107#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005108 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005109 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005110 break;
5111 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005112 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005113 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005114 case PV_FP:
5115 clear_string_option(&buf->b_p_fp);
5116 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005117#ifdef FEAT_QUICKFIX
5118 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005119 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005120 break;
5121 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005122 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005123 break;
5124 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005125 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005126 break;
5127#endif
5128#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5129 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005130 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005131 break;
5132#endif
5133#if defined(FEAT_CRYPT)
5134 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005135 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005136 break;
5137#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005138#ifdef FEAT_LINEBREAK
5139 case PV_SBR:
5140 clear_string_option(&((win_T *)from)->w_p_sbr);
5141 break;
5142#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005143#ifdef FEAT_STL_OPT
5144 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005145 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005146 break;
5147#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005148 case PV_UL:
5149 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5150 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005151#ifdef FEAT_LISP
5152 case PV_LW:
5153 clear_string_option(&buf->b_p_lw);
5154 break;
5155#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005156 case PV_MENC:
5157 clear_string_option(&buf->b_p_menc);
5158 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005159 case PV_LCS:
5160 clear_string_option(&((win_T *)from)->w_p_lcs);
5161 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5162 redraw_later(NOT_VALID);
5163 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005164 }
5165}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005166#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005167
5168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 * Get pointer to option variable, depending on local or global scope.
5170 */
5171 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005172get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173{
5174 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5175 {
5176 if (p->var == VAR_WIN)
5177 return (char_u *)GLOBAL_WO(get_varp(p));
5178 return p->var;
5179 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005180 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
5182 switch ((int)p->indir)
5183 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005184 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005186 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5187 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5188 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005190 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5191 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5192 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005193 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005194 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005195 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005196 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5197 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005199 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5200 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005202 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5203 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005204#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5205 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5206#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005207#if defined(FEAT_CRYPT)
5208 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5209#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005210#ifdef FEAT_LINEBREAK
5211 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5212#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005213#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005214 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005215#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005216 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005217#ifdef FEAT_LISP
5218 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5219#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005220 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005221 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005222 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
5223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005225 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227 return get_varp(p);
5228}
5229
5230/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005231 * Get pointer to option variable at 'opt_idx', depending on local or global
5232 * scope.
5233 */
5234 char_u *
5235get_option_varp_scope(int opt_idx, int opt_flags)
5236{
5237 return get_varp_scope(&(options[opt_idx]), opt_flags);
5238}
5239
5240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 * Get pointer to option variable.
5242 */
5243 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005244get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005246 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 if (p->var == NULL)
5248 return NULL;
5249
5250 switch ((int)p->indir)
5251 {
5252 case PV_NONE: return p->var;
5253
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005254 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005255 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005257 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005259 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005261 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005263 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005265 case PV_TC: return *curbuf->b_p_tc != NUL
5266 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005267 case PV_BKC: return *curbuf->b_p_bkc != NUL
5268 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005269 case PV_SISO: return curwin->w_p_siso >= 0
5270 ? (char_u *)&(curwin->w_p_siso) : p->var;
5271 case PV_SO: return curwin->w_p_so >= 0
5272 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005274 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005276 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5278#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005279 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005281 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005283 case PV_FP: return *curbuf->b_p_fp != NUL
5284 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005286 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005288 case PV_GP: return *curbuf->b_p_gp != NUL
5289 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5290 case PV_MP: return *curbuf->b_p_mp != NUL
5291 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005293#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5294 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5295 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5296#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005297#if defined(FEAT_CRYPT)
5298 case PV_CM: return *curbuf->b_p_cm != NUL
5299 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5300#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005301#ifdef FEAT_LINEBREAK
5302 case PV_SBR: return *curwin->w_p_sbr != NUL
5303 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5304#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005305#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005306 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005307 ? (char_u *)&(curwin->w_p_stl) : p->var;
5308#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005309 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5310 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005311#ifdef FEAT_LISP
5312 case PV_LW: return *curbuf->b_p_lw != NUL
5313 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5314#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005315 case PV_MENC: return *curbuf->b_p_menc != NUL
5316 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317#ifdef FEAT_ARABIC
5318 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5319#endif
5320 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005321 case PV_LCS: return *curwin->w_p_lcs != NUL
5322 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005323#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005324 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5325#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005326#ifdef FEAT_SYN_HL
5327 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5328 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005329 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005330 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332#ifdef FEAT_DIFF
5333 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5334#endif
5335#ifdef FEAT_FOLDING
5336 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5337 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5338 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5339 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5340 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5341 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5342 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5343# ifdef FEAT_EVAL
5344 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5345 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5346# endif
5347 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5348#endif
5349 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005350 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005351#ifdef FEAT_LINEBREAK
5352 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005355 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005356#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5358#endif
5359#ifdef FEAT_RIGHTLEFT
5360 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5361 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5362#endif
5363 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5364 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5365#ifdef FEAT_LINEBREAK
5366 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005367 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5368 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005370 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005372 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005373#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005374 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5375 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5376#endif
5377#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005378 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5379 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5380 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5384 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5387 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5389 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5390#ifdef FEAT_CINDENT
5391 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5392 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5393 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5394#endif
5395#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5396 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399#ifdef FEAT_FOLDING
5400 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005403#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005404 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005406#ifdef FEAT_COMPL_FUNC
5407 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005408 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005409#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005410#ifdef FEAT_EVAL
5411 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005414 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005420 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5422 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5423 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5424 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5425#ifdef FEAT_FIND_ID
5426# ifdef FEAT_EVAL
5427 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5428# endif
5429#endif
5430#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5431 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5432 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5433#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005434#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005435 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437#ifdef FEAT_CRYPT
5438 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5439#endif
5440#ifdef FEAT_LISP
5441 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5442#endif
5443 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5444 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5445 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5446 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5447 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005449#ifdef FEAT_TEXTOBJ
5450 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5453#ifdef FEAT_SMARTINDENT
5454 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5458#ifdef FEAT_SEARCHPATH
5459 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5460#endif
5461 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5462#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005463 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005464 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5465#endif
5466#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005467 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5468 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5469 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005470 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471#endif
5472 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5473 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5474 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5475 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005476#ifdef FEAT_PERSISTENT_UNDO
5477 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5480#ifdef FEAT_KEYMAP
5481 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5482#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005483#ifdef FEAT_SIGNS
5484 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5485#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005486#ifdef FEAT_VARTABS
5487 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5488 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5489#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005490 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005492 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 return (char_u *)&(curbuf->b_p_wm);
5494}
5495
5496/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005497 * Return a pointer to the variable for option at 'opt_idx'
5498 */
5499 char_u *
5500get_option_var(int opt_idx)
5501{
5502 return options[opt_idx].var;
5503}
5504
5505/*
5506 * Return the full name of the option at 'opt_idx'
5507 */
5508 char_u *
5509get_option_fullname(int opt_idx)
5510{
5511 return (char_u *)options[opt_idx].fullname;
5512}
5513
5514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 * Get the value of 'equalprg', either the buffer-local one or the global one.
5516 */
5517 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005518get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519{
5520 if (*curbuf->b_p_ep == NUL)
5521 return p_ep;
5522 return curbuf->b_p_ep;
5523}
5524
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525/*
5526 * Copy options from one window to another.
5527 * Used when splitting a window.
5528 */
5529 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005530win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
5532 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5533 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005534 after_copy_winopt(wp_to);
5535}
5536
5537/*
5538 * After copying window options: update variables depending on options.
5539 */
5540 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005541after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005542{
5543#ifdef FEAT_LINEBREAK
5544 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005545#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005546#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005547 fill_culopt_flags(NULL, wp);
5548 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005549#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005550 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
5553/*
5554 * Copy the options from one winopt_T to another.
5555 * Doesn't free the old option values in "to", use clear_winopt() for that.
5556 * The 'scroll' option is not copied, because it depends on the window height.
5557 * The 'previewwindow' option is reset, there can be only one preview window.
5558 */
5559 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005560copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
5562#ifdef FEAT_ARABIC
5563 to->wo_arab = from->wo_arab;
5564#endif
5565 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005566 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005568 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005569#ifdef FEAT_LINEBREAK
5570 to->wo_nuw = from->wo_nuw;
5571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572#ifdef FEAT_RIGHTLEFT
5573 to->wo_rl = from->wo_rl;
5574 to->wo_rlc = vim_strsave(from->wo_rlc);
5575#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005576#ifdef FEAT_LINEBREAK
5577 to->wo_sbr = vim_strsave(from->wo_sbr);
5578#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005579#ifdef FEAT_STL_OPT
5580 to->wo_stl = vim_strsave(from->wo_stl);
5581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005583#ifdef FEAT_DIFF
5584 to->wo_wrap_save = from->wo_wrap_save;
5585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586#ifdef FEAT_LINEBREAK
5587 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005588 to->wo_bri = from->wo_bri;
5589 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005591 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005593 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005594 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005595 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005596#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005597 to->wo_spell = from->wo_spell;
5598#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005599#ifdef FEAT_SYN_HL
5600 to->wo_cuc = from->wo_cuc;
5601 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005602 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005603 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605#ifdef FEAT_DIFF
5606 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005607 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005609#ifdef FEAT_CONCEAL
5610 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005611 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005612#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005613#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005614 to->wo_twk = vim_strsave(from->wo_twk);
5615 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617#ifdef FEAT_FOLDING
5618 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005619 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005621 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 to->wo_fdi = vim_strsave(from->wo_fdi);
5623 to->wo_fml = from->wo_fml;
5624 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005625 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005627 to->wo_fdm_save = from->wo_diff_saved
5628 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 to->wo_fdn = from->wo_fdn;
5630# ifdef FEAT_EVAL
5631 to->wo_fde = vim_strsave(from->wo_fde);
5632 to->wo_fdt = vim_strsave(from->wo_fdt);
5633# endif
5634 to->wo_fmr = vim_strsave(from->wo_fmr);
5635#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005636#ifdef FEAT_SIGNS
5637 to->wo_scl = vim_strsave(from->wo_scl);
5638#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005639
5640#ifdef FEAT_EVAL
5641 // Copy the script context so that we know where the value was last set.
5642 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5643 sizeof(to->wo_script_ctx));
5644#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005645 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646}
5647
5648/*
5649 * Check string options in a window for a NULL value.
5650 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005651 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005652check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 check_winopt(&win->w_onebuf_opt);
5655 check_winopt(&win->w_allbuf_opt);
5656}
5657
5658/*
5659 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5660 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005661 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005662check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
5664#ifdef FEAT_FOLDING
5665 check_string_option(&wop->wo_fdi);
5666 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005667 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668# ifdef FEAT_EVAL
5669 check_string_option(&wop->wo_fde);
5670 check_string_option(&wop->wo_fdt);
5671# endif
5672 check_string_option(&wop->wo_fmr);
5673#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005674#ifdef FEAT_SIGNS
5675 check_string_option(&wop->wo_scl);
5676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677#ifdef FEAT_RIGHTLEFT
5678 check_string_option(&wop->wo_rlc);
5679#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005680#ifdef FEAT_LINEBREAK
5681 check_string_option(&wop->wo_sbr);
5682#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005683#ifdef FEAT_STL_OPT
5684 check_string_option(&wop->wo_stl);
5685#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005686#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005687 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005688 check_string_option(&wop->wo_cc);
5689#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005690#ifdef FEAT_CONCEAL
5691 check_string_option(&wop->wo_cocu);
5692#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005693#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005694 check_string_option(&wop->wo_twk);
5695 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005696#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005697#ifdef FEAT_LINEBREAK
5698 check_string_option(&wop->wo_briopt);
5699#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005700 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005701 check_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702}
5703
5704/*
5705 * Free the allocated memory inside a winopt_T.
5706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005708clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
5710#ifdef FEAT_FOLDING
5711 clear_string_option(&wop->wo_fdi);
5712 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005713 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714# ifdef FEAT_EVAL
5715 clear_string_option(&wop->wo_fde);
5716 clear_string_option(&wop->wo_fdt);
5717# endif
5718 clear_string_option(&wop->wo_fmr);
5719#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005720#ifdef FEAT_SIGNS
5721 clear_string_option(&wop->wo_scl);
5722#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005723#ifdef FEAT_LINEBREAK
5724 clear_string_option(&wop->wo_briopt);
5725#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005726 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727#ifdef FEAT_RIGHTLEFT
5728 clear_string_option(&wop->wo_rlc);
5729#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005730#ifdef FEAT_LINEBREAK
5731 clear_string_option(&wop->wo_sbr);
5732#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005733#ifdef FEAT_STL_OPT
5734 clear_string_option(&wop->wo_stl);
5735#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005736#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005737 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005738 clear_string_option(&wop->wo_cc);
5739#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005740#ifdef FEAT_CONCEAL
5741 clear_string_option(&wop->wo_cocu);
5742#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005743#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005744 clear_string_option(&wop->wo_twk);
5745 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005746#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005747 clear_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748}
5749
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005750#ifdef FEAT_EVAL
5751// Index into the options table for a buffer-local option enum.
5752static int buf_opt_idx[BV_COUNT];
5753# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5754
5755/*
5756 * Initialize buf_opt_idx[] if not done already.
5757 */
5758 static void
5759init_buf_opt_idx(void)
5760{
5761 static int did_init_buf_opt_idx = FALSE;
5762 int i;
5763
5764 if (did_init_buf_opt_idx)
5765 return;
5766 did_init_buf_opt_idx = TRUE;
5767 for (i = 0; !istermoption_idx(i); i++)
5768 if (options[i].indir & PV_BUF)
5769 buf_opt_idx[options[i].indir & PV_MASK] = i;
5770}
5771#else
5772# define COPY_OPT_SCTX(buf, bv)
5773#endif
5774
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775/*
5776 * Copy global option values to local options for one buffer.
5777 * Used when creating a new buffer and sometimes when entering a buffer.
5778 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005779 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5781 * appropriate.
5782 * BCO_NOHELP Don't copy the values to a help buffer.
5783 */
5784 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005785buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
5787 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005788 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 int dont_do_help;
5790 int did_isk = FALSE;
5791
5792 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 * Skip this when the option defaults have not been set yet. Happens when
5794 * main() allocates the first buffer.
5795 */
5796 if (p_cpo != NULL)
5797 {
5798 /*
5799 * Always copy when entering and 'cpo' contains 'S'.
5800 * Don't copy when already initialized.
5801 * Don't copy when 'cpo' contains 's' and not entering.
5802 * 'S' BCO_ENTER initialized 's' should_copy
5803 * yes yes X X TRUE
5804 * yes no yes X FALSE
5805 * no X yes X FALSE
5806 * X no no yes FALSE
5807 * X no no no TRUE
5808 * no yes no X TRUE
5809 */
5810 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5811 && (buf->b_p_initialized
5812 || (!(flags & BCO_ENTER)
5813 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5814 should_copy = FALSE;
5815
5816 if (should_copy || (flags & BCO_ALWAYS))
5817 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005818#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005819 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005820 init_buf_opt_idx();
5821#endif
5822 // Don't copy the options specific to a help buffer when
5823 // BCO_NOHELP is given or the options were initialized already
5824 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5826 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005827 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 {
5829 save_p_isk = buf->b_p_isk;
5830 buf->b_p_isk = NULL;
5831 }
5832 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005833 * Always free the allocated strings. If not already initialized,
5834 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 */
5836 if (!buf->b_p_initialized)
5837 {
5838 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005839 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005842 switch (*p_ffs)
5843 {
5844 case 'm':
5845 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5846 case 'd':
5847 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5848 case 'u':
5849 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5850 default:
5851 buf->b_p_ff = vim_strsave(p_ff);
5852 }
5853 if (buf->b_p_ff != NULL)
5854 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 buf->b_p_bh = empty_option;
5856 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 }
5858 else
5859 free_buf_options(buf, FALSE);
5860
5861 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005862 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 buf->b_p_ai_nopaste = p_ai_nopaste;
5864 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005865 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005867 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 buf->b_p_tw_nopaste = p_tw_nopaste;
5869 buf->b_p_tw_nobin = p_tw_nobin;
5870 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005871 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 buf->b_p_wm_nopaste = p_wm_nopaste;
5873 buf->b_p_wm_nobin = p_wm_nobin;
5874 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005875 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005876 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005877 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005878 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005879 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005881 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005883 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005885 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 buf->b_p_ml_nobin = p_ml_nobin;
5887 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005888 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005889 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005890 buf->b_p_swf = FALSE;
5891 else
5892 {
5893 buf->b_p_swf = p_swf;
5894 COPY_OPT_SCTX(buf, BV_INF);
5895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005897 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005898#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005899 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005900 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005902#ifdef FEAT_COMPL_FUNC
5903 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005904 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005905 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005906 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005907#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005908#ifdef FEAT_EVAL
5909 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005910 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005913 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005915#ifdef FEAT_VARTABS
5916 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005917 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005918 if (p_vsts && p_vsts != empty_option)
5919 tabstop_set(p_vsts, &buf->b_p_vsts_array);
5920 else
5921 buf->b_p_vsts_array = 0;
5922 buf->b_p_vsts_nopaste = p_vsts_nopaste
5923 ? vim_strsave(p_vsts_nopaste) : NULL;
5924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005926 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005928 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929#ifdef FEAT_FOLDING
5930 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005931 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932#endif
5933 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005934 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005935 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005936 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005937 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5938 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005940 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005942 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943#ifdef FEAT_SMARTINDENT
5944 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005945 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946#endif
5947 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005948 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949#ifdef FEAT_CINDENT
5950 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005951 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005953 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005955 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005957 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005960 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5962 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005963 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964#endif
5965#ifdef FEAT_LISP
5966 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005967 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968#endif
5969#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005970 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005972 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005973 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005974 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005975#endif
5976#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02005977 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005978 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005979 (void)compile_cap_prog(&buf->b_s);
5980 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005981 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005982 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005983 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005984 buf->b_s.b_p_spo = vim_strsave(p_spo);
5985 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986#endif
5987#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5988 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005989 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005991 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005993 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005994#if defined(FEAT_EVAL)
5995 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005996 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998#ifdef FEAT_CRYPT
5999 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006000 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001#endif
6002#ifdef FEAT_SEARCHPATH
6003 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006004 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005#endif
6006#ifdef FEAT_KEYMAP
6007 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006008 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 buf->b_kmap_state |= KEYMAP_INIT;
6010#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006011#ifdef FEAT_TERMINAL
6012 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006013 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006014#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006015 // This isn't really an option, but copying the langmap and IME
6016 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006018 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006020 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006022 // options that are normally global but also have a local value
6023 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006025 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006026 buf->b_p_bkc = empty_option;
6027 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028#ifdef FEAT_QUICKFIX
6029 buf->b_p_gp = empty_option;
6030 buf->b_p_mp = empty_option;
6031 buf->b_p_efm = empty_option;
6032#endif
6033 buf->b_p_ep = empty_option;
6034 buf->b_p_kp = empty_option;
6035 buf->b_p_path = empty_option;
6036 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006037 buf->b_p_tc = empty_option;
6038 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039#ifdef FEAT_FIND_ID
6040 buf->b_p_def = empty_option;
6041 buf->b_p_inc = empty_option;
6042# ifdef FEAT_EVAL
6043 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006044 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045# endif
6046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 buf->b_p_dict = empty_option;
6048 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006049#ifdef FEAT_TEXTOBJ
6050 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006051 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006052#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006053#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6054 buf->b_p_bexpr = empty_option;
6055#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006056#if defined(FEAT_CRYPT)
6057 buf->b_p_cm = empty_option;
6058#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006059#ifdef FEAT_PERSISTENT_UNDO
6060 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006061 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006062#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006063#ifdef FEAT_LISP
6064 buf->b_p_lw = empty_option;
6065#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006066 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067
6068 /*
6069 * Don't copy the options set by ex_help(), use the saved values,
6070 * when going from a help buffer to a non-help buffer.
6071 * Don't touch these at all when BCO_NOHELP is used and going from
6072 * or to a help buffer.
6073 */
6074 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006075 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006077#ifdef FEAT_VARTABS
6078 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
6079 tabstop_set(p_vts, &buf->b_p_vts_array);
6080 else
6081 buf->b_p_vts_array = NULL;
6082#endif
6083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 else
6085 {
6086 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006087 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 did_isk = TRUE;
6089 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006090#ifdef FEAT_VARTABS
6091 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006092 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006093 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
6094 tabstop_set(p_vts, &buf->b_p_vts_array);
6095 else
6096 buf->b_p_vts_array = NULL;
6097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if (buf->b_p_bt[0] == 'h')
6100 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006102 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 }
6104 }
6105
6106 /*
6107 * When the options should be copied (ignoring BCO_ALWAYS), set the
6108 * flag that indicates that the options have been initialized.
6109 */
6110 if (should_copy)
6111 buf->b_p_initialized = TRUE;
6112 }
6113
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006114 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 if (did_isk)
6116 (void)buf_init_chartab(buf, FALSE);
6117}
6118
6119/*
6120 * Reset the 'modifiable' option and its default value.
6121 */
6122 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006123reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 int opt_idx;
6126
6127 curbuf->b_p_ma = FALSE;
6128 p_ma = FALSE;
6129 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006130 if (opt_idx >= 0)
6131 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132}
6133
6134/*
6135 * Set the global value for 'iminsert' to the local value.
6136 */
6137 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006138set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
6140 p_iminsert = curbuf->b_p_iminsert;
6141}
6142
6143/*
6144 * Set the global value for 'imsearch' to the local value.
6145 */
6146 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006147set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 p_imsearch = curbuf->b_p_imsearch;
6150}
6151
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152static int expand_option_idx = -1;
6153static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6154static int expand_option_flags = 0;
6155
6156 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006157set_context_in_set_cmd(
6158 expand_T *xp,
6159 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006160 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161{
6162 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006163 long_u flags = 0; // init for GCC
6164 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 char_u *p;
6166 char_u *s;
6167 int is_term_option = FALSE;
6168 int key;
6169
6170 expand_option_flags = opt_flags;
6171
6172 xp->xp_context = EXPAND_SETTINGS;
6173 if (*arg == NUL)
6174 {
6175 xp->xp_pattern = arg;
6176 return;
6177 }
6178 p = arg + STRLEN(arg) - 1;
6179 if (*p == ' ' && *(p - 1) != '\\')
6180 {
6181 xp->xp_pattern = p + 1;
6182 return;
6183 }
6184 while (p > arg)
6185 {
6186 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006187 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 if (*p == ' ' || *p == ',')
6189 {
6190 while (s > arg && *(s - 1) == '\\')
6191 --s;
6192 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006193 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 if (*p == ' ' && ((p - s) & 1) == 0)
6195 {
6196 ++p;
6197 break;
6198 }
6199 --p;
6200 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006201 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 {
6203 xp->xp_context = EXPAND_BOOL_SETTINGS;
6204 p += 2;
6205 }
6206 if (STRNCMP(p, "inv", 3) == 0)
6207 {
6208 xp->xp_context = EXPAND_BOOL_SETTINGS;
6209 p += 3;
6210 }
6211 xp->xp_pattern = arg = p;
6212 if (*arg == '<')
6213 {
6214 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006215 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 return;
6217 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006218 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 {
6220 xp->xp_context = EXPAND_NOTHING;
6221 return;
6222 }
6223 nextchar = *++p;
6224 is_term_option = TRUE;
6225 expand_option_name[2] = KEY2TERMCAP0(key);
6226 expand_option_name[3] = KEY2TERMCAP1(key);
6227 }
6228 else
6229 {
6230 if (p[0] == 't' && p[1] == '_')
6231 {
6232 p += 2;
6233 if (*p != NUL)
6234 ++p;
6235 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006236 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 nextchar = *++p;
6238 is_term_option = TRUE;
6239 expand_option_name[2] = p[-2];
6240 expand_option_name[3] = p[-1];
6241 }
6242 else
6243 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006244 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6246 p++;
6247 if (*p == NUL)
6248 return;
6249 nextchar = *p;
6250 *p = NUL;
6251 opt_idx = findoption(arg);
6252 *p = nextchar;
6253 if (opt_idx == -1 || options[opt_idx].var == NULL)
6254 {
6255 xp->xp_context = EXPAND_NOTHING;
6256 return;
6257 }
6258 flags = options[opt_idx].flags;
6259 if (flags & P_BOOL)
6260 {
6261 xp->xp_context = EXPAND_NOTHING;
6262 return;
6263 }
6264 }
6265 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006266 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6268 {
6269 ++p;
6270 nextchar = '=';
6271 }
6272 if ((nextchar != '=' && nextchar != ':')
6273 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6274 {
6275 xp->xp_context = EXPAND_UNSUCCESSFUL;
6276 return;
6277 }
6278 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6279 {
6280 xp->xp_context = EXPAND_OLD_SETTING;
6281 if (is_term_option)
6282 expand_option_idx = -1;
6283 else
6284 expand_option_idx = opt_idx;
6285 xp->xp_pattern = p + 1;
6286 return;
6287 }
6288 xp->xp_context = EXPAND_NOTHING;
6289 if (is_term_option || (flags & P_NUM))
6290 return;
6291
6292 xp->xp_pattern = p + 1;
6293
6294 if (flags & P_EXPAND)
6295 {
6296 p = options[opt_idx].var;
6297 if (p == (char_u *)&p_bdir
6298 || p == (char_u *)&p_dir
6299 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006300 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 || p == (char_u *)&p_rtp
6302#ifdef FEAT_SEARCHPATH
6303 || p == (char_u *)&p_cdpath
6304#endif
6305#ifdef FEAT_SESSION
6306 || p == (char_u *)&p_vdir
6307#endif
6308 )
6309 {
6310 xp->xp_context = EXPAND_DIRECTORIES;
6311 if (p == (char_u *)&p_path
6312#ifdef FEAT_SEARCHPATH
6313 || p == (char_u *)&p_cdpath
6314#endif
6315 )
6316 xp->xp_backslash = XP_BS_THREE;
6317 else
6318 xp->xp_backslash = XP_BS_ONE;
6319 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006320 else if (p == (char_u *)&p_ft)
6321 {
6322 xp->xp_context = EXPAND_FILETYPE;
6323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 else
6325 {
6326 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006327 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 if (p == (char_u *)&p_tags)
6329 xp->xp_backslash = XP_BS_THREE;
6330 else
6331 xp->xp_backslash = XP_BS_ONE;
6332 }
6333 }
6334
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006335 // For an option that is a list of file names, find the start of the
6336 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6338 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006339 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 if (*p == ' ' || *p == ',')
6341 {
6342 s = p;
6343 while (s > xp->xp_pattern && *(s - 1) == '\\')
6344 --s;
6345 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6346 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6347 {
6348 xp->xp_pattern = p + 1;
6349 break;
6350 }
6351 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006352
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006353#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006354 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006355 if (options[opt_idx].var == (char_u *)&p_sps
6356 && STRNCMP(p, "file:", 5) == 0)
6357 {
6358 xp->xp_pattern = p + 5;
6359 break;
6360 }
6361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 }
6363
6364 return;
6365}
6366
6367 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006368ExpandSettings(
6369 expand_T *xp,
6370 regmatch_T *regmatch,
6371 int *num_file,
6372 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006374 int num_normal = 0; // Nr of matching non-term-code settings
6375 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 int opt_idx;
6377 int match;
6378 int count = 0;
6379 char_u *str;
6380 int loop;
6381 int is_term_opt;
6382 char_u name_buf[MAX_KEY_NAME_LEN];
6383 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006384 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006386 // do this loop twice:
6387 // loop == 0: count the number of matching options
6388 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 for (loop = 0; loop <= 1; ++loop)
6390 {
6391 regmatch->rm_ic = ic;
6392 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6393 {
K.Takataeeec2542021-06-02 13:28:16 +02006394 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6396 {
6397 if (loop == 0)
6398 num_normal++;
6399 else
6400 (*file)[count++] = vim_strsave((char_u *)names[match]);
6401 }
6402 }
6403 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6404 opt_idx++)
6405 {
6406 if (options[opt_idx].var == NULL)
6407 continue;
6408 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6409 && !(options[opt_idx].flags & P_BOOL))
6410 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006411 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 if (is_term_opt && num_normal > 0)
6413 continue;
6414 match = FALSE;
6415 if (vim_regexec(regmatch, str, (colnr_T)0)
6416 || (options[opt_idx].shortname != NULL
6417 && vim_regexec(regmatch,
6418 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6419 match = TRUE;
6420 else if (is_term_opt)
6421 {
6422 name_buf[0] = '<';
6423 name_buf[1] = 't';
6424 name_buf[2] = '_';
6425 name_buf[3] = str[2];
6426 name_buf[4] = str[3];
6427 name_buf[5] = '>';
6428 name_buf[6] = NUL;
6429 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6430 {
6431 match = TRUE;
6432 str = name_buf;
6433 }
6434 }
6435 if (match)
6436 {
6437 if (loop == 0)
6438 {
6439 if (is_term_opt)
6440 num_term++;
6441 else
6442 num_normal++;
6443 }
6444 else
6445 (*file)[count++] = vim_strsave(str);
6446 }
6447 }
6448 /*
6449 * Check terminal key codes, these are not in the option table
6450 */
6451 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6452 {
6453 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6454 {
6455 if (!isprint(str[0]) || !isprint(str[1]))
6456 continue;
6457
6458 name_buf[0] = 't';
6459 name_buf[1] = '_';
6460 name_buf[2] = str[0];
6461 name_buf[3] = str[1];
6462 name_buf[4] = NUL;
6463
6464 match = FALSE;
6465 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6466 match = TRUE;
6467 else
6468 {
6469 name_buf[0] = '<';
6470 name_buf[1] = 't';
6471 name_buf[2] = '_';
6472 name_buf[3] = str[0];
6473 name_buf[4] = str[1];
6474 name_buf[5] = '>';
6475 name_buf[6] = NUL;
6476
6477 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6478 match = TRUE;
6479 }
6480 if (match)
6481 {
6482 if (loop == 0)
6483 num_term++;
6484 else
6485 (*file)[count++] = vim_strsave(name_buf);
6486 }
6487 }
6488
6489 /*
6490 * Check special key names.
6491 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006492 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6494 {
6495 name_buf[0] = '<';
6496 STRCPY(name_buf + 1, str);
6497 STRCAT(name_buf, ">");
6498
6499 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6500 {
6501 if (loop == 0)
6502 num_term++;
6503 else
6504 (*file)[count++] = vim_strsave(name_buf);
6505 }
6506 }
6507 }
6508 if (loop == 0)
6509 {
6510 if (num_normal > 0)
6511 *num_file = num_normal;
6512 else if (num_term > 0)
6513 *num_file = num_term;
6514 else
6515 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006516 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 if (*file == NULL)
6518 {
6519 *file = (char_u **)"";
6520 return FAIL;
6521 }
6522 }
6523 }
6524 return OK;
6525}
6526
6527 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006528ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006530 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 char_u *buf;
6532
6533 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006534 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 if (*file == NULL)
6536 return FAIL;
6537
6538 /*
6539 * For a terminal key code expand_option_idx is < 0.
6540 */
6541 if (expand_option_idx < 0)
6542 {
6543 var = find_termcode(expand_option_name + 2);
6544 if (var == NULL)
6545 expand_option_idx = findoption(expand_option_name);
6546 }
6547
6548 if (expand_option_idx >= 0)
6549 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006550 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 option_value2string(&options[expand_option_idx], expand_option_flags);
6552 var = NameBuff;
6553 }
6554 else if (var == NULL)
6555 var = (char_u *)"";
6556
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006557 // A backslash is required before some characters. This is the reverse of
6558 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 buf = vim_strsave_escaped(var, escape_chars);
6560
6561 if (buf == NULL)
6562 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006563 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 return FAIL;
6565 }
6566
6567#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006568 // For MS-Windows et al. we don't double backslashes at the start and
6569 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006570 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 if (var[0] == '\\' && var[1] == '\\'
6572 && expand_option_idx >= 0
6573 && (options[expand_option_idx].flags & P_EXPAND)
6574 && vim_isfilec(var[2])
6575 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006576 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577#endif
6578
6579 *file[0] = buf;
6580 *num_file = 1;
6581 return OK;
6582}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583
6584/*
6585 * Get the value for the numeric or string option *opp in a nice format into
6586 * NameBuff[]. Must not be called with a hidden option!
6587 */
6588 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006589option_value2string(
6590 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006591 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592{
6593 char_u *varp;
6594
6595 varp = get_varp_scope(opp, opt_flags);
6596
6597 if (opp->flags & P_NUM)
6598 {
6599 long wc = 0;
6600
6601 if (wc_use_keyname(varp, &wc))
6602 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6603 else if (wc != 0)
6604 STRCPY(NameBuff, transchar((int)wc));
6605 else
6606 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6607 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006608 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 {
6610 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006611 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 NameBuff[0] = NUL;
6613#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006614 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006615 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 STRCPY(NameBuff, "*****");
6617#endif
6618 else if (opp->flags & P_EXPAND)
6619 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006620 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 else if ((char_u **)opp->var == &p_pt)
6622 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6623 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006624 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 }
6626}
6627
6628/*
6629 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6630 * printed as a keyname.
6631 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6632 */
6633 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006634wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
6636 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6637 {
6638 *wcp = *(long *)varp;
6639 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6640 return TRUE;
6641 }
6642 return FALSE;
6643}
6644
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 * Return TRUE if "x" is present in 'shortmess' option, or
6647 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6648 */
6649 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006650shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006652 return p_shm != NULL &&
6653 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 || (vim_strchr(p_shm, 'a') != NULL
6655 && vim_strchr((char_u *)SHM_A, x) != NULL));
6656}
6657
6658/*
6659 * paste_option_changed() - Called after p_paste was set or reset.
6660 */
6661 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006662paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663{
6664 static int old_p_paste = FALSE;
6665 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006666 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667#ifdef FEAT_CMDL_INFO
6668 static int save_ru = 0;
6669#endif
6670#ifdef FEAT_RIGHTLEFT
6671 static int save_ri = 0;
6672 static int save_hkmap = 0;
6673#endif
6674 buf_T *buf;
6675
6676 if (p_paste)
6677 {
6678 /*
6679 * Paste switched from off to on.
6680 * Save the current values, so they can be restored later.
6681 */
6682 if (!old_p_paste)
6683 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006684 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006685 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
6687 buf->b_p_tw_nopaste = buf->b_p_tw;
6688 buf->b_p_wm_nopaste = buf->b_p_wm;
6689 buf->b_p_sts_nopaste = buf->b_p_sts;
6690 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006691 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006692#ifdef FEAT_VARTABS
6693 if (buf->b_p_vsts_nopaste)
6694 vim_free(buf->b_p_vsts_nopaste);
6695 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6696 ? vim_strsave(buf->b_p_vsts) : NULL;
6697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 }
6699
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006700 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006702 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703#ifdef FEAT_CMDL_INFO
6704 save_ru = p_ru;
6705#endif
6706#ifdef FEAT_RIGHTLEFT
6707 save_ri = p_ri;
6708 save_hkmap = p_hkmap;
6709#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006710 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006711 p_ai_nopaste = p_ai;
6712 p_et_nopaste = p_et;
6713 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 p_tw_nopaste = p_tw;
6715 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006716#ifdef FEAT_VARTABS
6717 if (p_vsts_nopaste)
6718 vim_free(p_vsts_nopaste);
6719 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 }
6722
6723 /*
6724 * Always set the option values, also when 'paste' is set when it is
6725 * already on.
6726 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006727 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006728 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006730 buf->b_p_tw = 0; // textwidth is 0
6731 buf->b_p_wm = 0; // wrapmargin is 0
6732 buf->b_p_sts = 0; // softtabstop is 0
6733 buf->b_p_ai = 0; // no auto-indent
6734 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006735#ifdef FEAT_VARTABS
6736 if (buf->b_p_vsts)
6737 free_string_option(buf->b_p_vsts);
6738 buf->b_p_vsts = empty_option;
6739 if (buf->b_p_vsts_array)
6740 vim_free(buf->b_p_vsts_array);
6741 buf->b_p_vsts_array = 0;
6742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 }
6744
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006745 // set global options
6746 p_sm = 0; // no showmatch
6747 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006750 status_redraw_all(); // redraw to remove the ruler
6751 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752#endif
6753#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006754 p_ri = 0; // no reverse insert
6755 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006757 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 p_tw = 0;
6759 p_wm = 0;
6760 p_sts = 0;
6761 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006762#ifdef FEAT_VARTABS
6763 if (p_vsts)
6764 free_string_option(p_vsts);
6765 p_vsts = empty_option;
6766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 }
6768
6769 /*
6770 * Paste switched from on to off: Restore saved values.
6771 */
6772 else if (old_p_paste)
6773 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006774 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006775 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 {
6777 buf->b_p_tw = buf->b_p_tw_nopaste;
6778 buf->b_p_wm = buf->b_p_wm_nopaste;
6779 buf->b_p_sts = buf->b_p_sts_nopaste;
6780 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006781 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006782#ifdef FEAT_VARTABS
6783 if (buf->b_p_vsts)
6784 free_string_option(buf->b_p_vsts);
6785 buf->b_p_vsts = buf->b_p_vsts_nopaste
6786 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6787 if (buf->b_p_vsts_array)
6788 vim_free(buf->b_p_vsts_array);
6789 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
6790 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
6791 else
6792 buf->b_p_vsts_array = 0;
6793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 }
6795
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006796 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006798 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006801 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 p_ru = save_ru;
6803#endif
6804#ifdef FEAT_RIGHTLEFT
6805 p_ri = save_ri;
6806 p_hkmap = save_hkmap;
6807#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006808 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006809 p_ai = p_ai_nopaste;
6810 p_et = p_et_nopaste;
6811 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 p_tw = p_tw_nopaste;
6813 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006814#ifdef FEAT_VARTABS
6815 if (p_vsts)
6816 free_string_option(p_vsts);
6817 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 }
6820
6821 old_p_paste = p_paste;
6822}
6823
6824/*
6825 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6826 *
6827 * Reset 'compatible' and set the values for options that didn't get set yet
6828 * to the Vim defaults.
6829 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006830 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 */
6832 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006833vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006835 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006836 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006837 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
6839 if (!option_was_set((char_u *)"cp"))
6840 {
6841 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006842 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6844 set_option_default(opt_idx, OPT_FREE, FALSE);
6845 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006846 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006848
6849 if (fname != NULL)
6850 {
6851 p = vim_getenv(envname, &dofree);
6852 if (p == NULL)
6853 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006854 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006855 p = FullName_save(fname, FALSE);
6856 if (p != NULL)
6857 {
6858 vim_setenv(envname, p);
6859 vim_free(p);
6860 }
6861 }
6862 else if (dofree)
6863 vim_free(p);
6864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865}
6866
6867/*
6868 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6869 */
6870 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006871change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006873 int opt_idx;
6874
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 if (p_cp != on)
6876 {
6877 p_cp = on;
6878 compatible_set();
6879 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006880 opt_idx = findoption((char_u *)"cp");
6881 if (opt_idx >= 0)
6882 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883}
6884
6885/*
6886 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006887 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 */
6889 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006890option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 int idx;
6893
6894 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006895 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 return FALSE;
6897 if (options[idx].flags & P_WAS_SET)
6898 return TRUE;
6899 return FALSE;
6900}
6901
6902/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006903 * Reset the flag indicating option "name" was set.
6904 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006905 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006906reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006907{
6908 int idx = findoption(name);
6909
6910 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006911 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006912 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006913 return OK;
6914 }
6915 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006916}
6917
6918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 * compatible_set() - Called when 'compatible' has been set or unset.
6920 *
6921 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6922 * flag) to a Vi compatible value.
6923 * When 'compatible' is unset: Set all options that have a different default
6924 * for Vim (without the P_VI_DEF flag) to that default.
6925 */
6926 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006927compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928{
6929 int opt_idx;
6930
Bram Moolenaardac13472019-09-16 21:06:21 +02006931 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6933 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6934 set_option_default(opt_idx, OPT_FREE, p_cp);
6935 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006936 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937}
6938
Bram Moolenaardac13472019-09-16 21:06:21 +02006939#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941/*
6942 * fill_breakat_flags() -- called when 'breakat' changes value.
6943 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006944 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006945fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006947 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 int i;
6949
6950 for (i = 0; i < 256; i++)
6951 breakat_flags[i] = FALSE;
6952
6953 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006954 for (p = p_breakat; *p; p++)
6955 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957#endif
6958
6959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 * Check if backspacing over something is allowed.
6961 */
6962 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006963can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006964 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006966#ifdef FEAT_JOB_CHANNEL
6967 if (what == BS_START && bt_prompt(curbuf))
6968 return FALSE;
6969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 switch (*p_bs)
6971 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006972 case '3': return TRUE;
6973 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 case '1': return (what != BS_START);
6975 case '0': return FALSE;
6976 }
6977 return vim_strchr(p_bs, what) != NULL;
6978}
6979
6980/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01006981 * Return the effective 'scrolloff' value for the current window, using the
6982 * global value when appropriate.
6983 */
6984 long
6985get_scrolloff_value(void)
6986{
6987 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
6988}
6989
6990/*
6991 * Return the effective 'sidescrolloff' value for the current window, using the
6992 * global value when appropriate.
6993 */
6994 long
6995get_sidescrolloff_value(void)
6996{
6997 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
6998}
6999
7000/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007001 * Get the local or global value of 'backupcopy'.
7002 */
7003 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007004get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007005{
7006 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7007}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007008
Bram Moolenaaree857022019-11-09 23:26:40 +01007009#if defined(FEAT_LINEBREAK) || defined(PROTO)
7010/*
7011 * Get the local or global value of 'showbreak'.
7012 */
7013 char_u *
7014get_showbreak_value(win_T *win)
7015{
7016 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7017 return p_sbr;
7018 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7019 return empty_option;
7020 return win->w_p_sbr;
7021}
7022#endif
7023
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007024#if defined(FEAT_EVAL) || defined(PROTO)
7025/*
7026 * Get window or buffer local options.
7027 */
7028 dict_T *
7029get_winbuf_options(int bufopt)
7030{
7031 dict_T *d;
7032 int opt_idx;
7033
7034 d = dict_alloc();
7035 if (d == NULL)
7036 return NULL;
7037
Bram Moolenaardac13472019-09-16 21:06:21 +02007038 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007039 {
7040 struct vimoption *opt = &options[opt_idx];
7041
7042 if ((bufopt && (opt->indir & PV_BUF))
7043 || (!bufopt && (opt->indir & PV_WIN)))
7044 {
7045 char_u *varp = get_varp(opt);
7046
7047 if (varp != NULL)
7048 {
7049 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007050 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007051 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007052 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007053 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007054 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007055 }
7056 }
7057 }
7058
7059 return d;
7060}
7061#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007062
Bram Moolenaardac13472019-09-16 21:06:21 +02007063#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007064/*
7065 * This is called when 'culopt' is changed
7066 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007067 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007068fill_culopt_flags(char_u *val, win_T *wp)
7069{
7070 char_u *p;
7071 char_u culopt_flags_new = 0;
7072
7073 if (val == NULL)
7074 p = wp->w_p_culopt;
7075 else
7076 p = val;
7077 while (*p != NUL)
7078 {
7079 if (STRNCMP(p, "line", 4) == 0)
7080 {
7081 p += 4;
7082 culopt_flags_new |= CULOPT_LINE;
7083 }
7084 else if (STRNCMP(p, "both", 4) == 0)
7085 {
7086 p += 4;
7087 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7088 }
7089 else if (STRNCMP(p, "number", 6) == 0)
7090 {
7091 p += 6;
7092 culopt_flags_new |= CULOPT_NBR;
7093 }
7094 else if (STRNCMP(p, "screenline", 10) == 0)
7095 {
7096 p += 10;
7097 culopt_flags_new |= CULOPT_SCRLINE;
7098 }
7099
7100 if (*p != ',' && *p != NUL)
7101 return FAIL;
7102 if (*p == ',')
7103 ++p;
7104 }
7105
7106 // Can't have both "line" and "screenline".
7107 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7108 return FAIL;
7109 wp->w_p_culopt_flags = culopt_flags_new;
7110
7111 return OK;
7112}
7113#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007114
7115/*
7116 * Get the value of 'magic' adjusted for Vim9 script.
7117 */
7118 int
7119magic_isset(void)
7120{
7121 switch (magic_overruled)
7122 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007123 case OPTION_MAGIC_ON: return TRUE;
7124 case OPTION_MAGIC_OFF: return FALSE;
7125 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007126 }
7127#ifdef FEAT_EVAL
7128 if (in_vim9script())
7129 return TRUE;
7130#endif
7131 return p_magic;
7132}