blob: 816837d0af75f8928d9e1eb6703540bf4638fba3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
13 * - Put it in the options array below (copy an existing entry).
14 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
16 * - Add a PV_XX entry to the enum below.
17 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
19 * - For a buffer option, add some code to buf_copy_options().
20 * - For a buffer string option, add code to check_buf_options().
21 * - If it's a numeric option, add any necessary bounds checks to do_set().
22 * - If it's a list of flags, add some code in do_set(), search for WW_ALL.
23 * - When adding an option with expansion (P_EXPAND), but with a different
24 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020025 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 * options.txt, and any other related places.
27 * - Add an entry in runtime/optwin.vim.
28 * When making changes:
29 * - Adjust the help for the option in doc/option.txt.
30 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
31 * comment at the help for the 'compatible' option.
32 */
33
34#define IN_OPTION_C
35#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020036#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010039static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020040static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010041static char_u *option_expand(int opt_idx, char_u *val);
42static void didset_options(void);
43static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000044#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000046#else
47# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
48#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010049static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
50static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020051static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020053static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010054static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010055static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
57static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020058static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000059static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020061static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000062static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010063static void check_winopt(winopt_T *wop);
64static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010065static void paste_option_changed(void);
66static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/*
69 * Initialize the options, first part.
70 *
71 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010072 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000073 */
74 void
Bram Moolenaar07268702018-03-01 21:57:32 +010075set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
77 char_u *p;
78 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000079 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81#ifdef FEAT_LANGMAP
82 langmap_init();
83#endif
84
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010085 // Be Vi compatible by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 p_cp = TRUE;
87
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010088 // Use POSIX compatibility when $VIM_POSIX is set.
Bram Moolenaar4399ef42005-02-12 14:29:27 +000089 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +000090 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +000091 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020092 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000093 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000094
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 /*
96 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +000097 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 */
Bram Moolenaar7c626922005-02-07 22:01:03 +000099 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100100#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100102 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000104 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200105#if defined(MSWIN)
106 {
107 // For MS-Windows put the path in quotes instead of escaping spaces.
108 char_u *cmd;
109 size_t len;
110
111 if (vim_strchr(p, ' ') != NULL)
112 {
113 len = STRLEN(p) + 3; // two quotes and a trailing NUL
114 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200115 if (cmd != NULL)
116 {
117 vim_snprintf((char *)cmd, len, "\"%s\"", p);
118 set_string_default("sh", cmd);
119 vim_free(cmd);
120 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200121 }
122 else
123 set_string_default("sh", p);
124 }
125#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100126 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 /*
130 * Set the default for 'backupskip' to include environment variables for
131 * temp files.
132 */
133 {
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100134#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100136#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100138#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000139 int len;
140 garray_T ga;
141 int mustfree;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200142 char_u *item;
143
144 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
146 ga_init2(&ga, 1, 100);
K.Takataeeec2542021-06-02 13:28:16 +0200147 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000149 mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100150#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100152# ifdef MACOS_X
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200153 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100154# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156# endif
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100157 else
158#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000159 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 if (p != NULL && *p != NUL)
161 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100162 // First time count the NUL, otherwise count the ','.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000163 len = (int)STRLEN(p) + 3;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200164 item = alloc(len);
165 STRCPY(item, p);
166 add_pathsep(item);
167 STRCAT(item, "*");
168 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
169 == NULL
170 && ga_grow(&ga, len) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 {
172 if (ga.ga_len > 0)
173 STRCAT(ga.ga_data, ",");
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200174 STRCAT(ga.ga_data, item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 ga.ga_len += len;
176 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200177 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000179 if (mustfree)
180 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 if (ga.ga_data != NULL)
183 {
184 set_string_default("bsk", ga.ga_data);
185 vim_free(ga.ga_data);
186 }
187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
189 /*
190 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
191 */
192 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000193 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000195#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
196 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
197#endif
198 {
ichizok02560422022-04-05 14:18:44 +0100199#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100200 // Use amount of memory available at this moment.
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200201 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100202#elif defined(HAVE_TOTAL_MEM)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100203 // Use amount of memory available to Vim.
Bram Moolenaar914572a2007-05-01 11:37:47 +0000204 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100205#else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000206 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000209 opt_idx = findoption((char_u *)"maxmem");
210 if (opt_idx >= 0)
211 {
212#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100213 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
214 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000215#endif
216 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
217 }
218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 }
220
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 {
222 char_u *cdpath;
223 char_u *buf;
224 int i;
225 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000226 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100228 // Initialize the 'cdpath' option's default value.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000229 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 if (cdpath != NULL)
231 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200232 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 if (buf != NULL)
234 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100235 buf[0] = ','; // start with ",", current dir first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 j = 1;
237 for (i = 0; cdpath[i] != NUL; ++i)
238 {
239 if (vim_ispathlistsep(cdpath[i]))
240 buf[j++] = ',';
241 else
242 {
243 if (cdpath[i] == ' ' || cdpath[i] == ',')
244 buf[j++] = '\\';
245 buf[j++] = cdpath[i];
246 }
247 }
248 buf[j] = NUL;
249 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000250 if (opt_idx >= 0)
251 {
252 options[opt_idx].def_val[VI_DEFAULT] = buf;
253 options[opt_idx].flags |= P_DEF_ALLOCED;
254 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200255 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100256 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000258 if (mustfree)
259 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 }
261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
ichizok02560422022-04-05 14:18:44 +0100263#if defined(FEAT_POSTSCRIPT) && \
264 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100265 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100267# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100269# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100271# elif defined(MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100273# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275# endif
276 );
277#endif
278
279#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100280 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100282# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000283 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100284# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
286
ichizok02560422022-04-05 14:18:44 +0100287# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289# endif
290 );
291#endif
292
293 /*
294 * Set all the options (except the terminal options) to their default
295 * value. Also set the global value for local options.
296 */
297 set_options_default(0);
298
matveytadbb1bf2022-02-01 17:26:12 +0000299#ifdef UNIX
300 // Force restricted-mode on for "nologin" or "false" $SHELL
301 p = get_isolated_shell_name();
302 if (p != NULL)
303 {
304 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
305 restricted = TRUE;
306 vim_free(p);
307 }
308#endif
309
Bram Moolenaar07268702018-03-01 21:57:32 +0100310#ifdef CLEAN_RUNTIMEPATH
311 if (clean_arg)
312 {
313 opt_idx = findoption((char_u *)"runtimepath");
314 if (opt_idx >= 0)
315 {
316 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
317 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
318 }
319 opt_idx = findoption((char_u *)"packpath");
320 if (opt_idx >= 0)
321 {
322 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
323 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
324 }
325 }
326#endif
327
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_GUI
329 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100330 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#endif
332
333 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100334 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100335 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 check_buf_options(curbuf);
337 check_win_options(curwin);
338 check_options();
339
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100340 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 didset_options();
342
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000343#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100344 // Use the current chartab for the generic chartab. This is not in
345 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000346 init_spell_chartab();
347#endif
348
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 /*
350 * Expand environment variables and things like "~" for the defaults.
351 * If option_expand() returns non-NULL the variable is expanded. This can
352 * only happen for non-indirect options.
353 * Also set the default to the expanded value, so ":set" does not list
354 * them.
355 * Don't set the P_ALLOCED flag, because we don't want to free the
356 * default.
357 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200358 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 {
360 if ((options[opt_idx].flags & P_GETTEXT)
361 && options[opt_idx].var != NULL)
362 p = (char_u *)_(*(char **)options[opt_idx].var);
363 else
364 p = option_expand(opt_idx, NULL);
365 if (p != NULL && (p = vim_strsave(p)) != NULL)
366 {
367 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100368 // VIMEXP
369 // Defaults for all expanded options are currently the same for Vi
370 // and Vim. When this changes, add some code here! Also need to
371 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 if (options[opt_idx].flags & P_DEF_ALLOCED)
373 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
374 options[opt_idx].def_val[VI_DEFAULT] = p;
375 options[opt_idx].flags |= P_DEF_ALLOCED;
376 }
377 }
378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100379 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100382 // Detect use of mlterm.
383 // Mlterm is a terminal emulator akin to xterm that has some special
384 // abilities (bidi namely).
385 // NOTE: mlterm's author is being asked to 'set' a variable
386 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100388 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#endif
390
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200391 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392
Bram Moolenaar4f974752019-02-17 17:44:42 +0100393# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 /*
395 * If $LANG isn't set, try to get a good value for it. This makes the
396 * right language be used automatically. Don't do this for English.
397 */
398 if (mch_getenv((char_u *)"LANG") == NULL)
399 {
400 char buf[20];
401
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100402 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
403 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
404 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
406 (LPTSTR)buf, 20);
407 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
408 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100409 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
411 STRCPY(buf, "zh_TW");
412 else if (STRNICMP(buf, "chs", 3) == 0
413 || STRNICMP(buf, "zhc", 3) == 0)
414 STRCPY(buf, "zh_CN");
415 else if (STRNICMP(buf, "jp", 2) == 0)
416 STRCPY(buf, "ja");
417 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100418 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100419 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421 }
ichizok02560422022-04-05 14:18:44 +0100422# elif defined(MACOS_CONVERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100423 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000424 mac_lang_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425# endif
426
K.Takataf883d902021-05-30 18:04:19 +0200427# ifdef MSWIN
428 // MS-Windows has builtin support for conversion to and from Unicode, using
429 // "utf-8" for 'encoding' should work best for most users.
430 p = vim_strsave((char_u *)ENC_DFLT);
431# else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100432 // enc_locale() will try to find the encoding of the current locale.
K.Takataf883d902021-05-30 18:04:19 +0200433 // This works best for properly configured systems, old and new.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 p = enc_locale();
K.Takataf883d902021-05-30 18:04:19 +0200435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 if (p != NULL)
437 {
438 char_u *save_enc;
439
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100440 // Try setting 'encoding' and check if the value is valid.
K.Takataf883d902021-05-30 18:04:19 +0200441 // If not, go back to the default encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 save_enc = p_enc;
443 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000444 if (STRCMP(p_enc, "gb18030") == 0)
445 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100446 // We don't support "gb18030", but "cp936" is a good substitute
447 // for practical purposes, thus use that. It's not an alias to
448 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000449 p_enc = vim_strsave((char_u *)"cp936");
450 vim_free(p);
451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 if (mb_init() == NULL)
453 {
454 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000455 if (opt_idx >= 0)
456 {
457 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
458 options[opt_idx].flags |= P_DEF_ALLOCED;
459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460
Bram Moolenaard0573012017-10-28 21:11:06 +0200461#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100462 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000463 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100464 // Adjust the default for 'isprint' and 'iskeyword' to match
465 // latin1. Also set the defaults for when 'nocompatible' is
466 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000467 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000468 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000469 set_string_option_direct((char_u *)"isk", -1,
470 ISK_LATIN1, OPT_FREE, SID_NONE);
471 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000472 if (opt_idx >= 0)
473 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000474 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000475 if (opt_idx >= 0)
476 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000477 (void)init_chartab();
478 }
479#endif
480
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200481#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100482 // Win32 console: When GetACP() returns a different value from
483 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200484 if (
485# ifdef VIMDLL
486 (!gui.in_use && !gui.starting) &&
487# endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100488 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 {
490 char buf[50];
491
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100492 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
493 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100494 if (GetConsoleCP() == 0)
495 sprintf(buf, "cp%ld", (long)GetACP());
496 else
497 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 p_tenc = vim_strsave((char_u *)buf);
499 if (p_tenc != NULL)
500 {
501 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000502 if (opt_idx >= 0)
503 {
504 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
505 options[opt_idx].flags |= P_DEF_ALLOCED;
506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 convert_setup(&input_conv, p_tenc, p_enc);
508 convert_setup(&output_conv, p_enc, p_tenc);
509 }
510 else
511 p_tenc = empty_option;
512 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100513#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100514#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100515 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000516 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 }
519 else
520 {
521 vim_free(p_enc);
522 p_enc = save_enc;
523 }
524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525
526#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100527 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 set_helplang_default(get_mess_lang());
529#endif
530}
531
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200532static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
533
534/*
535 * Set the "fileencodings" option to the default value for when 'encoding' is
536 * utf-8.
537 */
538 void
539set_fencs_unicode()
540{
541 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
542 OPT_FREE, 0);
543}
544
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545/*
546 * Set an option to its default value.
547 * This does not take care of side effects!
548 */
549 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100550set_option_default(
551 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100552 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
553 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100555 char_u *varp; // pointer to variable for current option
556 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000558 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
560
561 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
562 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100563 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 {
565 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
566 if (flags & P_STRING)
567 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200568 // 'fencs' default value depends on 'encoding'
569 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
570 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100571 // Use set_string_option_direct() for local options to handle
572 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200573 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200574 set_string_option_direct(NULL, opt_idx,
575 options[opt_idx].def_val[dvi], opt_flags, 0);
576 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200578 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
579 free_string_option(*(char_u **)(varp));
580 *(char_u **)varp = options[opt_idx].def_val[dvi];
581 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
583 }
584 else if (flags & P_NUM)
585 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000586 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 win_comp_scroll(curwin);
588 else
589 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100590 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
591
592 if ((long *)varp == &curwin->w_p_so
593 || (long *)varp == &curwin->w_p_siso)
594 // 'scrolloff' and 'sidescrolloff' local values have a
595 // different default value than the global default.
596 *(long *)varp = -1;
597 else
598 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100599 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 if (both)
601 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100602 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 }
604 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100607 // the cast to long is required for Manx C, long_i is needed for
608 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000609 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000610#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000612 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
613 *(int *)varp = FALSE;
614#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100615 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 if (both)
617 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
618 *(int *)varp;
619 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000620
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100621 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000622 flagsp = insecure_flag(opt_idx, opt_flags);
623 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 }
625
626#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200627 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628#endif
629}
630
631/*
632 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200633 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 */
635 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100636set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100637 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638{
639 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000641 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
Bram Moolenaardac13472019-09-16 21:06:21 +0200643 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200644 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200645 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100646 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200647# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200648 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200649 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200650# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100651 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 set_option_default(i, opt_flags, p_cp);
653
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100654 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000655 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200657 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658}
659
660/*
661 * Set the Vi-default value of a string option.
662 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100663 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100665 static void
666set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
668 char_u *p;
669 int opt_idx;
670
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100671 if (escape && vim_strchr(val, ' ') != NULL)
672 p = vim_strsave_escaped(val, (char_u *)" ");
673 else
674 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100675 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
677 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000678 if (opt_idx >= 0)
679 {
680 if (options[opt_idx].flags & P_DEF_ALLOCED)
681 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
682 options[opt_idx].def_val[VI_DEFAULT] = p;
683 options[opt_idx].flags |= P_DEF_ALLOCED;
684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 }
686}
687
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100688 void
689set_string_default(char *name, char_u *val)
690{
691 set_string_default_esc(name, val, FALSE);
692}
693
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200695 * For an option value that contains comma separated items, find "newval" in
696 * "origval". Return NULL if not found.
697 */
698 static char_u *
699find_dup_item(char_u *origval, char_u *newval, long_u flags)
700{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200701 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200702 size_t newlen;
703 char_u *s;
704
705 if (origval == NULL)
706 return NULL;
707
708 newlen = STRLEN(newval);
709 for (s = origval; *s != NUL; ++s)
710 {
711 if ((!(flags & P_COMMA)
712 || s == origval
713 || (s[-1] == ',' && !(bs & 1)))
714 && STRNCMP(s, newval, newlen) == 0
715 && (!(flags & P_COMMA)
716 || s[newlen] == ','
717 || s[newlen] == NUL))
718 return s;
719 // Count backslashes. Only a comma with an even number of backslashes
720 // or a single backslash preceded by a comma before it is recognized as
721 // a separator.
722 if ((s > origval + 1
723 && s[-1] == '\\'
724 && s[-2] != ',')
725 || (s == origval + 1
726 && s[-1] == '\\'))
727 ++bs;
728 else
729 bs = 0;
730 }
731 return NULL;
732}
733
734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 * Set the Vi-default value of a number option.
736 * Used for 'lines' and 'columns'.
737 */
738 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100739set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000741 int opt_idx;
742
743 opt_idx = findoption((char_u *)name);
744 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000745 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746}
747
Dominique Pelle748b3082022-01-08 12:41:16 +0000748#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200749/*
750 * Set all window-local and buffer-local options to the Vim default.
751 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200752 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200753 */
754 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200755set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200756{
757 win_T *save_curwin = curwin;
758 int i;
759
760 curwin = wp;
761 curbuf = curwin->w_buffer;
762 block_autocmds();
763
Bram Moolenaardac13472019-09-16 21:06:21 +0200764 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200765 {
766 struct vimoption *p = &(options[i]);
767 char_u *varp = get_varp_scope(p, OPT_LOCAL);
768
769 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200770 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200771 && !(options[i].flags & P_NODEFAULT)
772 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200773 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200774 }
775
776 unblock_autocmds();
777 curwin = save_curwin;
778 curbuf = curwin->w_buffer;
779}
Dominique Pelle748b3082022-01-08 12:41:16 +0000780#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200781
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000782#if defined(EXITFREE) || defined(PROTO)
783/*
784 * Free all options.
785 */
786 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100787free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000788{
789 int i;
790
Bram Moolenaardac13472019-09-16 21:06:21 +0200791 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000792 {
793 if (options[i].indir == PV_NONE)
794 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100795 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100796 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000797 free_string_option(*(char_u **)options[i].var);
798 if (options[i].flags & P_DEF_ALLOCED)
799 free_string_option(options[i].def_val[VI_DEFAULT]);
800 }
801 else if (options[i].var != VAR_WIN
802 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100803 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200804 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000805 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000806 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000807 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000808}
809#endif
810
811
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812/*
813 * Initialize the options, part two: After getting Rows and Columns and
814 * setting 'term'.
815 */
816 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100817set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000819 int idx;
820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100822 * 'scroll' defaults to half the window height. The stored default is zero,
823 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000825 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000826 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000827 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 comp_col();
829
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000830 /*
831 * 'window' is only for backwards compatibility with Vi.
832 * Default is Rows - 1.
833 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000834 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000835 p_window = Rows - 1;
836 set_number_default("window", Rows - 1);
837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100838 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100839#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000840 /*
841 * If 'background' wasn't set by the user, try guessing the value,
842 * depending on the terminal name. Only need to check for terminals
843 * with a dark background, that can handle color.
844 */
845 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000846 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
847 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000849 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100850 // don't mark it as set, when starting the GUI it may be
851 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000852 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000855
856#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100857 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000858#endif
859#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100860 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000861#endif
862#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100863 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865}
866
867/*
868 * Initialize the options, part three: After reading the .vimrc
869 */
870 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100871set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100873#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874/*
875 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
876 * This is done after other initializations, where 'shell' might have been
877 * set, but only if they have not been set before.
878 */
879 char_u *p;
880 int idx_srr;
881 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100882# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 int idx_sp;
884 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
887 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000888 if (idx_srr < 0)
889 do_srr = FALSE;
890 else
891 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100892# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000894 if (idx_sp < 0)
895 do_sp = FALSE;
896 else
897 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100898# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200899 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 if (p != NULL)
901 {
902 /*
903 * Default for p_sp is "| tee", for p_srr is ">".
904 * For known shells it is changed here to include stderr.
905 */
906 if ( fnamecmp(p, "csh") == 0
907 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100908# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 || fnamecmp(p, "csh.exe") == 0
910 || fnamecmp(p, "tcsh.exe") == 0
911# endif
912 )
913 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100914# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (do_sp)
916 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100917# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100919# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100921# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
923 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 if (do_srr)
926 {
927 p_srr = (char_u *)">&";
928 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
929 }
930 }
Mike Williams12795022021-06-28 20:53:58 +0200931# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200932 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
933 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200934 else if ( fnamecmp(p, "powershell") == 0
935 || fnamecmp(p, "powershell.exe") == 0
936 )
937 {
938# if defined(FEAT_QUICKFIX)
939 if (do_sp)
940 {
941 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
942 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
943 }
944# endif
945 if (do_srr)
946 {
947 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
948 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
949 }
950 }
951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 else
Natanael Copa56318362021-05-06 18:46:35 +0200953 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 if ( fnamecmp(p, "sh") == 0
955 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200956 || fnamecmp(p, "mksh") == 0
957 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000959 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200961 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200962 || fnamecmp(p, "ash") == 0
963 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200964 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100965# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 || fnamecmp(p, "cmd") == 0
967 || fnamecmp(p, "sh.exe") == 0
968 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200969 || fnamecmp(p, "mksh.exe") == 0
970 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000972 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 || fnamecmp(p, "bash.exe") == 0
974 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200975 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200976 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100978 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100980# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 if (do_sp)
982 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100983# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100985# else
Mike Williamsa3d1b292021-06-30 20:56:00 +0200986 if (fnamecmp(p, "pwsh") == 0)
987 p_sp = (char_u *)">%s 2>&1";
988 else
989 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100990# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
992 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (do_srr)
995 {
996 p_srr = (char_u *)">%s 2>&1";
997 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
998 }
999 }
1000 vim_free(p);
1001 }
1002#endif
1003
Bram Moolenaar4f974752019-02-17 17:44:42 +01001004#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001006 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1007 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001009 * set, but only if they have not been set before.
1010 * Default values depend on shell (cmd.exe is default shell):
1011 *
1012 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001013 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001014 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001015 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001016 * "sh" like shells - "-c" "\""
1017 *
1018 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 */
Mike Williams12795022021-06-28 20:53:58 +02001020 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1021 {
1022 int idx_opt;
1023
1024 idx_opt = findoption((char_u *)"shcf");
1025 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1026 {
1027 p_shcf = (char_u*)"-Command";
1028 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1029 }
1030
1031 idx_opt = findoption((char_u *)"sxq");
1032 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1033 {
1034 p_sxq = (char_u*)"\"";
1035 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1036 }
1037 }
1038 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
1040 int idx3;
1041
1042 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001043 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
1045 p_shcf = (char_u *)"-c";
1046 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1047 }
1048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001049 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001051 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 {
1053 p_sxq = (char_u *)"\"";
1054 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001057 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1058 {
1059 int idx3;
1060
1061 /*
1062 * cmd.exe on Windows will strip the first and last double quote given
1063 * on the command line, e.g. most of the time things like:
1064 * cmd /c "my path/to/echo" "my args to echo"
1065 * become:
1066 * my path/to/echo" "my args to echo
1067 * when executed.
1068 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001069 * To avoid this, set shellxquote to surround the command in
1070 * parenthesis. This appears to make most commands work, without
1071 * breaking commands that worked previously, such as
1072 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001073 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001074 idx3 = findoption((char_u *)"sxq");
1075 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1076 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001077 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001078 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1079 }
1080
Bram Moolenaara64ba222012-02-12 23:23:31 +01001081 idx3 = findoption((char_u *)"shcf");
1082 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1083 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001084 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001085 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1086 }
1087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088#endif
1089
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001090 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001091 {
1092 int idx_ffs = findoption((char_u *)"ffs");
1093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001094 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001095 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1096 set_fileformat(default_fileformat(), OPT_LOCAL);
1097 }
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100}
1101
1102#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1103/*
1104 * When 'helplang' is still at its default value, set it to "lang".
1105 * Only the first two characters of "lang" are used.
1106 */
1107 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001108set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110 int idx;
1111
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001112 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 return;
1114 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001115 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
1117 if (options[idx].flags & P_ALLOCED)
1118 free_string_option(p_hlg);
1119 p_hlg = vim_strsave(lang);
1120 if (p_hlg == NULL)
1121 p_hlg = empty_option;
1122 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001123 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001124 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001125 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1126 {
1127 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1128 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1129 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001130 // any C like setting, such as C.UTF-8, becomes "en"
1131 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1132 {
1133 p_hlg[0] = 'e';
1134 p_hlg[1] = 'n';
1135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 options[idx].flags |= P_ALLOCED;
1139 }
1140}
1141#endif
1142
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143/*
1144 * 'title' and 'icon' only default to true if they have not been set or reset
1145 * in .vimrc and we can read the old value.
1146 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1147 * they can be reset. This reduces startup time when using X on a remote
1148 * machine.
1149 */
1150 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001151set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152{
1153 int idx1;
1154 long val;
1155
1156 /*
1157 * If GUI is (going to be) used, we can always set the window title and
1158 * icon name. Saves a bit of time, because the X11 display server does
1159 * not need to be contacted.
1160 */
1161 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001162 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 {
1164#ifdef FEAT_GUI
1165 if (gui.starting || gui.in_use)
1166 val = TRUE;
1167 else
1168#endif
1169 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001170 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 p_title = val;
1172 }
1173 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001174 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {
1176#ifdef FEAT_GUI
1177 if (gui.starting || gui.in_use)
1178 val = TRUE;
1179 else
1180#endif
1181 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001182 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 p_icon = val;
1184 }
1185}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001187 void
1188ex_set(exarg_T *eap)
1189{
1190 int flags = 0;
1191
1192 if (eap->cmdidx == CMD_setlocal)
1193 flags = OPT_LOCAL;
1194 else if (eap->cmdidx == CMD_setglobal)
1195 flags = OPT_GLOBAL;
1196#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001197 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001198 ex_options(eap);
1199 else
1200#endif
1201 {
1202 if (eap->forceit)
1203 flags |= OPT_ONECOLUMN;
1204 (void)do_set(eap->arg, flags);
1205 }
1206}
1207
Bram Moolenaar47403942022-09-21 21:12:53 +01001208typedef enum {
1209 OP_NONE = 0,
1210 OP_ADDING, // "opt+=arg"
1211 OP_PREPENDING, // "opt^=arg"
1212 OP_REMOVING, // "opt-=arg"
1213} set_op_T;
1214
1215/*
1216 * Part of do_set() for string options.
1217 * Returns FAIL on failure, do not process further options.
1218 */
1219 static int
1220do_set_string(
1221 int opt_idx,
1222 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001223 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001224 int nextchar,
1225 set_op_T op_arg,
1226 int flags,
1227 int cp_val,
1228 char_u *varp_arg,
1229 char *errbuf,
1230 int *value_checked,
1231 char **errmsg)
1232{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001233 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001234 set_op_T op = op_arg;
1235 char_u *varp = varp_arg;
1236 char_u *save_arg = NULL;
1237 char_u *s = NULL;
1238 char_u *oldval = NULL; // previous value if *varp
1239 char_u *newval;
1240 char_u *origval = NULL;
1241 char_u *origval_l = NULL;
1242 char_u *origval_g = NULL;
1243#if defined(FEAT_EVAL)
1244 char_u *saved_origval = NULL;
1245 char_u *saved_origval_l = NULL;
1246 char_u *saved_origval_g = NULL;
1247 char_u *saved_newval = NULL;
1248#endif
1249 unsigned newlen;
1250 int comma;
1251 char_u whichwrap[80];
1252
1253 // When using ":set opt=val" for a global option
1254 // with a local value the local value will be
1255 // reset, use the global value here.
1256 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1257 && ((int)options[opt_idx].indir & PV_BOTH))
1258 varp = options[opt_idx].var;
1259
1260 // The old value is kept until we are sure that the new value is valid.
1261 oldval = *(char_u **)varp;
1262
1263 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1264 {
1265 origval_l = *(char_u **)get_varp_scope(
1266 &(options[opt_idx]), OPT_LOCAL);
1267 origval_g = *(char_u **)get_varp_scope(
1268 &(options[opt_idx]), OPT_GLOBAL);
1269
1270 // A global-local string option might have an empty option as value to
1271 // indicate that the global value should be used.
1272 if (((int)options[opt_idx].indir & PV_BOTH)
1273 && origval_l == empty_option)
1274 origval_l = origval_g;
1275 }
1276
1277 // When setting the local value of a global option, the old value may be
1278 // the global value.
1279 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1280 origval = *(char_u **)get_varp(&options[opt_idx]);
1281 else
1282 origval = oldval;
1283
1284 if (nextchar == '&') // set to default val
1285 {
1286 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1287 ? VI_DEFAULT : VIM_DEFAULT];
1288 if ((char_u **)varp == &p_bg)
1289 {
1290 // guess the value of 'background'
1291#ifdef FEAT_GUI
1292 if (gui.in_use)
1293 newval = gui_bg_default();
1294 else
1295#endif
1296 newval = term_bg_default();
1297 }
1298 else if ((char_u **)varp == &p_fencs && enc_utf8)
1299 newval = fencs_utf8_default;
1300
1301 // expand environment variables and ~ since the default value was
1302 // already expanded, only required when an environment variable was set
1303 // later
1304 if (newval == NULL)
1305 newval = empty_option;
1306 else
1307 {
1308 s = option_expand(opt_idx, newval);
1309 if (s == NULL)
1310 s = newval;
1311 newval = vim_strsave(s);
1312 }
1313 }
1314 else if (nextchar == '<') // set to global val
1315 {
1316 newval = vim_strsave(*(char_u **)get_varp_scope(
1317 &(options[opt_idx]), OPT_GLOBAL));
1318 }
1319 else
1320 {
Bram Moolenaar6f981142022-09-22 12:48:58 +01001321 ++arg; // jump to after the '=' or ':'
Bram Moolenaar47403942022-09-21 21:12:53 +01001322
1323 /*
1324 * Set 'keywordprg' to ":help" if an empty
1325 * value was passed to :set by the user.
Bram Moolenaar47403942022-09-21 21:12:53 +01001326 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001327 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
Bram Moolenaar47403942022-09-21 21:12:53 +01001328 {
Bram Moolenaar6f981142022-09-22 12:48:58 +01001329 save_arg = arg;
zeertzjqfcba86c2022-09-22 13:57:32 +01001330 arg = (char_u *)":help";
Bram Moolenaar47403942022-09-21 21:12:53 +01001331 }
1332 /*
1333 * Convert 'backspace' number to string, for
1334 * adding, prepending and removing string.
1335 */
1336 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1337 {
1338 int i = getdigits((char_u **)varp);
1339
1340 switch (i)
1341 {
1342 case 0:
1343 *(char_u **)varp = empty_option;
1344 break;
1345 case 1:
1346 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1347 break;
1348 case 2:
1349 *(char_u **)varp = vim_strsave(
1350 (char_u *)"indent,eol,start");
1351 break;
1352 case 3:
1353 *(char_u **)varp = vim_strsave(
1354 (char_u *)"indent,eol,nostop");
1355 break;
1356 }
1357 vim_free(oldval);
1358 if (origval == oldval)
1359 origval = *(char_u **)varp;
1360 if (origval_l == oldval)
1361 origval_l = *(char_u **)varp;
1362 if (origval_g == oldval)
1363 origval_g = *(char_u **)varp;
1364 oldval = *(char_u **)varp;
1365 }
1366 /*
1367 * Convert 'whichwrap' number to string, for backwards compatibility
1368 * with Vim 3.0.
1369 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001370 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
Bram Moolenaar47403942022-09-21 21:12:53 +01001371 {
1372 *whichwrap = NUL;
Bram Moolenaar6f981142022-09-22 12:48:58 +01001373 int i = getdigits(&arg);
Bram Moolenaar47403942022-09-21 21:12:53 +01001374 if (i & 1)
1375 STRCAT(whichwrap, "b,");
1376 if (i & 2)
1377 STRCAT(whichwrap, "s,");
1378 if (i & 4)
1379 STRCAT(whichwrap, "h,l,");
1380 if (i & 8)
1381 STRCAT(whichwrap, "<,>,");
1382 if (i & 16)
1383 STRCAT(whichwrap, "[,],");
1384 if (*whichwrap != NUL) // remove trailing ,
1385 whichwrap[STRLEN(whichwrap) - 1] = NUL;
Bram Moolenaar6f981142022-09-22 12:48:58 +01001386 save_arg = arg;
1387 arg = (char_u *)whichwrap;
Bram Moolenaar47403942022-09-21 21:12:53 +01001388 }
1389 /*
1390 * Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1391 * version 3.0
1392 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001393 else if (*arg == '>' && (varp == (char_u *)&p_dir
Bram Moolenaar47403942022-09-21 21:12:53 +01001394 || varp == (char_u *)&p_bdir))
Bram Moolenaar6f981142022-09-22 12:48:58 +01001395 ++arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001396
1397 /*
1398 * Copy the new string into allocated memory.
1399 * Can't use set_string_option_direct(), because we need to remove the
1400 * backslashes.
1401 */
1402 // get a bit too much
Bram Moolenaar6f981142022-09-22 12:48:58 +01001403 newlen = (unsigned)STRLEN(arg) + 1;
Bram Moolenaar47403942022-09-21 21:12:53 +01001404 if (op != OP_NONE)
1405 newlen += (unsigned)STRLEN(origval) + 1;
1406 newval = alloc(newlen);
1407 if (newval == NULL) // out of mem, don't change
1408 return FAIL;
1409 s = newval;
1410
1411 /*
1412 * Copy the string, skip over escaped chars.
1413 * For MS-DOS and WIN32 backslashes before normal file name characters
1414 * are not removed, and keep backslash at start, for "\\machine\path",
1415 * but do remove it for "\\\\machine\\path".
1416 * The reverse is found in ExpandOldSetting().
1417 */
zeertzjqfcba86c2022-09-22 13:57:32 +01001418 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar47403942022-09-21 21:12:53 +01001419 {
1420 int i;
1421
Bram Moolenaar6f981142022-09-22 12:48:58 +01001422 if (*arg == '\\' && arg[1] != NUL
Bram Moolenaar47403942022-09-21 21:12:53 +01001423#ifdef BACKSLASH_IN_FILENAME
1424 && !((flags & P_EXPAND)
Bram Moolenaar6f981142022-09-22 12:48:58 +01001425 && vim_isfilec(arg[1])
1426 && !VIM_ISWHITE(arg[1])
1427 && (arg[1] != '\\'
zeertzjqfcba86c2022-09-22 13:57:32 +01001428 || (s == newval && arg[2] != '\\')))
Bram Moolenaar47403942022-09-21 21:12:53 +01001429#endif
1430 )
Bram Moolenaar6f981142022-09-22 12:48:58 +01001431 ++arg; // remove backslash
1432 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar47403942022-09-21 21:12:53 +01001433 {
1434 // copy multibyte char
Bram Moolenaar6f981142022-09-22 12:48:58 +01001435 mch_memmove(s, arg, (size_t)i);
1436 arg += i;
Bram Moolenaar47403942022-09-21 21:12:53 +01001437 s += i;
1438 }
1439 else
Bram Moolenaar6f981142022-09-22 12:48:58 +01001440 *s++ = *arg++;
Bram Moolenaar47403942022-09-21 21:12:53 +01001441 }
1442 *s = NUL;
1443
1444 /*
1445 * Expand environment variables and ~.
1446 * Don't do it when adding without inserting a comma.
1447 */
1448 if (op == OP_NONE || (flags & P_COMMA))
1449 {
1450 s = option_expand(opt_idx, newval);
1451 if (s != NULL)
1452 {
1453 vim_free(newval);
1454 newlen = (unsigned)STRLEN(s) + 1;
1455 if (op != OP_NONE)
1456 newlen += (unsigned)STRLEN(origval) + 1;
1457 newval = alloc(newlen);
1458 if (newval == NULL)
1459 return FAIL;
1460 STRCPY(newval, s);
1461 }
1462 }
1463
1464 // locate newval[] in origval[] when removing it
1465 // and when adding to avoid duplicates
1466 int len = 0;
1467 if (op == OP_REMOVING || (flags & P_NODUP))
1468 {
1469 len = (int)STRLEN(newval);
1470 s = find_dup_item(origval, newval, flags);
1471
1472 // do not add if already there
1473 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1474 {
1475 op = OP_NONE;
1476 STRCPY(newval, origval);
1477 }
1478
1479 // if no duplicate, move pointer to end of original value
1480 if (s == NULL)
1481 s = origval + (int)STRLEN(origval);
1482 }
1483
1484 // concatenate the two strings; add a ',' if needed
1485 if (op == OP_ADDING || op == OP_PREPENDING)
1486 {
1487 comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1488 if (op == OP_ADDING)
1489 {
1490 len = (int)STRLEN(origval);
1491 // strip a trailing comma, would get 2
1492 if (comma && len > 1
1493 && (flags & P_ONECOMMA) == P_ONECOMMA
1494 && origval[len - 1] == ','
1495 && origval[len - 2] != '\\')
1496 len--;
1497 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1498 mch_memmove(newval, origval, (size_t)len);
1499 }
1500 else
1501 {
1502 len = (int)STRLEN(newval);
1503 STRMOVE(newval + len + comma, origval);
1504 }
1505 if (comma)
1506 newval[len] = ',';
1507 }
1508
1509 // Remove newval[] from origval[]. (Note: "len" has been set above and
1510 // is used here).
1511 if (op == OP_REMOVING)
1512 {
1513 STRCPY(newval, origval);
1514 if (*s)
1515 {
1516 // may need to remove a comma
1517 if (flags & P_COMMA)
1518 {
1519 if (s == origval)
1520 {
1521 // include comma after string
1522 if (s[len] == ',')
1523 ++len;
1524 }
1525 else
1526 {
1527 // include comma before string
1528 --s;
1529 ++len;
1530 }
1531 }
1532 STRMOVE(newval + (s - origval), s + len);
1533 }
1534 }
1535
1536 if (flags & P_FLAGLIST)
1537 {
1538 // Remove flags that appear twice.
1539 for (s = newval; *s;)
1540 {
1541 // if options have P_FLAGLIST and P_ONECOMMA such as
1542 // 'whichwrap'
1543 if (flags & P_ONECOMMA)
1544 {
1545 if (*s != ',' && *(s + 1) == ','
1546 && vim_strchr(s + 2, *s) != NULL)
1547 {
1548 // Remove the duplicated value and the next comma.
1549 STRMOVE(s, s + 2);
1550 continue;
1551 }
1552 }
1553 else
1554 {
1555 if ((!(flags & P_COMMA) || *s != ',')
1556 && vim_strchr(s + 1, *s) != NULL)
1557 {
1558 STRMOVE(s, s + 1);
1559 continue;
1560 }
1561 }
1562 ++s;
1563 }
1564 }
1565
zeertzjqfcba86c2022-09-22 13:57:32 +01001566 if (save_arg != NULL)
1567 arg = save_arg; // arg was temporarily changed, restore it
Bram Moolenaar47403942022-09-21 21:12:53 +01001568 }
1569
1570 /*
1571 * Set the new value.
1572 */
1573 *(char_u **)(varp) = newval;
1574
1575#if defined(FEAT_EVAL)
1576 if (!starting
1577# ifdef FEAT_CRYPT
1578 && options[opt_idx].indir != PV_KEY
1579# endif
1580 && origval != NULL && newval != NULL)
1581 {
1582 // origval may be freed by did_set_string_option(), make a copy.
1583 saved_origval = vim_strsave(origval);
1584 // newval (and varp) may become invalid if the buffer is closed by
1585 // autocommands.
1586 saved_newval = vim_strsave(newval);
1587 if (origval_l != NULL)
1588 saved_origval_l = vim_strsave(origval_l);
1589 if (origval_g != NULL)
1590 saved_origval_g = vim_strsave(origval_g);
1591 }
1592#endif
1593
1594 {
1595 long_u *p = insecure_flag(opt_idx, opt_flags);
1596 int secure_saved = secure;
1597
1598 // When an option is set in the sandbox, from a modeline or in secure
1599 // mode, then deal with side effects in secure mode. Also when the
1600 // value was set with the P_INSECURE flag and is not completely
1601 // replaced.
1602 if ((opt_flags & OPT_MODELINE)
1603#ifdef HAVE_SANDBOX
1604 || sandbox != 0
1605#endif
1606 || (op != OP_NONE && (*p & P_INSECURE)))
1607 secure = 1;
1608
1609 // Handle side effects, and set the global value for ":set" on local
1610 // options. Note: when setting 'syntax' or 'filetype' autocommands may
1611 // be triggered that can cause havoc.
1612 *errmsg = did_set_string_option(
1613 opt_idx, (char_u **)varp, oldval, errbuf,
1614 opt_flags, value_checked);
1615
1616 secure = secure_saved;
1617 }
1618
1619#if defined(FEAT_EVAL)
1620 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00001621 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01001622 saved_origval_l, saved_origval_g, saved_newval);
1623 vim_free(saved_origval);
1624 vim_free(saved_origval_l);
1625 vim_free(saved_origval_g);
1626 vim_free(saved_newval);
1627#endif
1628
Bram Moolenaar6f981142022-09-22 12:48:58 +01001629 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001630 return *errmsg == NULL ? OK : FAIL;
1631}
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633/*
1634 * Parse 'arg' for option settings.
1635 *
1636 * 'arg' may be IObuff, but only when no errors can be present and option
1637 * does not need to be expanded with option_expand().
1638 * "opt_flags":
1639 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001640 * OPT_GLOBAL for ":setglobal"
1641 * OPT_LOCAL for ":setlocal" and a modeline
1642 * OPT_MODELINE for a modeline
1643 * OPT_WINONLY to only set window-local options
1644 * OPT_NOWIN to skip setting window-local options
1645 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 *
1647 * returns FAIL if an error is detected, OK otherwise
1648 */
1649 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001650do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02001651 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001652 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
Bram Moolenaar1594f312021-07-08 16:40:13 +02001654 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001656 char *errmsg;
1657 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001659 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1660 int nextchar; // next non-white char after option name
1661 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 int len;
1663 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001664 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001666 long_u flags; // flags for current option
1667 char_u *varp = NULL; // pointer to variable for current option
1668 int did_show = FALSE; // already showed one value
Bram Moolenaar47403942022-09-21 21:12:53 +01001669 set_op_T op = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 int cp_val = 0;
1671 char_u key_name[2];
1672
1673 if (*arg == NUL)
1674 {
1675 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001676 did_show = TRUE;
1677 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
1679
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001680 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 {
1682 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001683 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001685 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1686 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {
1688 /*
1689 * ":set all" show all options.
1690 * ":set all&" set all options to their default value.
1691 */
1692 arg += 3;
1693 if (*arg == '&')
1694 {
1695 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001696 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001698 didset_options();
1699 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001700 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
1702 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001705 did_show = TRUE;
1706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001708 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 {
1710 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001711 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001712 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 arg += 7;
1714 }
1715 else
1716 {
1717 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001718 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 {
1720 prefix = 0;
1721 arg += 2;
1722 }
1723 else if (STRNCMP(arg, "inv", 3) == 0)
1724 {
1725 prefix = 2;
1726 arg += 3;
1727 }
1728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001729 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 key = 0;
1731 if (*arg == '<')
1732 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001734 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1736 len = 5;
1737 else
1738 {
1739 len = 1;
1740 while (arg[len] != NUL && arg[len] != '>')
1741 ++len;
1742 }
1743 if (arg[len] != '>')
1744 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001745 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 goto skip;
1747 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001748 arg[len] = NUL; // put NUL after name
1749 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001751 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001753 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
1755 else
1756 {
1757 len = 0;
1758 /*
1759 * The two characters after "t_" may not be alphanumeric.
1760 */
1761 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1762 len = 4;
1763 else
1764 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1765 ++len;
1766 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001767 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001769 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001771 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 }
1773
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001774 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 afterchar = arg[len];
1776
Bram Moolenaardeb108b2021-07-08 17:35:36 +02001777 if (in_vim9script())
1778 {
1779 char_u *p = skipwhite(arg + len);
1780
1781 // disallow white space before =val, +=val, -=val, ^=val
1782 if (p > arg + len && (p[0] == '='
1783 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1784 && p[1] == '=')))
1785 {
1786 errmsg = e_no_white_space_allowed_between_option_and;
1787 arg = p;
1788 startarg = p;
1789 goto skip;
1790 }
1791 }
1792 else
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001793 // skip white space, allow ":set ai ?", ":set hlsearch !"
1794 while (VIM_ISWHITE(arg[len]))
1795 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
Bram Moolenaar47403942022-09-21 21:12:53 +01001797 op = OP_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 if (arg[len] != NUL && arg[len + 1] == '=')
1799 {
1800 if (arg[len] == '+')
1801 {
Bram Moolenaar47403942022-09-21 21:12:53 +01001802 op = OP_ADDING; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 ++len;
1804 }
1805 else if (arg[len] == '^')
1806 {
Bram Moolenaar47403942022-09-21 21:12:53 +01001807 op = OP_PREPENDING; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 ++len;
1809 }
1810 else if (arg[len] == '-')
1811 {
Bram Moolenaar47403942022-09-21 21:12:53 +01001812 op = OP_REMOVING; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 ++len;
1814 }
1815 }
1816 nextchar = arg[len];
1817
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001818 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 {
Bram Moolenaar1594f312021-07-08 16:40:13 +02001820 if (in_vim9script() && arg > arg_start
1821 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1822 errmsg = e_no_white_space_allowed_between_option_and;
1823 else
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001824 errmsg = e_unknown_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 goto skip;
1826 }
1827
1828 if (opt_idx >= 0)
1829 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001830 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001832 // Only give an error message when requesting the value of
1833 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1835 && (!(options[opt_idx].flags & P_BOOL)
1836 || nextchar == '?'))
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001837 errmsg = e_option_not_supported;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 goto skip;
1839 }
1840
1841 flags = options[opt_idx].flags;
1842 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1843 }
1844 else
1845 {
1846 flags = P_STRING;
1847 if (key < 0)
1848 {
1849 key_name[0] = KEY2TERMCAP0(key);
1850 key_name[1] = KEY2TERMCAP1(key);
1851 }
1852 else
1853 {
1854 key_name[0] = KS_KEY;
1855 key_name[1] = (key & 0xff);
1856 }
1857 }
1858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001859 // Skip all options that are not window-local (used when showing
1860 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001861 if ((opt_flags & OPT_WINONLY)
1862 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1863 goto skip;
1864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001865 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001866 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1867 && options[opt_idx].var == VAR_WIN)
1868 goto skip;
1869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001870 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001871 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001873 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001874 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001875 errmsg = e_not_allowed_in_modeline;
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001876 goto skip;
1877 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001878 if ((flags & P_MLE) && !p_mle)
1879 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001880 errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
Bram Moolenaar110289e2019-05-23 15:38:06 +02001881 goto skip;
1882 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001883#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001884 // In diff mode some options are overruled. This avoids that
1885 // 'foldmethod' becomes "marker" instead of "diff" and that
1886 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001887 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001888 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001889 && (
1890#ifdef FEAT_FOLDING
1891 options[opt_idx].indir == PV_FDM ||
1892#endif
1893 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001894 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897
1898#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001899 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001900 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001902 errmsg = e_not_allowed_in_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 goto skip;
1904 }
1905#endif
1906
1907 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1908 {
1909 arg += len;
1910 cp_val = p_cp;
1911 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1912 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001913 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
1915 cp_val = FALSE;
1916 arg += 3;
1917 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001918 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
1920 cp_val = TRUE;
1921 arg += 2;
1922 }
1923 }
1924 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001925 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001927 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 goto skip;
1929 }
1930 }
1931
1932 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001933 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001934 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 */
1936 if (nextchar == '?'
1937 || (prefix == 1
1938 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1939 && !(flags & P_BOOL)))
1940 {
1941 /*
1942 * print value
1943 */
1944 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001945 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 else
1947 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001948 gotocmdline(TRUE); // cursor at status line
1949 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 }
1951 if (opt_idx >= 0)
1952 {
1953 showoneopt(&options[opt_idx], opt_flags);
1954#ifdef FEAT_EVAL
1955 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001956 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001957 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001958 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001959 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001960 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001961 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001962 (int)options[opt_idx].indir & PV_MASK]);
1963 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001964 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001965 (int)options[opt_idx].indir & PV_MASK]);
1966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967#endif
1968 }
1969 else
1970 {
1971 char_u *p;
1972
1973 p = find_termcode(key_name);
1974 if (p == NULL)
1975 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001976 errmsg = e_key_code_not_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 goto skip;
1978 }
1979 else
1980 (void)show_one_termcode(key_name, p, TRUE);
1981 }
1982 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001983 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001984 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
1986 else
1987 {
Bram Moolenaar916a8182018-11-25 02:18:29 +01001988 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001989
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001990 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 {
1992 if (nextchar == '=' || nextchar == ':')
1993 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001994 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 goto skip;
1996 }
1997
1998 /*
1999 * ":set opt!": invert
2000 * ":set opt&": reset to default value
2001 * ":set opt<": reset to global value
2002 */
2003 if (nextchar == '!')
2004 value = *(int *)(varp) ^ 1;
2005 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002006 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 ((flags & P_VI_DEF) || cp_val)
2008 ? VI_DEFAULT : VIM_DEFAULT];
2009 else if (nextchar == '<')
2010 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002011 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if ((int *)varp == &curbuf->b_p_ar
2013 && opt_flags == OPT_LOCAL)
2014 value = -1;
2015 else
2016 value = *(int *)get_varp_scope(&(options[opt_idx]),
2017 OPT_GLOBAL);
2018 }
2019 else
2020 {
2021 /*
2022 * ":set invopt": invert
2023 * ":set opt" or ":set noopt": set or reset
2024 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002025 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002027 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 goto skip;
2029 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002030 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 value = *(int *)(varp) ^ 1;
2032 else
2033 value = prefix;
2034 }
2035
2036 errmsg = set_bool_option(opt_idx, varp, (int)value,
2037 opt_flags);
2038 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002039 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
2041 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2042 || prefix != 1)
2043 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002044 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 goto skip;
2046 }
2047
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002048 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 {
2050 /*
2051 * Different ways to set a number option:
2052 * & set to default value
2053 * < set to global value
2054 * <xx> accept special key codes for 'wildchar'
2055 * c accept any non-digit for 'wildchar'
2056 * [-]0-9 set number
2057 * other error
2058 */
2059 ++arg;
2060 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002061 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 ((flags & P_VI_DEF) || cp_val)
2063 ? VI_DEFAULT : VIM_DEFAULT];
2064 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002065 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002066 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
2067 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002068 if ((long *)varp == &curbuf->b_p_ul
2069 && opt_flags == OPT_LOCAL)
2070 value = NO_LOCAL_UNDOLEVEL;
2071 else
2072 value = *(long *)get_varp_scope(
2073 &(options[opt_idx]), OPT_GLOBAL);
2074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 else if (((long *)varp == &p_wc
2076 || (long *)varp == &p_wcm)
2077 && (*arg == '<'
2078 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002079 || (*arg != NUL
2080 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 && !VIM_ISDIGIT(*arg))))
2082 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002083 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (value == 0 && (long *)varp != &p_wcm)
2085 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002086 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 goto skip;
2088 }
2089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2091 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002092 // Allow negative (for 'undolevels'), octal and
2093 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002094 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002095 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02002096 if (i == 0 || (arg[i] != NUL
2097 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002099 errmsg = e_number_required_after_equal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 goto skip;
2101 }
2102 }
2103 else
2104 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002105 errmsg = e_number_required_after_equal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 goto skip;
2107 }
2108
Bram Moolenaar47403942022-09-21 21:12:53 +01002109 if (op == OP_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 value = *(long *)varp + value;
Bram Moolenaar47403942022-09-21 21:12:53 +01002111 else if (op == OP_PREPENDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 value = *(long *)varp * value;
Bram Moolenaar47403942022-09-21 21:12:53 +01002113 else if (op == OP_REMOVING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 value = *(long *)varp - value;
2115 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00002116 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002118 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
Bram Moolenaar47403942022-09-21 21:12:53 +01002120 if (do_set_string(opt_idx, opt_flags, &arg, nextchar,
2121 op, flags, cp_val, varp, errbuf,
2122 &value_checked, &errmsg) == FAIL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002123 {
Bram Moolenaar47403942022-09-21 21:12:53 +01002124 if (errmsg != NULL)
2125 goto skip;
2126 break;
Bram Moolenaard7c96872019-06-15 17:12:48 +02002127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002129 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
2131 char_u *p;
2132
2133 if (nextchar == '&')
2134 {
2135 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002136 errmsg = e_not_found_in_termcap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
2138 else
2139 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002140 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002141 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 if (*p == '\\' && p[1] != NUL)
2143 ++p;
2144 nextchar = *p;
2145 *p = NUL;
2146 add_termcode(key_name, arg, FALSE);
2147 *p = nextchar;
2148 }
2149 if (full_screen)
2150 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002151 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002154
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002156 did_set_option(
Bram Moolenaar47403942022-09-21 21:12:53 +01002157 opt_idx, opt_flags, op == OP_NONE, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
2159
2160skip:
2161 /*
2162 * Advance to next argument.
2163 * - skip until a blank found, taking care of backslashes
2164 * - skip blanks
2165 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2166 */
2167 for (i = 0; i < 2 ; ++i)
2168 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002169 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (*arg++ == '\\' && *arg != NUL)
2171 ++arg;
2172 arg = skipwhite(arg);
2173 if (*arg != '=')
2174 break;
2175 }
2176 }
2177
2178 if (errmsg != NULL)
2179 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002180 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002181 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (i + (arg - startarg) < IOSIZE)
2183 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002184 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 STRCAT(IObuff, ": ");
2186 mch_memmove(IObuff + i, startarg, (arg - startarg));
2187 IObuff[i + (arg - startarg)] = NUL;
2188 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002189 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 trans_characters(IObuff, IOSIZE);
2191
Bram Moolenaar13608d82022-08-29 15:06:50 +01002192 ++no_wait_return; // wait_return() done later
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002193 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 --no_wait_return;
2195
2196 return FAIL;
2197 }
2198
2199 arg = skipwhite(arg);
2200 }
2201
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002202theend:
2203 if (silent_mode && did_show)
2204 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002205 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002206 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002207 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002208 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002209 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002210 out_flush();
2211 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002212 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002213 }
2214
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 return OK;
2216}
2217
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002218/*
2219 * Call this when an option has been given a new value through a user command.
2220 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2221 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002222 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002223did_set_option(
2224 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002225 int opt_flags, // possibly with OPT_MODELINE
2226 int new_value, // value was replaced completely
2227 int value_checked) // value was checked to be safe, no need to set the
2228 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002229{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002230 long_u *p;
2231
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002232 options[opt_idx].flags |= P_WAS_SET;
2233
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002234 // When an option is set in the sandbox, from a modeline or in secure mode
2235 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2236 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002237 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002238 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002239#ifdef HAVE_SANDBOX
2240 || sandbox != 0
2241#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002242 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002243 *p = *p | P_INSECURE;
2244 else if (new_value)
2245 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002246}
2247
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248/*
2249 * Convert a key name or string into a key value.
2250 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002251 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002253 int
2254string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
2256 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002257 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (*arg == '^')
2259 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002260 if (multi_byte)
2261 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 return *arg;
2263}
2264
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265/*
2266 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2267 * maketitle() to create and display it.
2268 * When switching the title or icon off, call mch_restore_title() to get
2269 * the old value back.
2270 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002271 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002272did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273{
2274 if (starting != NO_SCREEN
2275#ifdef FEAT_GUI
2276 && !gui.starting
2277#endif
2278 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281
2282/*
2283 * set_options_bin - called when 'bin' changes value.
2284 */
2285 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002286set_options_bin(
2287 int oldval,
2288 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002289 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290{
2291 /*
2292 * The option values that are changed when 'bin' changes are
2293 * copied when 'bin is set and restored when 'bin' is reset.
2294 */
2295 if (newval)
2296 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002297 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
2299 if (!(opt_flags & OPT_GLOBAL))
2300 {
2301 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2302 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2303 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2304 curbuf->b_p_et_nobin = curbuf->b_p_et;
2305 }
2306 if (!(opt_flags & OPT_LOCAL))
2307 {
2308 p_tw_nobin = p_tw;
2309 p_wm_nobin = p_wm;
2310 p_ml_nobin = p_ml;
2311 p_et_nobin = p_et;
2312 }
2313 }
2314
2315 if (!(opt_flags & OPT_GLOBAL))
2316 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002317 curbuf->b_p_tw = 0; // no automatic line wrap
2318 curbuf->b_p_wm = 0; // no automatic line wrap
2319 curbuf->b_p_ml = 0; // no modelines
2320 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 }
2322 if (!(opt_flags & OPT_LOCAL))
2323 {
2324 p_tw = 0;
2325 p_wm = 0;
2326 p_ml = FALSE;
2327 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002328 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
2330 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002331 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
2333 if (!(opt_flags & OPT_GLOBAL))
2334 {
2335 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2336 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2337 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2338 curbuf->b_p_et = curbuf->b_p_et_nobin;
2339 }
2340 if (!(opt_flags & OPT_LOCAL))
2341 {
2342 p_tw = p_tw_nobin;
2343 p_wm = p_wm_nobin;
2344 p_ml = p_ml_nobin;
2345 p_et = p_et_nobin;
2346 }
2347 }
2348}
2349
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350/*
2351 * Expand environment variables for some string options.
2352 * These string options cannot be indirect!
2353 * If "val" is NULL expand the current value of the option.
2354 * Return pointer to NameBuff, or NULL when not expanded.
2355 */
2356 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002357option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002359 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2361 return NULL;
2362
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002363 // If val is longer than MAXPATHL no meaningful expansion can be done,
2364 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 if (val != NULL && STRLEN(val) > MAXPATHL)
2366 return NULL;
2367
2368 if (val == NULL)
2369 val = *(char_u **)options[opt_idx].var;
2370
2371 /*
2372 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2373 * Escape spaces when expanding 'tags', they are used to separate file
2374 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002375 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 */
2377 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002378 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002379#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002380 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2381#endif
2382 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002383 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 return NULL;
2385
2386 return NameBuff;
2387}
2388
2389/*
2390 * After setting various option values: recompute variables that depend on
2391 * option values.
2392 */
2393 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002394didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002396 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 (void)init_chartab();
2398
Bram Moolenaardac13472019-09-16 21:06:21 +02002399 didset_string_options();
2400
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002401#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002402 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002403 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002404 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002405 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002406#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002407 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 (void)check_cedit();
Bram Moolenaar597a4222014-06-25 14:39:50 +02002409#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002410 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002411 fill_breakat_flags();
2412#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002413 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002414}
2415
2416/*
2417 * More side effects of setting options.
2418 */
2419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002420didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002421{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002422 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002423 (void)highlight_changed();
2424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002425 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002426 check_opt_wim();
2427
Bram Moolenaareed9d462021-02-15 20:38:25 +01002428 // Parse default for 'listchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002429 (void)set_chars_option(curwin, &curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002430
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002431 // Parse default for 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002432 (void)set_chars_option(curwin, &curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002433
2434#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002435 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002436 (void)check_clipboard_option();
2437#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002438#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002439 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002440 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002441 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002442 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444}
2445
2446/*
2447 * Check for string options that are NULL (normally only termcap options).
2448 */
2449 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002450check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
2452 int opt_idx;
2453
2454 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2455 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2456 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2457}
2458
2459/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002460 * Return the option index found by a pointer into term_strings[].
2461 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002463 int
2464get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002466 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467
2468 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2469 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002470 return opt_idx;
2471 return -1; // cannot happen: didn't find it!
2472}
2473
2474/*
2475 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2476 * Return the option index or -1 if not found.
2477 */
2478 int
2479set_term_option_alloced(char_u **p)
2480{
2481 int opt_idx = get_term_opt_idx(p);
2482
2483 if (opt_idx >= 0)
2484 options[opt_idx].flags |= P_ALLOCED;
2485 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486}
2487
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002488#if defined(FEAT_EVAL) || defined(PROTO)
2489/*
2490 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2491 * Return FALSE when it wasn't.
2492 * Return -1 for an unknown option.
2493 */
2494 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002495was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002496{
2497 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002498 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002499
2500 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002501 {
2502 flagp = insecure_flag(idx, opt_flags);
2503 return (*flagp & P_INSECURE) != 0;
2504 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002505 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002506 return -1;
2507}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002508
2509/*
2510 * Get a pointer to the flags used for the P_INSECURE flag of option
2511 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002512 * NOTE: Caller must make sure that "curwin" is set to the window from which
2513 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002514 */
2515 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002516insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002517{
2518 if (opt_flags & OPT_LOCAL)
2519 switch ((int)options[opt_idx].indir)
2520 {
2521#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002522 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002523#endif
2524#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002525# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002526 case PV_FDE: return &curwin->w_p_fde_flags;
2527 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002528# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002529# ifdef FEAT_BEVAL
2530 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2531# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002532 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002533 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002535 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002536# endif
2537#endif
2538 }
2539
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002540 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002541 return &options[opt_idx].flags;
2542}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002543#endif
2544
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002545/*
2546 * Redraw the window title and/or tab page text later.
2547 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002548void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002549{
2550 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002551 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002552}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002553
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002555 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2556 * characters or characters in "allowed".
2557 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002558 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002559valid_name(char_u *val, char *allowed)
2560{
2561 char_u *s;
2562
2563 for (s = val; *s != NUL; ++s)
2564 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2565 return FALSE;
2566 return TRUE;
2567}
2568
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002569#if defined(FEAT_EVAL) || defined(PROTO)
2570/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002571 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002572 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002573 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002574 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002575set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002576{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002577 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2578 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002579 sctx_T new_script_ctx = script_ctx;
2580
Bram Moolenaar51258742020-05-03 17:19:33 +02002581 // Modeline already has the line number set.
2582 if (!(opt_flags & OPT_MODELINE))
2583 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002584
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002585 // Remember where the option was set. For local options need to do that
2586 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002587 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002588 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002589 if (both || (opt_flags & OPT_LOCAL))
2590 {
2591 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002592 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002593 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002594 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002595 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002596 if (both)
2597 // also setting the "all buffers" value
2598 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2599 new_script_ctx;
2600 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002601 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002602}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002603
2604/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002605 * Get the script context of global option "name".
2606 *
2607 */
2608 sctx_T *
2609get_option_sctx(char *name)
2610{
2611 int idx = findoption((char_u *)name);
2612
2613 if (idx >= 0)
2614 return &options[idx].script_ctx;
2615 siemsg("no such option: %s", name);
2616 return NULL;
2617}
2618
2619/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002620 * Set the script_ctx for a termcap option.
2621 * "name" must be the two character code, e.g. "RV".
2622 * When "name" is NULL use "opt_idx".
2623 */
2624 void
2625set_term_option_sctx_idx(char *name, int opt_idx)
2626{
2627 char_u buf[5];
2628 int idx;
2629
2630 if (name == NULL)
2631 idx = opt_idx;
2632 else
2633 {
2634 buf[0] = 't';
2635 buf[1] = '_';
2636 buf[2] = name[0];
2637 buf[3] = name[1];
2638 buf[4] = 0;
2639 idx = findoption(buf);
2640 }
2641 if (idx >= 0)
2642 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2643}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002644#endif
2645
Bram Moolenaarf4140482020-02-15 23:06:45 +01002646#if defined(FEAT_EVAL)
2647/*
2648 * Apply the OptionSet autocommand.
2649 */
2650 static void
2651apply_optionset_autocmd(
2652 int opt_idx,
2653 long opt_flags,
2654 long oldval,
2655 long oldval_g,
2656 long newval,
2657 char *errmsg)
2658{
2659 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2660
2661 // Don't do this while starting up, failure or recursively.
2662 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2663 return;
2664
2665 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2666 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2667 oldval_g);
2668 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2669 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2670 (opt_flags & OPT_LOCAL) ? "local" : "global");
2671 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2672 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2673 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2674 if (opt_flags & OPT_LOCAL)
2675 {
2676 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2677 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2678 }
2679 if (opt_flags & OPT_GLOBAL)
2680 {
2681 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2682 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2683 }
2684 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2685 {
2686 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2687 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2688 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2689 }
2690 if (opt_flags & OPT_MODELINE)
2691 {
2692 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2693 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2694 }
2695 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2696 NULL, FALSE, NULL);
2697 reset_v_option_vars();
2698}
2699#endif
2700
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701/*
2702 * Set the value of a boolean option, and take care of side effects.
2703 * Returns NULL for success, or an error message for an error.
2704 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002705 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002706set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002707 int opt_idx, // index in options[] table
2708 char_u *varp, // pointer to the option variable
2709 int value, // new value
2710 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
2712 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002713#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002714 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002715#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002716 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002718 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if ((secure
2720#ifdef HAVE_SANDBOX
2721 || sandbox != 0
2722#endif
2723 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002724 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002726#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002727 // Save the global value before changing anything. This is needed as for
2728 // a global-only option setting the "local value" in fact sets the global
2729 // value (since there is only one value).
2730 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2731 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2732 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002733#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002734
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002735 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002737 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002738 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739#endif
2740
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002741#ifdef FEAT_GUI
2742 need_mouse_correct = TRUE;
2743#endif
2744
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2747 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2748
2749 /*
2750 * Handle side effects of changing a bool option.
2751 */
2752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002753 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
Bram Moolenaar920694c2016-08-21 17:45:02 +02002757#ifdef FEAT_LANGMAP
2758 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002759 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002760 p_lnr = !p_lrm;
2761 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002762 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002763 p_lrm = !p_lnr;
2764#endif
2765
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002766#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002768 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2769 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002770 // Only take action when the option was set. When reset we do not
2771 // delete the undo file, the option may be set again without making
2772 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002773 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002774 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002775 char_u hash[UNDO_HASH_SIZE];
2776 buf_T *save_curbuf = curbuf;
2777
Bram Moolenaar29323592016-07-24 22:04:11 +02002778 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002779 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 // When 'undofile' is set globally: for every buffer, otherwise
2781 // only for the current buffer: Try to read in the undofile,
2782 // if one exists, the buffer wasn't changed and the buffer was
2783 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002784 if ((curbuf == save_curbuf
2785 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2786 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2787 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002788#ifdef FEAT_CRYPT
2789 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2790 continue;
2791#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002792 u_compute_hash(hash);
2793 u_read_undo(NULL, hash, curbuf->b_fname);
2794 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002795 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002796 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002797 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002798 }
2799#endif
2800
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 else if ((int *)varp == &curbuf->b_p_ro)
2802 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002803 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2805 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002806
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002807 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002808 if (curbuf->b_p_ro)
2809 curbuf->b_did_warn = FALSE;
2810
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002811 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
2813
Bram Moolenaar9c449722010-07-20 18:44:27 +02002814#ifdef FEAT_GUI
2815 else if ((int *)varp == &p_mh)
2816 {
2817 if (!p_mh)
2818 gui_mch_mousehide(FALSE);
2819 }
2820#endif
2821
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002822 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002824 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002825# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002826 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002827 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2828 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002829 {
2830 curbuf->b_p_ma = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002831 return e_cannot_make_terminal_with_running_job_modifiable;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002832 }
2833# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002834 redraw_titles();
2835 }
K.Takata845bbb72022-11-05 18:31:19 +00002836 // redraw the window title and tab page text when 'endoffile', 'endofline',
2837 // 'fixeol' or 'bomb' is changed
2838 else if ((int *)varp == &curbuf->b_p_eof
2839 || (int *)varp == &curbuf->b_p_eol
2840 || (int *)varp == &curbuf->b_p_fixeol
2841 || (int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002842 {
2843 redraw_titles();
2844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002846 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 else if ((int *)varp == &curbuf->b_p_bin)
2848 {
2849 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002850 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
2852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2855 {
2856 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2857 NULL, NULL, TRUE, curbuf);
2858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002860 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 else if ((int *)varp == &curbuf->b_p_swf)
2862 {
2863 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002864 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002866 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2867 // buf->b_p_swf
2868 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 }
2870
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002871 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 else if ((int *)varp == &p_terse)
2873 {
2874 char_u *p;
2875
2876 p = vim_strchr(p_shm, SHM_SEARCH);
2877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 if (p_terse && p == NULL)
2880 {
2881 STRCPY(IObuff, p_shm);
2882 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002883 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002885 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002887 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 }
2889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002890 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 else if ((int *)varp == &p_paste)
2892 {
2893 paste_option_changed();
2894 }
2895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002896 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 else if ((int *)varp == &p_im)
2898 {
2899 if (p_im)
2900 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002901 if ((State & MODE_INSERT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 need_start_insertmode = TRUE;
2903 stop_insert_mode = FALSE;
2904 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002905 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002906 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
2908 need_start_insertmode = FALSE;
2909 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002910 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002911 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 restart_edit = 0;
2913 }
2914 }
2915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 else if ((int *)varp == &p_ic && p_hls)
2918 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002919 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
2921
2922#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 else if ((int *)varp == &p_hls)
2925 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002926 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 }
2928#endif
2929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002930 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2931 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 else if ((int *)varp == &curwin->w_p_scb)
2933 {
2934 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002937 curwin->w_scbind_pos = curwin->w_topline;
2938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940
Bram Moolenaar4033c552017-09-16 20:54:51 +02002941#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002942 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 else if ((int *)varp == &curwin->w_p_pvw)
2944 {
2945 if (curwin->w_p_pvw)
2946 {
2947 win_T *win;
2948
Bram Moolenaar29323592016-07-24 22:04:11 +02002949 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 if (win->w_p_pvw && win != curwin)
2951 {
2952 curwin->w_p_pvw = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002953 return e_preview_window_already_exists;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
2955 }
2956 }
2957#endif
2958
Bram Moolenaarf6196f42022-10-02 21:29:55 +01002959 else if ((int *)varp == &curwin->w_p_sms)
2960 {
2961 if (!curwin->w_p_sms)
2962 {
2963 curwin->w_skipcol = 0;
2964 changed_line_abv_curs();
2965 }
2966 }
2967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002968 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 else if ((int *)varp == &curbuf->b_p_tx)
2970 {
2971 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2972 }
2973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002974 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 set_string_option_direct((char_u *)"ffs", -1,
2978 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002979 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981
2982 /*
2983 * When 'lisp' option changes include/exclude '-' in
2984 * keyword characters.
2985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2987 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002992 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002994 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996
2997 else if ((int *)varp == &curbuf->b_changed)
2998 {
2999 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003000 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003001 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004
3005#ifdef BACKSLASH_IN_FILENAME
3006 else if ((int *)varp == &p_ssl)
3007 {
3008 if (p_ssl)
3009 {
3010 psepc = '/';
3011 psepcN = '\\';
3012 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
3014 else
3015 {
3016 psepc = '\\';
3017 psepcN = '/';
3018 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 }
3020
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003021 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 buflist_slash_adjust();
3023 alist_slash_adjust();
3024# ifdef FEAT_EVAL
3025 scriptnames_slash_adjust();
3026# endif
3027 }
3028#endif
3029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003030 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 else if ((int *)varp == &curwin->w_p_wrap)
3032 {
3033 if (curwin->w_p_wrap)
3034 curwin->w_leftcol = 0;
3035 }
3036
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 else if ((int *)varp == &p_ea)
3038 {
3039 if (p_ea && !old_value)
3040 win_equal(curwin, FALSE, 0);
3041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043 else if ((int *)varp == &p_wiv)
3044 {
3045 /*
3046 * When 'weirdinvert' changed, set/reset 't_xs'.
3047 * Then set 'weirdinvert' according to value of 't_xs'.
3048 */
3049 if (p_wiv && !old_value)
3050 T_XS = (char_u *)"y";
3051 else if (!p_wiv && old_value)
3052 T_XS = empty_option;
3053 p_wiv = (*T_XS != NUL);
3054 }
3055
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003056#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 else if ((int *)varp == &p_beval)
3058 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003059 if (!balloonEvalForTerm)
3060 {
3061 if (p_beval && !old_value)
3062 gui_mch_enable_beval_area(balloonEval);
3063 else if (!p_beval && old_value)
3064 gui_mch_disable_beval_area(balloonEval);
3065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003067#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003068#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003069 else if ((int *)varp == &p_bevalterm)
3070 {
3071 mch_bevalterm_changed();
3072 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003075#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 else if ((int *)varp == &p_acd)
3077 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003078 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003079 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 }
3081#endif
3082
3083#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 else if ((int *)varp == &curwin->w_p_diff)
3086 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003087 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003088 diff_buf_adjust(curwin);
3089# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 if (foldmethodIsDiff(curwin))
3091 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
3094#endif
3095
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003096#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003097 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 else if ((int *)varp == &p_imdisable)
3099 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003100 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 if (p_imdisable)
3102 im_set_active(FALSE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003103 else if (State & MODE_INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003104 // When the option is set from an autocommand, it may need to take
3105 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003106 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
3108#endif
3109
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003110#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003111 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003112 else if ((int *)varp == &curwin->w_p_spell)
3113 {
3114 if (curwin->w_p_spell)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003115 errmsg = did_set_spelllang(curwin);
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003116 }
3117#endif
3118
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119#ifdef FEAT_ARABIC
3120 if ((int *)varp == &curwin->w_p_arab)
3121 {
3122 if (curwin->w_p_arab)
3123 {
3124 /*
3125 * 'arabic' is set, handle various sub-settings.
3126 */
3127 if (!p_tbidi)
3128 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003129 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 if (!curwin->w_p_rl)
3131 {
3132 curwin->w_p_rl = TRUE;
3133 changed_window_setting();
3134 }
3135
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003136 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 if (!p_arshape)
3138 {
3139 p_arshape = TRUE;
3140 redraw_later_clear();
3141 }
3142 }
3143
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003144 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003145 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003147 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003148 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3149
Bram Moolenaar8820b482017-03-16 17:23:31 +01003150 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003151 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003152#ifdef FEAT_EVAL
3153 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3154#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003157 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159
3160# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003161 // Force-set the necessary keymap for arabic
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003162 errmsg = set_option_value((char_u *)"keymap",
3163 0L, (char_u *)"arabic", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 }
3166 else
3167 {
3168 /*
3169 * 'arabic' is reset, handle various sub-settings.
3170 */
3171 if (!p_tbidi)
3172 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003173 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 if (curwin->w_p_rl)
3175 {
3176 curwin->w_p_rl = FALSE;
3177 changed_window_setting();
3178 }
3179
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003180 // 'arabicshape' isn't reset, it is a global option and
3181 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 }
3183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003184 // 'delcombine' isn't reset, it is a global option and another
3185 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003188 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 curbuf->b_p_iminsert = B_IMODE_NONE;
3190 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3191# endif
3192 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003193 }
3194
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003195#endif
3196
Bram Moolenaar44826212019-08-22 21:23:20 +02003197#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3198 else if (((int *)varp == &curwin->w_p_nu
3199 || (int *)varp == &curwin->w_p_rnu)
3200 && gui.in_use
3201 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3202 && curbuf->b_signlist != NULL)
3203 {
3204 // If the 'number' or 'relativenumber' options are modified and
3205 // 'signcolumn' is set to 'number', then clear the screen for a full
3206 // refresh. Otherwise the sign icons are not displayed properly in the
3207 // number column. If the 'number' option is set and only the
3208 // 'relativenumber' option is toggled, then don't refresh the screen
3209 // (optimization).
3210 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003211 redraw_all_later(UPD_CLEAR);
Bram Moolenaar44826212019-08-22 21:23:20 +02003212 }
3213#endif
3214
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003215#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003216 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003217 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003218 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003219# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003220 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003221 if (
3222# ifdef VIMDLL
3223 !gui.in_use && !gui.starting &&
3224# endif
3225 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003226 {
3227 p_tgc = 0;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003228 return e_24_bit_colors_are_not_supported_on_this_environment;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003229 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003230 if (is_term_win32())
3231 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003232# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003233# ifdef FEAT_GUI
3234 if (!gui.in_use && !gui.starting)
3235# endif
3236 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003237# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003238 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003239 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003240 {
3241 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003242 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003243 init_highlight(TRUE, FALSE);
3244 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003245# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003246# ifdef FEAT_TERMINAL
3247 term_update_colors_all();
LemonBoyb2b3acb2022-05-20 10:10:34 +01003248 term_update_palette_all();
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003249 term_update_wincolor_all();
3250# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003251 }
3252#endif
3253
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 /*
3255 * End of handling side effects for bool options.
3256 */
3257
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003258 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 options[opt_idx].flags |= P_WAS_SET;
3261
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003262#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003263 apply_optionset_autocmd(opt_idx, opt_flags,
3264 (long)(old_value ? TRUE : FALSE),
3265 (long)(old_global_value ? TRUE : FALSE),
3266 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003267#endif
3268
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003269 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003270 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003271 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003272 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003273
3274 if ((opt_flags & OPT_NO_REDRAW) == 0)
3275 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003277 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278}
3279
3280/*
3281 * Set the value of a number option, and take care of side effects.
3282 * Returns NULL for success, or an error message for an error.
3283 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003284 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003285set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003286 int opt_idx, // index in options[] table
3287 char_u *varp, // pointer to the option variable
3288 long value, // new value
3289 char *errbuf, // buffer for error messages
3290 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003291 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3292 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003294 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003296#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003297 long old_global_value = 0; // only used when setting a local and
3298 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003299#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003300 long old_Rows = Rows; // remember old Rows
3301 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 long *pp = (long *)varp;
3303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003304 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003305 if ((secure
3306#ifdef HAVE_SANDBOX
3307 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003309 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003310 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003312#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003313 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003314 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003315 // value (since there is only one value).
3316 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003317 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3318 OPT_GLOBAL);
3319#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003320
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 *pp = value;
3322#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003323 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003324 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003326#ifdef FEAT_GUI
3327 need_mouse_correct = TRUE;
3328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
Bram Moolenaar14f24742012-08-08 18:01:05 +02003330 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003332 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003333#ifdef FEAT_VARTABS
3334 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3335 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003336 ? tabstop_first(curbuf->b_p_vts_array)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003337 : curbuf->b_p_ts;
3338#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 }
3342
3343 /*
3344 * Number options that need some action when changed
3345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 if (pp == &p_wh || pp == &p_hh)
3347 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003348 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 if (p_wh < 1)
3350 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003351 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 p_wh = 1;
3353 }
3354 if (p_wmh > p_wh)
3355 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003356 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 p_wh = p_wmh;
3358 }
3359 if (p_hh < 0)
3360 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003361 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 p_hh = 0;
3363 }
3364
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003365 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003366 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
3368 if (pp == &p_wh && curwin->w_height < p_wh)
3369 win_setheight((int)p_wh);
3370 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3371 win_setheight((int)p_hh);
3372 }
3373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 else if (pp == &p_wmh)
3375 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003376 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 if (p_wmh < 0)
3378 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003379 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 p_wmh = 0;
3381 }
3382 if (p_wmh > p_wh)
3383 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003384 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 p_wmh = p_wh;
3386 }
3387 win_setminheight();
3388 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003389 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003391 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 if (p_wiw < 1)
3393 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003394 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 p_wiw = 1;
3396 }
3397 if (p_wmw > p_wiw)
3398 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003399 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 p_wiw = p_wmw;
3401 }
3402
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003403 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003404 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 win_setwidth((int)p_wiw);
3406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 else if (pp == &p_wmw)
3408 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003409 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (p_wmw < 0)
3411 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003412 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 p_wmw = 0;
3414 }
3415 if (p_wmw > p_wiw)
3416 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003417 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 p_wmw = p_wiw;
3419 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003420 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003423 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 else if (pp == &p_ls)
3425 {
3426 last_status(FALSE);
3427 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003428
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003429 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003430 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003431 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003432 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435#ifdef FEAT_GUI
3436 else if (pp == &p_linespace)
3437 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003438 // Recompute gui.char_height and resize the Vim window to keep the
3439 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003440 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003441 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 }
3443#endif
3444
3445#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003446 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 else if (pp == &curwin->w_p_fdl)
3448 {
3449 if (curwin->w_p_fdl < 0)
3450 curwin->w_p_fdl = 0;
3451 newFoldLevel();
3452 }
3453
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003454 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 else if (pp == &curwin->w_p_fml)
3456 {
3457 foldUpdateAll(curwin);
3458 }
3459
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003460 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 else if (pp == &curwin->w_p_fdn)
3462 {
3463 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3464 foldUpdateAll(curwin);
3465 }
3466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003467 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 else if (pp == &curwin->w_p_fdc)
3469 {
3470 if (curwin->w_p_fdc < 0)
3471 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003472 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 curwin->w_p_fdc = 0;
3474 }
3475 else if (curwin->w_p_fdc > 12)
3476 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003477 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 curwin->w_p_fdc = 12;
3479 }
3480 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003481#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003483 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3485 {
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003486#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (foldmethodIsIndent(curwin))
3488 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003489#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003490 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3491 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003492 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3493 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003496 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003497 else if (pp == &p_mco)
3498 {
3499 if (p_mco > MAX_MCO)
3500 p_mco = MAX_MCO;
3501 else if (p_mco < 0)
3502 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003503 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003504 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 else if (pp == &curbuf->b_p_iminsert)
3507 {
3508 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3509 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003510 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 curbuf->b_p_iminsert = B_IMODE_NONE;
3512 }
3513 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003514 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003516#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003517 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 status_redraw_curbuf();
3519#endif
3520 }
3521
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003522#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003523 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003524 else if (pp == &p_imst)
3525 {
3526 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003527 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003528 }
3529#endif
3530
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003531 else if (pp == &p_window)
3532 {
3533 if (p_window < 1)
3534 p_window = 1;
3535 else if (p_window >= Rows)
3536 p_window = Rows - 1;
3537 }
3538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 else if (pp == &curbuf->b_p_imsearch)
3540 {
3541 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3542 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003543 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 curbuf->b_p_imsearch = B_IMODE_NONE;
3545 }
3546 p_imsearch = curbuf->b_p_imsearch;
3547 }
3548
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003549 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 else if (pp == &p_titlelen)
3551 {
3552 if (p_titlelen < 0)
3553 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003554 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 p_titlelen = 85;
3556 }
3557 if (starting != NO_SCREEN && old_value != p_titlelen)
3558 need_maketitle = TRUE;
3559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003561 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 else if (pp == &p_ch)
3563 {
Bram Moolenaara2a89732022-08-31 14:46:18 +01003564 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003566 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 p_ch = 1;
3568 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003569 if (p_ch > Rows - min_rows() + 1)
3570 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003572 // Only compute the new window layout when startup has been
3573 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaarc9f5f732022-10-06 11:39:06 +01003574 if ((p_ch != old_value
3575 || tabline_height() + topframe->fr_height != Rows - p_ch)
Bram Moolenaar0816f472022-10-05 15:42:32 +01003576 && full_screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577#ifdef FEAT_GUI
3578 && !gui.starting
3579#endif
Bram Moolenaar0816f472022-10-05 15:42:32 +01003580 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003581 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
3583
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003584 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 else if (pp == &p_uc)
3586 {
3587 if (p_uc < 0)
3588 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003589 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 p_uc = 100;
3591 }
3592 if (p_uc && !old_value)
3593 ml_open_files();
3594 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003595#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003596 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003597 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003598 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003599 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003600 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003601 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003602 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003603 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003604 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003605 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003606 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003607 }
3608 }
3609#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003610#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003611 else if (pp == &p_mzq)
3612 mzvim_reset_timer();
3613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003615#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003616 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003617 else if (pp == &p_pyx)
3618 {
3619 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003620 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003621 }
3622#endif
3623
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003624 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 else if (pp == &p_ul)
3626 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003627 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003629 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 p_ul = value;
3631 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003632 else if (pp == &curbuf->b_p_ul)
3633 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003634 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003635 curbuf->b_p_ul = old_value;
3636 u_sync(TRUE);
3637 curbuf->b_p_ul = value;
3638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003640#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003641 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003642 else if (pp == &curwin->w_p_nuw)
3643 {
3644 if (curwin->w_p_nuw < 1)
3645 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003646 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003647 curwin->w_p_nuw = 1;
3648 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003649 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003650 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003651 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003652 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003653 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003654 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003655 }
3656#endif
3657
Bram Moolenaar1a384422010-07-14 19:53:30 +02003658 else if (pp == &curbuf->b_p_tw)
3659 {
3660 if (curbuf->b_p_tw < 0)
3661 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003662 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003663 curbuf->b_p_tw = 0;
3664 }
3665#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003666 {
3667 win_T *wp;
3668 tabpage_T *tp;
3669
3670 FOR_ALL_TAB_WINDOWS(tp, wp)
3671 check_colorcolumn(wp);
3672 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003673#endif
3674 }
3675
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 /*
3677 * Check the bounds for numeric options here
3678 */
3679 if (Rows < min_rows() && full_screen)
3680 {
3681 if (errbuf != NULL)
3682 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003683 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003684 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 errmsg = errbuf;
3686 }
3687 Rows = min_rows();
3688 }
3689 if (Columns < MIN_COLUMNS && full_screen)
3690 {
3691 if (errbuf != NULL)
3692 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003693 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003694 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 errmsg = errbuf;
3696 }
3697 Columns = MIN_COLUMNS;
3698 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003699 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 /*
3702 * If the screen (shell) height has been changed, assume it is the
3703 * physical screenheight.
3704 */
3705 if (old_Rows != Rows || old_Columns != Columns)
3706 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003707 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 if (updating_screen)
3709 *pp = old_value;
3710 else if (full_screen
3711#ifdef FEAT_GUI
3712 && !gui.starting
3713#endif
3714 )
3715 set_shellsize((int)Columns, (int)Rows, TRUE);
3716 else
3717 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003718 // Postpone the resizing; check the size and cmdline position for
3719 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 check_shellsize();
3721 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3722 cmdline_row = Rows - p_ch;
3723 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003724 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003725 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
3727
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 if (curbuf->b_p_ts <= 0)
3729 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003730 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 curbuf->b_p_ts = 8;
3732 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003733 else if (curbuf->b_p_ts > TABSTOP_MAX)
3734 {
3735 errmsg = e_invalid_argument;
3736 curbuf->b_p_ts = 8;
3737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 if (p_tm < 0)
3739 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003740 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 p_tm = 0;
3742 }
3743 if ((curwin->w_p_scr <= 0
3744 || (curwin->w_p_scr > curwin->w_height
3745 && curwin->w_height > 0))
3746 && full_screen)
3747 {
3748 if (pp == &(curwin->w_p_scr))
3749 {
3750 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003751 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 win_comp_scroll(curwin);
3753 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003754 // If 'scroll' became invalid because of a side effect silently adjust
3755 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 else if (curwin->w_p_scr <= 0)
3757 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003758 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 curwin->w_p_scr = curwin->w_height;
3760 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003761 if (p_hi < 0)
3762 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003763 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003764 p_hi = 0;
3765 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003766 else if (p_hi > 10000)
3767 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003768 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003769 p_hi = 10000;
3770 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003771 if (p_re < 0 || p_re > 2)
3772 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003773 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003774 p_re = 0;
3775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 if (p_report < 0)
3777 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003778 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 p_report = 1;
3780 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003781 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003783 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 p_sj = Rows / 2;
3785 else
3786 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003787 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 p_sj = 1;
3789 }
3790 }
3791 if (p_so < 0 && full_screen)
3792 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003793 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 p_so = 0;
3795 }
3796 if (p_siso < 0 && full_screen)
3797 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003798 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 p_siso = 0;
3800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 if (p_cwh < 1)
3802 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003803 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 p_cwh = 1;
3805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (p_ut < 0)
3807 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003808 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 p_ut = 2000;
3810 }
3811 if (p_ss < 0)
3812 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003813 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 p_ss = 0;
3815 }
3816
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003817 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3819 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3820
3821 options[opt_idx].flags |= P_WAS_SET;
3822
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003823#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003824 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3825 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003826#endif
3827
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003828 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003829 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003830 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003831 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003832 if ((opt_flags & OPT_NO_REDRAW) == 0)
3833 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
3835 return errmsg;
3836}
3837
3838/*
3839 * Called after an option changed: check if something needs to be redrawn.
3840 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003841 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003842check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003844 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003845 int doclear = (flags & P_RCLR) == P_RCLR;
3846 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003848 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
3851 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3852 changed_window_setting();
3853 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003854 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003855 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003856 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003857 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003858 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003860 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861}
3862
3863/*
3864 * Find index for option 'arg'.
3865 * Return -1 if not found.
3866 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003867 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003868findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869{
3870 int opt_idx;
3871 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003872 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 int is_term_opt;
3874
3875 /*
3876 * For first call: Initialize the quick-access table.
3877 * It contains the index for the first option that starts with a certain
3878 * letter. There are 26 letters, plus the first "t_" option.
3879 */
3880 if (quick_tab[1] == 0)
3881 {
3882 p = options[0].fullname;
3883 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3884 {
3885 if (s[0] != p[0])
3886 {
3887 if (s[0] == 't' && s[1] == '_')
3888 quick_tab[26] = opt_idx;
3889 else
3890 quick_tab[CharOrdLow(s[0])] = opt_idx;
3891 }
3892 p = s;
3893 }
3894 }
3895
3896 /*
3897 * Check for name starting with an illegal character.
3898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 return -1;
3901
3902 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3903 if (is_term_opt)
3904 opt_idx = quick_tab[26];
3905 else
3906 opt_idx = quick_tab[CharOrdLow(arg[0])];
3907 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3908 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003909 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 break;
3911 }
3912 if (s == NULL && !is_term_opt)
3913 {
3914 opt_idx = quick_tab[CharOrdLow(arg[0])];
3915 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3916 {
3917 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003918 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 break;
3920 s = NULL;
3921 }
3922 }
3923 if (s == NULL)
3924 opt_idx = -1;
3925 return opt_idx;
3926}
3927
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00003928#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
3929 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930/*
3931 * Get the value for an option.
3932 *
3933 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003934 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003935 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003936 * String option: gov_string, *stringval gets allocated string.
3937 * Hidden Number option: gov_hidden_number.
3938 * Hidden Toggle option: gov_hidden_bool.
3939 * Hidden String option: gov_hidden_string.
3940 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003941 *
3942 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003944 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003945get_option_value(
3946 char_u *name,
3947 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003948 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003949 int *flagsp,
3950 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951{
3952 int opt_idx;
3953 char_u *varp;
3954
3955 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003956 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003957 {
3958 int key;
3959
3960 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003961 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003962 {
3963 char_u key_name[2];
3964 char_u *p;
3965
Bram Moolenaar10c75c42021-12-28 20:53:30 +00003966 if (flagsp != NULL)
3967 *flagsp = 0; // terminal option has no flags
3968
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003969 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003970 if (key < 0)
3971 {
3972 key_name[0] = KEY2TERMCAP0(key);
3973 key_name[1] = KEY2TERMCAP1(key);
3974 }
3975 else
3976 {
3977 key_name[0] = KS_KEY;
3978 key_name[1] = (key & 0xff);
3979 }
3980 p = find_termcode(key_name);
3981 if (p != NULL)
3982 {
3983 if (stringval != NULL)
3984 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003985 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003986 }
3987 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003988 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003991 varp = get_varp_scope(&(options[opt_idx]), scope);
3992
3993 if (flagsp != NULL)
3994 // Return the P_xxxx option flags.
3995 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996
3997 if (options[opt_idx].flags & P_STRING)
3998 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003999 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004000 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 if (stringval != NULL)
4002 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004003 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004004 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4005 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004007 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004008 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004009 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 *stringval = vim_strsave(*(char_u **)(varp));
4014 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004015 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004018 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004019 return (options[opt_idx].flags & P_NUM)
4020 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 if (options[opt_idx].flags & P_NUM)
4022 *numval = *(long *)varp;
4023 else
4024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004025 // Special case: 'modified' is b_changed, but we also want to consider
4026 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 if ((int *)varp == &curbuf->b_changed)
4028 *numval = curbufIsChanged();
4029 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004030 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004032 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033}
4034#endif
4035
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004036#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004037/*
4038 * Returns the option attributes and its value. Unlike the above function it
4039 * will return either global value or local value of the option depending on
4040 * what was requested, but it will never return global value if it was
4041 * requested to return local one and vice versa. Neither it will return
4042 * buffer-local value if it was requested to return window-local one.
4043 *
4044 * Pretends that option is absent if it is not present in the requested scope
4045 * (i.e. has no global, window-local or buffer-local value depending on
4046 * opt_type). Uses
4047 *
4048 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004049 * 0 hidden or unknown option, also option that does not have requested
4050 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004051 * see SOPT_* in vim.h for other flags
4052 *
4053 * Possible opt_type values: see SREQ_* in vim.h
4054 */
4055 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004056get_option_value_strict(
4057 char_u *name,
4058 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004059 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004060 int opt_type,
4061 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004062{
4063 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004064 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004065 struct vimoption *p;
4066 int r = 0;
4067
4068 opt_idx = findoption(name);
4069 if (opt_idx < 0)
4070 return 0;
4071
4072 p = &(options[opt_idx]);
4073
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004074 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004075 if (p->var == NULL)
4076 return 0;
4077
4078 if (p->flags & P_BOOL)
4079 r |= SOPT_BOOL;
4080 else if (p->flags & P_NUM)
4081 r |= SOPT_NUM;
4082 else if (p->flags & P_STRING)
4083 r |= SOPT_STRING;
4084
4085 if (p->indir == PV_NONE)
4086 {
4087 if (opt_type == SREQ_GLOBAL)
4088 r |= SOPT_GLOBAL;
4089 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004090 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004091 }
4092 else
4093 {
4094 if (p->indir & PV_BOTH)
4095 r |= SOPT_GLOBAL;
4096 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004097 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004098
4099 if (p->indir & PV_WIN)
4100 {
4101 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004102 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004103 else
4104 r |= SOPT_WIN;
4105 }
4106 else if (p->indir & PV_BUF)
4107 {
4108 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004109 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004110 else
4111 r |= SOPT_BUF;
4112 }
4113 }
4114
4115 if (stringval == NULL)
4116 return r;
4117
4118 if (opt_type == SREQ_GLOBAL)
4119 varp = p->var;
4120 else
4121 {
4122 if (opt_type == SREQ_BUF)
4123 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004124 // Special case: 'modified' is b_changed, but we also want to
4125 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004126 if (p->indir == PV_MOD)
4127 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004128 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004129 varp = NULL;
4130 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004131# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004132 else if (p->indir == PV_KEY)
4133 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004134 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004135 *stringval = NULL;
4136 varp = NULL;
4137 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004138# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004139 else
4140 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004141 buf_T *save_curbuf = curbuf;
4142
4143 // only getting a pointer, no need to use aucmd_prepbuf()
4144 curbuf = (buf_T *)from;
4145 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004146 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004147 curbuf = save_curbuf;
4148 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004149 }
4150 }
4151 else if (opt_type == SREQ_WIN)
4152 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004153 win_T *save_curwin = curwin;
4154
4155 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004156 curbuf = curwin->w_buffer;
4157 varp = get_varp(p);
4158 curwin = save_curwin;
4159 curbuf = curwin->w_buffer;
4160 }
4161 if (varp == p->var)
4162 return (r | SOPT_UNSET);
4163 }
4164
4165 if (varp != NULL)
4166 {
4167 if (p->flags & P_STRING)
4168 *stringval = vim_strsave(*(char_u **)(varp));
4169 else if (p->flags & P_NUM)
4170 *numval = *(long *) varp;
4171 else
4172 *numval = *(int *)varp;
4173 }
4174
4175 return r;
4176}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004177
4178/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004179 * Iterate over options. First argument is a pointer to a pointer to a
4180 * structure inside options[] array, second is option type like in the above
4181 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004182 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004183 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004184 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004185 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004186 * first argument to NULL.
4187 *
4188 * Returns full option name for current option on each call.
4189 */
4190 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004191option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004192{
4193 struct vimoption *ret = NULL;
4194 do
4195 {
4196 if (*option == NULL)
4197 *option = (void *) options;
4198 else if (((struct vimoption *) (*option))->fullname == NULL)
4199 {
4200 *option = NULL;
4201 return NULL;
4202 }
4203 else
4204 *option = (void *) (((struct vimoption *) (*option)) + 1);
4205
4206 ret = ((struct vimoption *) (*option));
4207
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004208 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004209 if (ret->var == NULL)
4210 {
4211 ret = NULL;
4212 continue;
4213 }
4214
4215 switch (opt_type)
4216 {
4217 case SREQ_GLOBAL:
4218 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4219 ret = NULL;
4220 break;
4221 case SREQ_BUF:
4222 if (!(ret->indir & PV_BUF))
4223 ret = NULL;
4224 break;
4225 case SREQ_WIN:
4226 if (!(ret->indir & PV_WIN))
4227 ret = NULL;
4228 break;
4229 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004230 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004231 return NULL;
4232 }
4233 }
4234 while (ret == NULL);
4235
4236 return (char_u *)ret->fullname;
4237}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004238#endif
4239
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004241 * Return the flags for the option at 'opt_idx'.
4242 */
4243 long_u
4244get_option_flags(int opt_idx)
4245{
4246 return options[opt_idx].flags;
4247}
4248
4249/*
4250 * Set a flag for the option at 'opt_idx'.
4251 */
4252 void
4253set_option_flag(int opt_idx, long_u flag)
4254{
4255 options[opt_idx].flags |= flag;
4256}
4257
4258/*
4259 * Clear a flag for the option at 'opt_idx'.
4260 */
4261 void
4262clear_option_flag(int opt_idx, long_u flag)
4263{
4264 options[opt_idx].flags &= ~flag;
4265}
4266
4267/*
4268 * Returns TRUE if the option at 'opt_idx' is a global option
4269 */
4270 int
4271is_global_option(int opt_idx)
4272{
4273 return options[opt_idx].indir == PV_NONE;
4274}
4275
4276/*
4277 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4278 * local value.
4279 */
4280 int
4281is_global_local_option(int opt_idx)
4282{
4283 return options[opt_idx].indir & PV_BOTH;
4284}
4285
4286/*
4287 * Returns TRUE if the option at 'opt_idx' is a window-local option
4288 */
4289 int
4290is_window_local_option(int opt_idx)
4291{
4292 return options[opt_idx].var == VAR_WIN;
4293}
4294
4295/*
4296 * Returns TRUE if the option at 'opt_idx' is a hidden option
4297 */
4298 int
4299is_hidden_option(int opt_idx)
4300{
4301 return options[opt_idx].var == NULL;
4302}
4303
4304#if defined(FEAT_CRYPT) || defined(PROTO)
4305/*
4306 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4307 */
4308 int
4309is_crypt_key_option(int opt_idx)
4310{
4311 return options[opt_idx].indir == PV_KEY;
4312}
4313#endif
4314
4315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 * Set the value of option "name".
4317 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004318 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004319 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004321 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004322set_option_value(
4323 char_u *name,
4324 long number,
4325 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004326 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
4328 int opt_idx;
4329 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004330 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
4332 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004333 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004334 {
4335 int key;
4336
4337 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004338 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004339 {
4340 char_u key_name[2];
4341
4342 if (key < 0)
4343 {
4344 key_name[0] = KEY2TERMCAP0(key);
4345 key_name[1] = KEY2TERMCAP1(key);
4346 }
4347 else
4348 {
4349 key_name[0] = KS_KEY;
4350 key_name[1] = (key & 0xff);
4351 }
4352 add_termcode(key_name, string, FALSE);
4353 if (full_screen)
4354 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004355 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01004356 return NULL;
4357 }
4358
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004359 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 else
4362 {
4363 flags = options[opt_idx].flags;
4364#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004365 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004367 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004368 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004369 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004372 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004373 return set_string_option(opt_idx, string, opt_flags);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004374
4375 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
4376 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004378 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004380 int idx;
4381
4382 // Either we are given a string or we are setting option
4383 // to zero.
4384 for (idx = 0; string[idx] == '0'; ++idx)
4385 ;
4386 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004387 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004388 // There's another character after zeros or the string
4389 // is empty. In both cases, we are trying to set a
4390 // num option using a string.
4391 semsg(_(e_number_required_after_str_equal_str),
4392 name, string);
4393 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004394
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004397 if (flags & P_NUM)
4398 return set_num_option(opt_idx, varp, number,
4399 NULL, 0, opt_flags);
4400 else
4401 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004403
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004405 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406}
4407
4408/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004409 * Call set_option_value() and when an error is returned report it.
4410 */
4411 void
4412set_option_value_give_err(
4413 char_u *name,
4414 long number,
4415 char_u *string,
4416 int opt_flags) // OPT_LOCAL or 0 (both)
4417{
4418 char *errmsg = set_option_value(name, number, string, opt_flags);
4419
4420 if (errmsg != NULL)
4421 emsg(_(errmsg));
4422}
4423
4424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 * Get the terminal code for a terminal option.
4426 * Returns NULL when not found.
4427 */
4428 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004429get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430{
4431 int opt_idx;
4432 char_u *varp;
4433
4434 if (tname[0] != 't' || tname[1] != '_' ||
4435 tname[2] == NUL || tname[3] == NUL)
4436 return NULL;
4437 if ((opt_idx = findoption(tname)) >= 0)
4438 {
4439 varp = get_varp(&(options[opt_idx]));
4440 if (varp != NULL)
4441 varp = *(char_u **)(varp);
4442 return varp;
4443 }
4444 return find_termcode(tname + 2);
4445}
4446
4447 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004448get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449{
4450 int i;
4451
4452 i = findoption((char_u *)"hl");
4453 if (i >= 0)
4454 return options[i].def_val[VI_DEFAULT];
4455 return (char_u *)NULL;
4456}
4457
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004458 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004459get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004460{
4461 int i;
4462
4463 i = findoption((char_u *)"enc");
4464 if (i >= 0)
4465 return options[i].def_val[VI_DEFAULT];
4466 return (char_u *)NULL;
4467}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004468
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004469#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004470 int
4471is_option_allocated(char *name)
4472{
4473 int idx = findoption((char_u *)name);
4474
4475 return idx >= 0 && (options[idx].flags & P_ALLOCED);
4476}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004477#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004478
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004479#if defined(FEAT_EVAL) || defined(PROTO)
4480/*
4481 * Return TRUE if "name" is a string option.
4482 * Returns FALSE if option "name" does not exist.
4483 */
4484 int
4485is_string_option(char_u *name)
4486{
4487 int idx = findoption(name);
4488
4489 return idx >= 0 && (options[idx].flags & P_STRING);
4490}
4491#endif
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493/*
4494 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004495 * When "has_lt" is true there is a '<' before "*arg_arg".
4496 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 */
4498 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004499find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004501 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004503 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505 /*
4506 * Don't use get_special_key_code() for t_xx, we don't want it to call
4507 * add_termcap_entry().
4508 */
4509 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4510 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004511 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004513 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004515 key = find_special_key(&arg, &modifiers,
4516 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004517 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 key = 0;
4519 }
4520 return key;
4521}
4522
4523/*
4524 * if 'all' == 0: show changed options
4525 * if 'all' == 1: show all normal options
4526 * if 'all' == 2: show all terminal options
4527 */
4528 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004529showoptions(
4530 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004531 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532{
4533 struct vimoption *p;
4534 int col;
4535 int isterm;
4536 char_u *varp;
4537 struct vimoption **items;
4538 int item_count;
4539 int run;
4540 int row, rows;
4541 int cols;
4542 int i;
4543 int len;
4544
4545#define INC 20
4546#define GAP 3
4547
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004548 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (items == NULL)
4550 return;
4551
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004552 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004554 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004556 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004558 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004560 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
4562 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004563 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 * 1. display the short items
4565 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004566 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 */
4568 for (run = 1; run <= 2 && !got_int; ++run)
4569 {
4570 /*
4571 * collect the items in items[]
4572 */
4573 item_count = 0;
4574 for (p = &options[0]; p->fullname != NULL; p++)
4575 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004576 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004577 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004578 continue;
4579
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 varp = NULL;
4581 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004582 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
4584 if (p->indir != PV_NONE && !isterm)
4585 varp = get_varp_scope(p, opt_flags);
4586 }
4587 else
4588 varp = get_varp(p);
4589 if (varp != NULL
4590 && ((all == 2 && isterm)
4591 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004592 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004594 if (opt_flags & OPT_ONECOLUMN)
4595 len = Columns;
4596 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004597 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 else
4599 {
4600 option_value2string(p, opt_flags);
4601 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4602 }
4603 if ((len <= INC - GAP && run == 1) ||
4604 (len > INC - GAP && run == 2))
4605 items[item_count++] = p;
4606 }
4607 }
4608
4609 /*
4610 * display the items
4611 */
4612 if (run == 1)
4613 {
4614 cols = (Columns + GAP - 3) / INC;
4615 if (cols == 0)
4616 cols = 1;
4617 rows = (item_count + cols - 1) / cols;
4618 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004619 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 rows = item_count;
4621 for (row = 0; row < rows && !got_int; ++row)
4622 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004623 msg_putchar('\n'); // go to next line
4624 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 break;
4626 col = 0;
4627 for (i = row; i < item_count; i += rows)
4628 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004629 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 showoneopt(items[i], opt_flags);
4631 col += INC;
4632 }
4633 out_flush();
4634 ui_breakcheck();
4635 }
4636 }
4637 vim_free(items);
4638}
4639
4640/*
4641 * Return TRUE if option "p" has its default value.
4642 */
4643 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004644optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645{
4646 int dvi;
4647
4648 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004649 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004650 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004652 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004654 // the cast to long is required for Manx C, long_i is
4655 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004656 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004657 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4659}
4660
4661/*
4662 * showoneopt: show the value of one option
4663 * must not be called with a hidden option!
4664 */
4665 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004666showoneopt(
4667 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004668 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004670 char_u *varp;
4671 int save_silent = silent_mode;
4672
4673 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004674 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
4676 varp = get_varp_scope(p, opt_flags);
4677
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004678 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4680 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004681 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004683 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004685 msg_puts(" ");
4686 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 if (!(p->flags & P_BOOL))
4688 {
4689 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004690 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 option_value2string(p, opt_flags);
4692 msg_outtrans(NameBuff);
4693 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004694
4695 silent_mode = save_silent;
4696 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697}
4698
4699/*
4700 * Write modified options as ":set" commands to a file.
4701 *
4702 * There are three values for "opt_flags":
4703 * OPT_GLOBAL: Write global option values and fresh values of
4704 * buffer-local options (used for start of a session
4705 * file).
4706 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4707 * curwin (used for a vimrc file).
4708 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4709 * and local values for window-local options of
4710 * curwin. Local values are also written when at the
4711 * default value, because a modeline or autocommand
4712 * may have set them when doing ":edit file" and the
4713 * user has set them back at the default or fresh
4714 * value.
4715 * When "local_only" is TRUE, don't write fresh
4716 * values, only local values (for ":mkview").
4717 * (fresh value = value used for a new buffer or window for a local option).
4718 *
4719 * Return FAIL on error, OK otherwise.
4720 */
4721 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004722makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004725 char_u *varp; // currently used value
4726 char_u *varp_fresh; // local value
4727 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 char *cmd;
4729 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004730 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731
4732 /*
4733 * The options that don't have a default (terminal name, columns, lines)
4734 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004735 * Do the loop over "options[]" twice: once for options with the
4736 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004738 for (pri = 1; pri >= 0; --pri)
4739 {
4740 for (p = &options[0]; !istermoption(p); p++)
4741 if (!(p->flags & P_NO_MKRC)
4742 && !istermoption(p)
4743 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004745 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4747 continue;
4748
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004749 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4750 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4752 continue;
4753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004754 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004756 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 continue;
4758
Bram Moolenaard23b7142021-04-17 21:04:34 +02004759 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4760 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004761 continue;
4762
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 round = 2;
4764 if (p->indir != PV_NONE)
4765 {
4766 if (p->var == VAR_WIN)
4767 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004768 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 if (!(opt_flags & OPT_LOCAL))
4770 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004771 // When fresh value of window-local option is not at the
4772 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4774 {
4775 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004776 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
4778 round = 1;
4779 varp_local = varp;
4780 varp = varp_fresh;
4781 }
4782 }
4783 }
4784 }
4785
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004786 // Round 1: fresh value for window-local options.
4787 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 for ( ; round <= 2; varp = varp_local, ++round)
4789 {
4790 if (round == 1 || (opt_flags & OPT_GLOBAL))
4791 cmd = "set";
4792 else
4793 cmd = "setlocal";
4794
4795 if (p->flags & P_BOOL)
4796 {
4797 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4798 return FAIL;
4799 }
4800 else if (p->flags & P_NUM)
4801 {
4802 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4803 return FAIL;
4804 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004805 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004807 int do_endif = FALSE;
4808
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004809 // Don't set 'syntax' and 'filetype' again if the value is
4810 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004811 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004812#if defined(FEAT_SYN_HL)
4813 p->indir == PV_SYN ||
4814#endif
4815 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
4817 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4818 *(char_u **)(varp)) < 0
4819 || put_eol(fd) < 0)
4820 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004821 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
4823 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004824 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004826 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 {
4828 if (put_line(fd, "endif") == FAIL)
4829 return FAIL;
4830 }
4831 }
4832 }
4833 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 return OK;
4836}
4837
4838#if defined(FEAT_FOLDING) || defined(PROTO)
4839/*
4840 * Generate set commands for the local fold options only. Used when
4841 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4842 */
4843 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004844makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004846 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004848 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 == FAIL
4850# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004851 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004853 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 == FAIL
4855 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4856 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4857 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4858 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4859 )
4860 return FAIL;
4861
4862 return OK;
4863}
4864#endif
4865
4866 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004867put_setstring(
4868 FILE *fd,
4869 char *cmd,
4870 char *name,
4871 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004872 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873{
4874 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004875 char_u *buf = NULL;
4876 char_u *part = NULL;
4877 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
4879 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4880 return FAIL;
4881 if (*valuep != NULL)
4882 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004883 // Output 'pastetoggle' as key names. For other
4884 // options some characters have to be escaped with
4885 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 if (valuep == &p_pt)
4887 {
4888 s = *valuep;
4889 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01004890 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 return FAIL;
4892 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004893 // expand the option value, replace $HOME by ~
4894 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004896 int size = (int)STRLEN(*valuep) + 1;
4897
4898 // replace home directory in the whole option value into "buf"
4899 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004900 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004901 goto fail;
4902 home_replace(NULL, *valuep, buf, size, FALSE);
4903
4904 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004905 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004906 // can be expanded when read back.
4907 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4908 && vim_strchr(*valuep, ',') != NULL)
4909 {
4910 part = alloc(size);
4911 if (part == NULL)
4912 goto fail;
4913
4914 // write line break to clear the option, e.g. ':set rtp='
4915 if (put_eol(fd) == FAIL)
4916 goto fail;
4917
4918 p = buf;
4919 while (*p != NUL)
4920 {
4921 // for each comma separated option part, append value to
4922 // the option, :set rtp+=value
4923 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4924 goto fail;
4925 (void)copy_option_part(&p, part, size, ",");
4926 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4927 goto fail;
4928 }
4929 vim_free(buf);
4930 vim_free(part);
4931 return OK;
4932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004934 {
4935 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004937 }
4938 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940 else if (put_escstr(fd, *valuep, 2) == FAIL)
4941 return FAIL;
4942 }
4943 if (put_eol(fd) < 0)
4944 return FAIL;
4945 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004946fail:
4947 vim_free(buf);
4948 vim_free(part);
4949 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950}
4951
4952 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004953put_setnum(
4954 FILE *fd,
4955 char *cmd,
4956 char *name,
4957 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958{
4959 long wc;
4960
4961 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4962 return FAIL;
4963 if (wc_use_keyname((char_u *)valuep, &wc))
4964 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004965 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4967 return FAIL;
4968 }
4969 else if (fprintf(fd, "%ld", *valuep) < 0)
4970 return FAIL;
4971 if (put_eol(fd) < 0)
4972 return FAIL;
4973 return OK;
4974}
4975
4976 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004977put_setbool(
4978 FILE *fd,
4979 char *cmd,
4980 char *name,
4981 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004983 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004984 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4986 || put_eol(fd) < 0)
4987 return FAIL;
4988 return OK;
4989}
4990
4991/*
4992 * Clear all the terminal options.
4993 * If the option has been allocated, free the memory.
4994 * Terminal options are never hidden or indirect.
4995 */
4996 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004997clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 /*
5000 * Reset a few things before clearing the old options. This may cause
5001 * outputting a few things that the terminal doesn't understand, but the
5002 * screen will be cleared later, so this is OK.
5003 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005004 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005005 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005007 // When starting the GUI close the display opened for the clipboard.
5008 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 if (gui.starting)
5010 clear_xterm_clip();
5011#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005012 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005014 free_termoptions();
5015}
5016
5017 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005018free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005019{
5020 struct vimoption *p;
5021
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005022 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 if (istermoption(p))
5024 {
5025 if (p->flags & P_ALLOCED)
5026 free_string_option(*(char_u **)(p->var));
5027 if (p->flags & P_DEF_ALLOCED)
5028 free_string_option(p->def_val[VI_DEFAULT]);
5029 *(char_u **)(p->var) = empty_option;
5030 p->def_val[VI_DEFAULT] = empty_option;
5031 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005032#ifdef FEAT_EVAL
5033 // remember where the option was cleared
5034 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037 clear_termcodes();
5038}
5039
5040/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005041 * Free the string for one term option, if it was allocated.
5042 * Set the string to empty_option and clear allocated flag.
5043 * "var" points to the option value.
5044 */
5045 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005046free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005047{
5048 struct vimoption *p;
5049
5050 for (p = &options[0]; p->fullname != NULL; p++)
5051 if (p->var == var)
5052 {
5053 if (p->flags & P_ALLOCED)
5054 free_string_option(*(char_u **)(p->var));
5055 *(char_u **)(p->var) = empty_option;
5056 p->flags &= ~P_ALLOCED;
5057 break;
5058 }
5059}
5060
5061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 * Set the terminal option defaults to the current value.
5063 * Used after setting the terminal name.
5064 */
5065 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005066set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067{
5068 struct vimoption *p;
5069
5070 for (p = &options[0]; p->fullname != NULL; p++)
5071 {
5072 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5073 {
5074 if (p->flags & P_DEF_ALLOCED)
5075 {
5076 free_string_option(p->def_val[VI_DEFAULT]);
5077 p->flags &= ~P_DEF_ALLOCED;
5078 }
5079 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5080 if (p->flags & P_ALLOCED)
5081 {
5082 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005083 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 }
5085 }
5086 }
5087}
5088
5089/*
5090 * return TRUE if 'p' starts with 't_'
5091 */
5092 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005093istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094{
5095 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5096}
5097
Bram Moolenaardac13472019-09-16 21:06:21 +02005098/*
5099 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5100 */
5101 int
5102istermoption_idx(int opt_idx)
5103{
5104 return istermoption(&options[opt_idx]);
5105}
5106
Bram Moolenaar113e1072019-01-20 15:30:40 +01005107#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005109 * Unset local option value, similar to ":set opt<".
5110 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005111 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005112unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005113{
5114 struct vimoption *p;
5115 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005116 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005117
5118 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005119 if (opt_idx < 0)
5120 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005121 p = &(options[opt_idx]);
5122
5123 switch ((int)p->indir)
5124 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005125 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005126 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005127 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005128 break;
5129 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005130 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005131 break;
5132 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005133 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005134 break;
5135 case PV_AR:
5136 buf->b_p_ar = -1;
5137 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005138 case PV_BKC:
5139 clear_string_option(&buf->b_p_bkc);
5140 buf->b_bkc_flags = 0;
5141 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005142 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005143 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005144 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005145 case PV_TC:
5146 clear_string_option(&buf->b_p_tc);
5147 buf->b_tc_flags = 0;
5148 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005149 case PV_SISO:
5150 curwin->w_p_siso = -1;
5151 break;
5152 case PV_SO:
5153 curwin->w_p_so = -1;
5154 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005155# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005156 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005157 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005158 break;
5159 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005160 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005161 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005162# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005163 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005164 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005165 break;
5166 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005167 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005168 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005169# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005170 case PV_TSRFU:
5171 clear_string_option(&buf->b_p_tsrfu);
5172 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005173# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005174 case PV_FP:
5175 clear_string_option(&buf->b_p_fp);
5176 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005177# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005178 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005179 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005180 break;
5181 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005182 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005183 break;
5184 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005185 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005186 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005187# endif
5188# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005189 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005190 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005191 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005192# endif
5193# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005194 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005195 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005196 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005197# endif
5198# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01005199 case PV_SBR:
5200 clear_string_option(&((win_T *)from)->w_p_sbr);
5201 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005202# endif
5203# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005204 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005205 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005206 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005207# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005208 case PV_UL:
5209 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5210 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005211 case PV_LW:
5212 clear_string_option(&buf->b_p_lw);
5213 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005214 case PV_MENC:
5215 clear_string_option(&buf->b_p_menc);
5216 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005217 case PV_LCS:
5218 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005219 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005220 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005221 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005222 case PV_FCS:
5223 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005224 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005225 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005226 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005227 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005228 clear_string_option(&((win_T *)from)->w_p_ve);
5229 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005230 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005231 }
5232}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005233#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005234
5235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005237 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 */
5239 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005240get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005242 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
5244 if (p->var == VAR_WIN)
5245 return (char_u *)GLOBAL_WO(get_varp(p));
5246 return p->var;
5247 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005248 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 {
5250 switch ((int)p->indir)
5251 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005252 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005254 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5255 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5256 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005258 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5259 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5260 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005261 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005262 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005263 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005264 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5265 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005267 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5268 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005270 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5271 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005272#ifdef FEAT_COMPL_FUNC
5273 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5274#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005275#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5276 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5277#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005278#if defined(FEAT_CRYPT)
5279 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5280#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005281#ifdef FEAT_LINEBREAK
5282 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5283#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005284#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005285 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005286#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005287 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005288 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005289 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005290 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005291 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005292 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005293 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005294
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005296 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
5298 return get_varp(p);
5299}
5300
5301/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005302 * Get pointer to option variable at 'opt_idx', depending on local or global
5303 * scope.
5304 */
5305 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005306get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005307{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005308 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005309}
5310
5311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 * Get pointer to option variable.
5313 */
5314 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005315get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005317 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 if (p->var == NULL)
5319 return NULL;
5320
5321 switch ((int)p->indir)
5322 {
5323 case PV_NONE: return p->var;
5324
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005325 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005326 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005328 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005330 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005332 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005334 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005336 case PV_TC: return *curbuf->b_p_tc != NUL
5337 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005338 case PV_BKC: return *curbuf->b_p_bkc != NUL
5339 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005340 case PV_SISO: return curwin->w_p_siso >= 0
5341 ? (char_u *)&(curwin->w_p_siso) : p->var;
5342 case PV_SO: return curwin->w_p_so >= 0
5343 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005345 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005347 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5349#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005350 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005352 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005354#ifdef FEAT_COMPL_FUNC
5355 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5356 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5357#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005358 case PV_FP: return *curbuf->b_p_fp != NUL
5359 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005361 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005363 case PV_GP: return *curbuf->b_p_gp != NUL
5364 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5365 case PV_MP: return *curbuf->b_p_mp != NUL
5366 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005368#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5369 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5370 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5371#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005372#if defined(FEAT_CRYPT)
5373 case PV_CM: return *curbuf->b_p_cm != NUL
5374 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5375#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005376#ifdef FEAT_LINEBREAK
5377 case PV_SBR: return *curwin->w_p_sbr != NUL
5378 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5379#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005380#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005381 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005382 ? (char_u *)&(curwin->w_p_stl) : p->var;
5383#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005384 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5385 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005386 case PV_LW: return *curbuf->b_p_lw != NUL
5387 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005388 case PV_MENC: return *curbuf->b_p_menc != NUL
5389 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390#ifdef FEAT_ARABIC
5391 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5392#endif
5393 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005394 case PV_LCS: return *curwin->w_p_lcs != NUL
5395 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005396 case PV_FCS: return *curwin->w_p_fcs != NUL
5397 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005398 case PV_VE: return *curwin->w_p_ve != NUL
5399 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005400#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005401 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5402#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005403#ifdef FEAT_SYN_HL
5404 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5405 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005406 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005407 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409#ifdef FEAT_DIFF
5410 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5411#endif
5412#ifdef FEAT_FOLDING
5413 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5414 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5415 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5416 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5417 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5418 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5419 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5420# ifdef FEAT_EVAL
5421 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5422 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5423# endif
5424 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5425#endif
5426 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005427 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005428#ifdef FEAT_LINEBREAK
5429 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005432 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005433#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5435#endif
5436#ifdef FEAT_RIGHTLEFT
5437 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5438 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5439#endif
5440 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01005441 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5443#ifdef FEAT_LINEBREAK
5444 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005445 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5446 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005448 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005450 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005451#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005452 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5453 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5454#endif
5455#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005456 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5457 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5458 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
5461 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5462 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5465 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5467 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5469 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5470 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01005471 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474#ifdef FEAT_FOLDING
5475 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005478#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005479 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005481#ifdef FEAT_COMPL_FUNC
5482 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005483 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005484#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005485#ifdef FEAT_EVAL
5486 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5487#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01005488 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005490 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005496 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5498 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5499 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5500 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5501#ifdef FEAT_FIND_ID
5502# ifdef FEAT_EVAL
5503 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5504# endif
5505#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01005506#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5508 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5509#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005510#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005511 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513#ifdef FEAT_CRYPT
5514 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01005517 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5519 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5520 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5521 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5522 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005524 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5531#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005532 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005533 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5534#endif
5535#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005536 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5537 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5538 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005539 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540#endif
5541 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5542 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5543 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5544 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005545#ifdef FEAT_PERSISTENT_UNDO
5546 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5549#ifdef FEAT_KEYMAP
5550 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5551#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005552#ifdef FEAT_SIGNS
5553 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5554#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005555#ifdef FEAT_VARTABS
5556 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5557 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5558#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005559 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005561 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 return (char_u *)&(curbuf->b_p_wm);
5563}
5564
5565/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005566 * Return a pointer to the variable for option at 'opt_idx'
5567 */
5568 char_u *
5569get_option_var(int opt_idx)
5570{
5571 return options[opt_idx].var;
5572}
5573
Dominique Pelle748b3082022-01-08 12:41:16 +00005574#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005575/*
5576 * Return the full name of the option at 'opt_idx'
5577 */
5578 char_u *
5579get_option_fullname(int opt_idx)
5580{
5581 return (char_u *)options[opt_idx].fullname;
5582}
Dominique Pelle748b3082022-01-08 12:41:16 +00005583#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005584
5585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 * Get the value of 'equalprg', either the buffer-local one or the global one.
5587 */
5588 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005589get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
5591 if (*curbuf->b_p_ep == NUL)
5592 return p_ep;
5593 return curbuf->b_p_ep;
5594}
5595
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596/*
5597 * Copy options from one window to another.
5598 * Used when splitting a window.
5599 */
5600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005601win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602{
5603 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5604 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005605 after_copy_winopt(wp_to);
5606}
5607
5608/*
5609 * After copying window options: update variables depending on options.
5610 */
5611 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00005612after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005613{
5614#ifdef FEAT_LINEBREAK
5615 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005616#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005617#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005618 fill_culopt_flags(NULL, wp);
5619 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005620#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005621 set_chars_option(wp, &wp->w_p_lcs, TRUE);
5622 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005623}
5624
5625 static char_u *
5626copy_option_val(char_u *val)
5627{
5628 if (val == empty_option)
5629 return empty_option; // no need to allocate memory
5630 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
5633/*
5634 * Copy the options from one winopt_T to another.
5635 * Doesn't free the old option values in "to", use clear_winopt() for that.
5636 * The 'scroll' option is not copied, because it depends on the window height.
5637 * The 'previewwindow' option is reset, there can be only one preview window.
5638 */
5639 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005640copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
5642#ifdef FEAT_ARABIC
5643 to->wo_arab = from->wo_arab;
5644#endif
5645 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005646 to->wo_lcs = copy_option_val(from->wo_lcs);
5647 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005649 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005650 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02005651 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005652#ifdef FEAT_LINEBREAK
5653 to->wo_nuw = from->wo_nuw;
5654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655#ifdef FEAT_RIGHTLEFT
5656 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005657 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005659#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005660 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01005661#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005662#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005663 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005666#ifdef FEAT_DIFF
5667 to->wo_wrap_save = from->wo_wrap_save;
5668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669#ifdef FEAT_LINEBREAK
5670 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005671 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005672 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005674 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005676 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01005677 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005678 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005679 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005680#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005681 to->wo_spell = from->wo_spell;
5682#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005683#ifdef FEAT_SYN_HL
5684 to->wo_cuc = from->wo_cuc;
5685 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005686 to->wo_culopt = copy_option_val(from->wo_culopt);
5687 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689#ifdef FEAT_DIFF
5690 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005691 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005693#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005694 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005695 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005696#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005697#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005698 to->wo_twk = copy_option_val(from->wo_twk);
5699 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701#ifdef FEAT_FOLDING
5702 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005703 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005705 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005706 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 to->wo_fml = from->wo_fml;
5708 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005709 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005710 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005711 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005712 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 to->wo_fdn = from->wo_fdn;
5714# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005715 to->wo_fde = copy_option_val(from->wo_fde);
5716 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005718 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005720#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005721 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005722#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005723
5724#ifdef FEAT_EVAL
5725 // Copy the script context so that we know where the value was last set.
5726 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5727 sizeof(to->wo_script_ctx));
5728#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005729 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730}
5731
5732/*
5733 * Check string options in a window for a NULL value.
5734 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005735 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005736check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
5738 check_winopt(&win->w_onebuf_opt);
5739 check_winopt(&win->w_allbuf_opt);
5740}
5741
5742/*
5743 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5744 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005745 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005746check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
5748#ifdef FEAT_FOLDING
5749 check_string_option(&wop->wo_fdi);
5750 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005751 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752# ifdef FEAT_EVAL
5753 check_string_option(&wop->wo_fde);
5754 check_string_option(&wop->wo_fdt);
5755# endif
5756 check_string_option(&wop->wo_fmr);
5757#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005758#ifdef FEAT_SIGNS
5759 check_string_option(&wop->wo_scl);
5760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761#ifdef FEAT_RIGHTLEFT
5762 check_string_option(&wop->wo_rlc);
5763#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005764#ifdef FEAT_LINEBREAK
5765 check_string_option(&wop->wo_sbr);
5766#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005767#ifdef FEAT_STL_OPT
5768 check_string_option(&wop->wo_stl);
5769#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005770#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005771 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005772 check_string_option(&wop->wo_cc);
5773#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005774#ifdef FEAT_CONCEAL
5775 check_string_option(&wop->wo_cocu);
5776#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005777#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005778 check_string_option(&wop->wo_twk);
5779 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005780#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005781#ifdef FEAT_LINEBREAK
5782 check_string_option(&wop->wo_briopt);
5783#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005784 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005785 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005786 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005787 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788}
5789
5790/*
5791 * Free the allocated memory inside a winopt_T.
5792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005794clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
5796#ifdef FEAT_FOLDING
5797 clear_string_option(&wop->wo_fdi);
5798 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005799 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800# ifdef FEAT_EVAL
5801 clear_string_option(&wop->wo_fde);
5802 clear_string_option(&wop->wo_fdt);
5803# endif
5804 clear_string_option(&wop->wo_fmr);
5805#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005806#ifdef FEAT_SIGNS
5807 clear_string_option(&wop->wo_scl);
5808#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005809#ifdef FEAT_LINEBREAK
5810 clear_string_option(&wop->wo_briopt);
5811#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005812 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813#ifdef FEAT_RIGHTLEFT
5814 clear_string_option(&wop->wo_rlc);
5815#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005816#ifdef FEAT_LINEBREAK
5817 clear_string_option(&wop->wo_sbr);
5818#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005819#ifdef FEAT_STL_OPT
5820 clear_string_option(&wop->wo_stl);
5821#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005822#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005823 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005824 clear_string_option(&wop->wo_cc);
5825#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005826#ifdef FEAT_CONCEAL
5827 clear_string_option(&wop->wo_cocu);
5828#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005829#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005830 clear_string_option(&wop->wo_twk);
5831 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005832#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005833 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005834 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005835 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836}
5837
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005838#ifdef FEAT_EVAL
5839// Index into the options table for a buffer-local option enum.
5840static int buf_opt_idx[BV_COUNT];
5841# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5842
5843/*
5844 * Initialize buf_opt_idx[] if not done already.
5845 */
5846 static void
5847init_buf_opt_idx(void)
5848{
5849 static int did_init_buf_opt_idx = FALSE;
5850 int i;
5851
5852 if (did_init_buf_opt_idx)
5853 return;
5854 did_init_buf_opt_idx = TRUE;
5855 for (i = 0; !istermoption_idx(i); i++)
5856 if (options[i].indir & PV_BUF)
5857 buf_opt_idx[options[i].indir & PV_MASK] = i;
5858}
5859#else
5860# define COPY_OPT_SCTX(buf, bv)
5861#endif
5862
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863/*
5864 * Copy global option values to local options for one buffer.
5865 * Used when creating a new buffer and sometimes when entering a buffer.
5866 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005867 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5869 * appropriate.
5870 * BCO_NOHELP Don't copy the values to a help buffer.
5871 */
5872 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005873buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874{
5875 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005876 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 int dont_do_help;
5878 int did_isk = FALSE;
5879
5880 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 * Skip this when the option defaults have not been set yet. Happens when
5882 * main() allocates the first buffer.
5883 */
5884 if (p_cpo != NULL)
5885 {
5886 /*
5887 * Always copy when entering and 'cpo' contains 'S'.
5888 * Don't copy when already initialized.
5889 * Don't copy when 'cpo' contains 's' and not entering.
5890 * 'S' BCO_ENTER initialized 's' should_copy
5891 * yes yes X X TRUE
5892 * yes no yes X FALSE
5893 * no X yes X FALSE
5894 * X no no yes FALSE
5895 * X no no no TRUE
5896 * no yes no X TRUE
5897 */
5898 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5899 && (buf->b_p_initialized
5900 || (!(flags & BCO_ENTER)
5901 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5902 should_copy = FALSE;
5903
5904 if (should_copy || (flags & BCO_ALWAYS))
5905 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005906#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005907 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005908 init_buf_opt_idx();
5909#endif
5910 // Don't copy the options specific to a help buffer when
5911 // BCO_NOHELP is given or the options were initialized already
5912 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5914 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005915 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 {
5917 save_p_isk = buf->b_p_isk;
5918 buf->b_p_isk = NULL;
5919 }
5920 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005921 * Always free the allocated strings. If not already initialized,
5922 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 */
5924 if (!buf->b_p_initialized)
5925 {
5926 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005927 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005930 switch (*p_ffs)
5931 {
5932 case 'm':
5933 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5934 case 'd':
5935 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5936 case 'u':
5937 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5938 default:
5939 buf->b_p_ff = vim_strsave(p_ff);
5940 }
5941 if (buf->b_p_ff != NULL)
5942 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 buf->b_p_bh = empty_option;
5944 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 }
5946 else
5947 free_buf_options(buf, FALSE);
5948
5949 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005950 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 buf->b_p_ai_nopaste = p_ai_nopaste;
5952 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005953 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005955 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 buf->b_p_tw_nopaste = p_tw_nopaste;
5957 buf->b_p_tw_nobin = p_tw_nobin;
5958 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005959 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 buf->b_p_wm_nopaste = p_wm_nopaste;
5961 buf->b_p_wm_nobin = p_wm_nobin;
5962 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005963 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005964 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005965 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005966 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005967 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005969 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005971 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005973 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 buf->b_p_ml_nobin = p_ml_nobin;
5975 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005976 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005977 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005978 buf->b_p_swf = FALSE;
5979 else
5980 {
5981 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00005982 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005985 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005986#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005987 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005988 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005990#ifdef FEAT_COMPL_FUNC
5991 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005992 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005993 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005994 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005995 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005996 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005997#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005998#ifdef FEAT_EVAL
5999 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006000 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006001 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006004 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006006#ifdef FEAT_VARTABS
6007 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006008 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006009 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006010 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006011 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006012 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006013 buf->b_p_vsts_nopaste = p_vsts_nopaste
6014 ? vim_strsave(p_vsts_nopaste) : NULL;
6015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006017 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006019 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020#ifdef FEAT_FOLDING
6021 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006022 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023#endif
6024 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006025 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006026 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006027 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006028 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6029 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006031 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006033 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006035 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006037 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006038
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006040 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006042 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006044 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006045 buf->b_p_cinsd = vim_strsave(p_cinsd);
6046 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006047 buf->b_p_lop = vim_strsave(p_lop);
6048 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006049
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006050 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006053 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006055 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006057 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006059 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006061 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006062 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006063 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006064#endif
6065#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006066 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006067 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006068 (void)compile_cap_prog(&buf->b_s);
6069 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006070 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006071 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006072 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006073 buf->b_s.b_p_spo = vim_strsave(p_spo);
6074 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006076#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006078 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006080 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006082 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006083#if defined(FEAT_EVAL)
6084 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006085 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087#ifdef FEAT_CRYPT
6088 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006089 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006092 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093#ifdef FEAT_KEYMAP
6094 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006095 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 buf->b_kmap_state |= KEYMAP_INIT;
6097#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006098#ifdef FEAT_TERMINAL
6099 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006100 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006101#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006102 // This isn't really an option, but copying the langmap and IME
6103 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006105 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006107 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006109 // options that are normally global but also have a local value
6110 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006112 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006113 buf->b_p_bkc = empty_option;
6114 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115#ifdef FEAT_QUICKFIX
6116 buf->b_p_gp = empty_option;
6117 buf->b_p_mp = empty_option;
6118 buf->b_p_efm = empty_option;
6119#endif
6120 buf->b_p_ep = empty_option;
6121 buf->b_p_kp = empty_option;
6122 buf->b_p_path = empty_option;
6123 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006124 buf->b_p_tc = empty_option;
6125 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126#ifdef FEAT_FIND_ID
6127 buf->b_p_def = empty_option;
6128 buf->b_p_inc = empty_option;
6129# ifdef FEAT_EVAL
6130 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006131 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132# endif
6133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 buf->b_p_dict = empty_option;
6135 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006136#ifdef FEAT_COMPL_FUNC
6137 buf->b_p_tsrfu = empty_option;
6138#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006139 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006140 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006141#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6142 buf->b_p_bexpr = empty_option;
6143#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006144#if defined(FEAT_CRYPT)
6145 buf->b_p_cm = empty_option;
6146#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006147#ifdef FEAT_PERSISTENT_UNDO
6148 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006149 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006150#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006151 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006152 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
6154 /*
6155 * Don't copy the options set by ex_help(), use the saved values,
6156 * when going from a help buffer to a non-help buffer.
6157 * Don't touch these at all when BCO_NOHELP is used and going from
6158 * or to a help buffer.
6159 */
6160 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006163#ifdef FEAT_VARTABS
6164 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006165 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006166 else
6167 buf->b_p_vts_array = NULL;
6168#endif
6169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 else
6171 {
6172 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006173 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 did_isk = TRUE;
6175 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006176 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006177#ifdef FEAT_VARTABS
6178 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006179 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006180 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006181 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006182 else
6183 buf->b_p_vts_array = NULL;
6184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 if (buf->b_p_bt[0] == 'h')
6187 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006189 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 }
6191 }
6192
6193 /*
6194 * When the options should be copied (ignoring BCO_ALWAYS), set the
6195 * flag that indicates that the options have been initialized.
6196 */
6197 if (should_copy)
6198 buf->b_p_initialized = TRUE;
6199 }
6200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006201 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 if (did_isk)
6203 (void)buf_init_chartab(buf, FALSE);
6204}
6205
6206/*
6207 * Reset the 'modifiable' option and its default value.
6208 */
6209 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006210reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
6212 int opt_idx;
6213
6214 curbuf->b_p_ma = FALSE;
6215 p_ma = FALSE;
6216 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006217 if (opt_idx >= 0)
6218 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219}
6220
6221/*
6222 * Set the global value for 'iminsert' to the local value.
6223 */
6224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006225set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226{
6227 p_iminsert = curbuf->b_p_iminsert;
6228}
6229
6230/*
6231 * Set the global value for 'imsearch' to the local value.
6232 */
6233 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006234set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 p_imsearch = curbuf->b_p_imsearch;
6237}
6238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239static int expand_option_idx = -1;
6240static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6241static int expand_option_flags = 0;
6242
6243 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006244set_context_in_set_cmd(
6245 expand_T *xp,
6246 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006247 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248{
6249 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006250 long_u flags = 0; // init for GCC
6251 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 char_u *p;
6253 char_u *s;
6254 int is_term_option = FALSE;
6255 int key;
6256
6257 expand_option_flags = opt_flags;
6258
6259 xp->xp_context = EXPAND_SETTINGS;
6260 if (*arg == NUL)
6261 {
6262 xp->xp_pattern = arg;
6263 return;
6264 }
6265 p = arg + STRLEN(arg) - 1;
6266 if (*p == ' ' && *(p - 1) != '\\')
6267 {
6268 xp->xp_pattern = p + 1;
6269 return;
6270 }
6271 while (p > arg)
6272 {
6273 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006274 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 if (*p == ' ' || *p == ',')
6276 {
6277 while (s > arg && *(s - 1) == '\\')
6278 --s;
6279 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006280 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 if (*p == ' ' && ((p - s) & 1) == 0)
6282 {
6283 ++p;
6284 break;
6285 }
6286 --p;
6287 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006288 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 {
6290 xp->xp_context = EXPAND_BOOL_SETTINGS;
6291 p += 2;
6292 }
6293 if (STRNCMP(p, "inv", 3) == 0)
6294 {
6295 xp->xp_context = EXPAND_BOOL_SETTINGS;
6296 p += 3;
6297 }
6298 xp->xp_pattern = arg = p;
6299 if (*arg == '<')
6300 {
6301 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006302 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 return;
6304 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006305 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
6307 xp->xp_context = EXPAND_NOTHING;
6308 return;
6309 }
6310 nextchar = *++p;
6311 is_term_option = TRUE;
6312 expand_option_name[2] = KEY2TERMCAP0(key);
6313 expand_option_name[3] = KEY2TERMCAP1(key);
6314 }
6315 else
6316 {
6317 if (p[0] == 't' && p[1] == '_')
6318 {
6319 p += 2;
6320 if (*p != NUL)
6321 ++p;
6322 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006323 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 nextchar = *++p;
6325 is_term_option = TRUE;
6326 expand_option_name[2] = p[-2];
6327 expand_option_name[3] = p[-1];
6328 }
6329 else
6330 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006331 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6333 p++;
6334 if (*p == NUL)
6335 return;
6336 nextchar = *p;
6337 *p = NUL;
6338 opt_idx = findoption(arg);
6339 *p = nextchar;
6340 if (opt_idx == -1 || options[opt_idx].var == NULL)
6341 {
6342 xp->xp_context = EXPAND_NOTHING;
6343 return;
6344 }
6345 flags = options[opt_idx].flags;
6346 if (flags & P_BOOL)
6347 {
6348 xp->xp_context = EXPAND_NOTHING;
6349 return;
6350 }
6351 }
6352 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006353 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6355 {
6356 ++p;
6357 nextchar = '=';
6358 }
6359 if ((nextchar != '=' && nextchar != ':')
6360 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6361 {
6362 xp->xp_context = EXPAND_UNSUCCESSFUL;
6363 return;
6364 }
6365 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6366 {
6367 xp->xp_context = EXPAND_OLD_SETTING;
6368 if (is_term_option)
6369 expand_option_idx = -1;
6370 else
6371 expand_option_idx = opt_idx;
6372 xp->xp_pattern = p + 1;
6373 return;
6374 }
6375 xp->xp_context = EXPAND_NOTHING;
6376 if (is_term_option || (flags & P_NUM))
6377 return;
6378
6379 xp->xp_pattern = p + 1;
6380
6381 if (flags & P_EXPAND)
6382 {
6383 p = options[opt_idx].var;
6384 if (p == (char_u *)&p_bdir
6385 || p == (char_u *)&p_dir
6386 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006387 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390#ifdef FEAT_SESSION
6391 || p == (char_u *)&p_vdir
6392#endif
6393 )
6394 {
6395 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006396 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 xp->xp_backslash = XP_BS_THREE;
6398 else
6399 xp->xp_backslash = XP_BS_ONE;
6400 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006401 else if (p == (char_u *)&p_ft)
6402 {
6403 xp->xp_context = EXPAND_FILETYPE;
6404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 else
6406 {
6407 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006408 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 if (p == (char_u *)&p_tags)
6410 xp->xp_backslash = XP_BS_THREE;
6411 else
6412 xp->xp_backslash = XP_BS_ONE;
6413 }
6414 }
6415
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006416 // For an option that is a list of file names, find the start of the
6417 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6419 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006420 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 if (*p == ' ' || *p == ',')
6422 {
6423 s = p;
6424 while (s > xp->xp_pattern && *(s - 1) == '\\')
6425 --s;
6426 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6427 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6428 {
6429 xp->xp_pattern = p + 1;
6430 break;
6431 }
6432 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006433
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006434#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006435 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006436 if (options[opt_idx].var == (char_u *)&p_sps
6437 && STRNCMP(p, "file:", 5) == 0)
6438 {
6439 xp->xp_pattern = p + 5;
6440 break;
6441 }
6442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444}
6445
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006446/*
6447 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
6448 *
6449 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
6450 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
6451 *
6452 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
6453 * regular expression 'regmatch', then stores the match in matches[idx] and
6454 * returns TRUE.
6455 *
6456 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6457 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
6458 *
6459 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6460 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
6461 */
6462 static int
6463match_str(
6464 char_u *str,
6465 regmatch_T *regmatch,
6466 char_u **matches,
6467 int idx,
6468 int test_only,
6469 int fuzzy,
6470 char_u *fuzzystr,
6471 fuzmatch_str_T *fuzmatch)
6472{
6473 if (!fuzzy)
6474 {
6475 if (vim_regexec(regmatch, str, (colnr_T)0))
6476 {
6477 if (!test_only)
6478 matches[idx] = vim_strsave(str);
6479 return TRUE;
6480 }
6481 }
6482 else
6483 {
6484 int score;
6485
6486 score = fuzzy_match_str(str, fuzzystr);
6487 if (score != 0)
6488 {
6489 if (!test_only)
6490 {
6491 fuzmatch[idx].idx = idx;
6492 fuzmatch[idx].str = vim_strsave(str);
6493 fuzmatch[idx].score = score;
6494 }
6495
6496 return TRUE;
6497 }
6498 }
6499
6500 return FALSE;
6501}
6502
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006504ExpandSettings(
6505 expand_T *xp,
6506 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006507 char_u *fuzzystr,
6508 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01006509 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006510 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006512 int num_normal = 0; // Nr of matching non-term-code settings
6513 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 int opt_idx;
6515 int match;
6516 int count = 0;
6517 char_u *str;
6518 int loop;
6519 int is_term_opt;
6520 char_u name_buf[MAX_KEY_NAME_LEN];
6521 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006522 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006523 int fuzzy;
6524 fuzmatch_str_T *fuzmatch = NULL;
6525
Christian Brabandtcb747892022-05-08 21:10:56 +01006526 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006528 // do this loop twice:
6529 // loop == 0: count the number of matching options
6530 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 for (loop = 0; loop <= 1; ++loop)
6532 {
6533 regmatch->rm_ic = ic;
6534 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6535 {
K.Takataeeec2542021-06-02 13:28:16 +02006536 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006537 {
6538 if (match_str((char_u *)names[match], regmatch, *matches,
6539 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 {
6541 if (loop == 0)
6542 num_normal++;
6543 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006544 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 }
6548 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6549 opt_idx++)
6550 {
6551 if (options[opt_idx].var == NULL)
6552 continue;
6553 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6554 && !(options[opt_idx].flags & P_BOOL))
6555 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006556 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 if (is_term_opt && num_normal > 0)
6558 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006559
6560 if (match_str(str, regmatch, *matches, count, (loop == 0),
6561 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 {
6563 if (loop == 0)
6564 {
6565 if (is_term_opt)
6566 num_term++;
6567 else
6568 num_normal++;
6569 }
6570 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006571 count++;
6572 }
6573 else if (!fuzzy && options[opt_idx].shortname != NULL
6574 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006575 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006576 {
6577 // Compare against the abbreviated option name (for regular
6578 // expression match). Fuzzy matching (previous if) already
6579 // matches against both the expanded and abbreviated names.
6580 if (loop == 0)
6581 {
6582 if (is_term_opt)
6583 num_term++;
6584 else
6585 num_normal++;
6586 }
6587 else
6588 (*matches)[count++] = vim_strsave(str);
6589 }
6590 else if (is_term_opt)
6591 {
6592 name_buf[0] = '<';
6593 name_buf[1] = 't';
6594 name_buf[2] = '_';
6595 name_buf[3] = str[2];
6596 name_buf[4] = str[3];
6597 name_buf[5] = '>';
6598 name_buf[6] = NUL;
6599
6600 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6601 fuzzy, fuzzystr, fuzmatch))
6602 {
6603 if (loop == 0)
6604 num_term++;
6605 else
6606 count++;
6607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 }
6609 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006610
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 /*
6612 * Check terminal key codes, these are not in the option table
6613 */
6614 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6615 {
6616 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6617 {
6618 if (!isprint(str[0]) || !isprint(str[1]))
6619 continue;
6620
6621 name_buf[0] = 't';
6622 name_buf[1] = '_';
6623 name_buf[2] = str[0];
6624 name_buf[3] = str[1];
6625 name_buf[4] = NUL;
6626
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006627 if (match_str(name_buf, regmatch, *matches, count,
6628 (loop == 0), fuzzy, fuzzystr, fuzmatch))
6629 {
6630 if (loop == 0)
6631 num_term++;
6632 else
6633 count++;
6634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 else
6636 {
6637 name_buf[0] = '<';
6638 name_buf[1] = 't';
6639 name_buf[2] = '_';
6640 name_buf[3] = str[0];
6641 name_buf[4] = str[1];
6642 name_buf[5] = '>';
6643 name_buf[6] = NUL;
6644
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006645 if (match_str(name_buf, regmatch, *matches, count,
6646 (loop == 0), fuzzy, fuzzystr,
6647 fuzmatch))
6648 {
6649 if (loop == 0)
6650 num_term++;
6651 else
6652 count++;
6653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 }
6655 }
6656
6657 /*
6658 * Check special key names.
6659 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006660 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6662 {
6663 name_buf[0] = '<';
6664 STRCPY(name_buf + 1, str);
6665 STRCAT(name_buf, ">");
6666
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006667 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6668 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 {
6670 if (loop == 0)
6671 num_term++;
6672 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006673 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 }
6675 }
6676 }
6677 if (loop == 0)
6678 {
6679 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006680 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006682 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 else
6684 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006685 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006687 *matches = ALLOC_MULT(char_u *, *numMatches);
6688 if (*matches == NULL)
6689 {
6690 *matches = (char_u **)"";
6691 return FAIL;
6692 }
6693 }
6694 else
6695 {
6696 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
6697 if (fuzmatch == NULL)
6698 {
6699 *matches = (char_u **)"";
6700 return FAIL;
6701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 }
6703 }
6704 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006705
6706 if (fuzzy &&
6707 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
6708 return FAIL;
6709
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 return OK;
6711}
6712
6713 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006714ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006716 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 char_u *buf;
6718
6719 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006720 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 if (*file == NULL)
6722 return FAIL;
6723
6724 /*
6725 * For a terminal key code expand_option_idx is < 0.
6726 */
6727 if (expand_option_idx < 0)
6728 {
6729 var = find_termcode(expand_option_name + 2);
6730 if (var == NULL)
6731 expand_option_idx = findoption(expand_option_name);
6732 }
6733
6734 if (expand_option_idx >= 0)
6735 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006736 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 option_value2string(&options[expand_option_idx], expand_option_flags);
6738 var = NameBuff;
6739 }
6740 else if (var == NULL)
6741 var = (char_u *)"";
6742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006743 // A backslash is required before some characters. This is the reverse of
6744 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 buf = vim_strsave_escaped(var, escape_chars);
6746
6747 if (buf == NULL)
6748 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006749 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 return FAIL;
6751 }
6752
6753#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006754 // For MS-Windows et al. we don't double backslashes at the start and
6755 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006756 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 if (var[0] == '\\' && var[1] == '\\'
6758 && expand_option_idx >= 0
6759 && (options[expand_option_idx].flags & P_EXPAND)
6760 && vim_isfilec(var[2])
6761 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006762 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763#endif
6764
6765 *file[0] = buf;
6766 *num_file = 1;
6767 return OK;
6768}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769
6770/*
6771 * Get the value for the numeric or string option *opp in a nice format into
6772 * NameBuff[]. Must not be called with a hidden option!
6773 */
6774 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006775option_value2string(
6776 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006777 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
6779 char_u *varp;
6780
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006781 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
6783 if (opp->flags & P_NUM)
6784 {
6785 long wc = 0;
6786
6787 if (wc_use_keyname(varp, &wc))
6788 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6789 else if (wc != 0)
6790 STRCPY(NameBuff, transchar((int)wc));
6791 else
6792 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6793 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006794 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 {
6796 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006797 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 NameBuff[0] = NUL;
6799#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006800 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006801 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 STRCPY(NameBuff, "*****");
6803#endif
6804 else if (opp->flags & P_EXPAND)
6805 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006806 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 else if ((char_u **)opp->var == &p_pt)
6808 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6809 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006810 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
6812}
6813
6814/*
6815 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6816 * printed as a keyname.
6817 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6818 */
6819 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006820wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
6822 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6823 {
6824 *wcp = *(long *)varp;
6825 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6826 return TRUE;
6827 }
6828 return FALSE;
6829}
6830
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 * Return TRUE if "x" is present in 'shortmess' option, or
6833 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6834 */
6835 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006836shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006838 return p_shm != NULL &&
6839 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 || (vim_strchr(p_shm, 'a') != NULL
6841 && vim_strchr((char_u *)SHM_A, x) != NULL));
6842}
6843
6844/*
6845 * paste_option_changed() - Called after p_paste was set or reset.
6846 */
6847 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006848paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850 static int old_p_paste = FALSE;
6851 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006852 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854#ifdef FEAT_RIGHTLEFT
6855 static int save_ri = 0;
6856 static int save_hkmap = 0;
6857#endif
6858 buf_T *buf;
6859
6860 if (p_paste)
6861 {
6862 /*
6863 * Paste switched from off to on.
6864 * Save the current values, so they can be restored later.
6865 */
6866 if (!old_p_paste)
6867 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006868 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006869 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 {
6871 buf->b_p_tw_nopaste = buf->b_p_tw;
6872 buf->b_p_wm_nopaste = buf->b_p_wm;
6873 buf->b_p_sts_nopaste = buf->b_p_sts;
6874 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006875 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006876#ifdef FEAT_VARTABS
6877 if (buf->b_p_vsts_nopaste)
6878 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006879 buf->b_p_vsts_nopaste =
6880 buf->b_p_vsts && buf->b_p_vsts != empty_option
6881 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 }
6884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006885 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006887 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889#ifdef FEAT_RIGHTLEFT
6890 save_ri = p_ri;
6891 save_hkmap = p_hkmap;
6892#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006893 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006894 p_ai_nopaste = p_ai;
6895 p_et_nopaste = p_et;
6896 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 p_tw_nopaste = p_tw;
6898 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006899#ifdef FEAT_VARTABS
6900 if (p_vsts_nopaste)
6901 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006902 p_vsts_nopaste = p_vsts && p_vsts != empty_option
6903 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 }
6906
6907 /*
6908 * Always set the option values, also when 'paste' is set when it is
6909 * already on.
6910 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006911 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006912 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006914 buf->b_p_tw = 0; // textwidth is 0
6915 buf->b_p_wm = 0; // wrapmargin is 0
6916 buf->b_p_sts = 0; // softtabstop is 0
6917 buf->b_p_ai = 0; // no auto-indent
6918 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006919#ifdef FEAT_VARTABS
6920 if (buf->b_p_vsts)
6921 free_string_option(buf->b_p_vsts);
6922 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006923 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 }
6926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006927 // set global options
6928 p_sm = 0; // no showmatch
6929 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006931 status_redraw_all(); // redraw to remove the ruler
6932 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006934 p_ri = 0; // no reverse insert
6935 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006937 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 p_tw = 0;
6939 p_wm = 0;
6940 p_sts = 0;
6941 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006942#ifdef FEAT_VARTABS
6943 if (p_vsts)
6944 free_string_option(p_vsts);
6945 p_vsts = empty_option;
6946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 }
6948
6949 /*
6950 * Paste switched from on to off: Restore saved values.
6951 */
6952 else if (old_p_paste)
6953 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006954 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006955 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {
6957 buf->b_p_tw = buf->b_p_tw_nopaste;
6958 buf->b_p_wm = buf->b_p_wm_nopaste;
6959 buf->b_p_sts = buf->b_p_sts_nopaste;
6960 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006961 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006962#ifdef FEAT_VARTABS
6963 if (buf->b_p_vsts)
6964 free_string_option(buf->b_p_vsts);
6965 buf->b_p_vsts = buf->b_p_vsts_nopaste
6966 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006967 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006968 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006969 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006970 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006971 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 }
6974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006975 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006977 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006979 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981#ifdef FEAT_RIGHTLEFT
6982 p_ri = save_ri;
6983 p_hkmap = save_hkmap;
6984#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006985 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006986 p_ai = p_ai_nopaste;
6987 p_et = p_et_nopaste;
6988 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 p_tw = p_tw_nopaste;
6990 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006991#ifdef FEAT_VARTABS
6992 if (p_vsts)
6993 free_string_option(p_vsts);
6994 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 }
6997
6998 old_p_paste = p_paste;
6999}
7000
7001/*
7002 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7003 *
7004 * Reset 'compatible' and set the values for options that didn't get set yet
7005 * to the Vim defaults.
7006 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007007 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 */
7009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007010vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007012 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007013 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007014 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
7016 if (!option_was_set((char_u *)"cp"))
7017 {
7018 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007019 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7021 set_option_default(opt_idx, OPT_FREE, FALSE);
7022 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007023 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007025
7026 if (fname != NULL)
7027 {
7028 p = vim_getenv(envname, &dofree);
7029 if (p == NULL)
7030 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007031 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007032 p = FullName_save(fname, FALSE);
7033 if (p != NULL)
7034 {
7035 vim_setenv(envname, p);
7036 vim_free(p);
7037 }
7038 }
7039 else if (dofree)
7040 vim_free(p);
7041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042}
7043
7044/*
7045 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7046 */
7047 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007048change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007050 int opt_idx;
7051
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 if (p_cp != on)
7053 {
7054 p_cp = on;
7055 compatible_set();
7056 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007057 opt_idx = findoption((char_u *)"cp");
7058 if (opt_idx >= 0)
7059 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060}
7061
7062/*
7063 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007064 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 */
7066 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007067option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
7069 int idx;
7070
7071 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007072 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 return FALSE;
7074 if (options[idx].flags & P_WAS_SET)
7075 return TRUE;
7076 return FALSE;
7077}
7078
7079/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007080 * Reset the flag indicating option "name" was set.
7081 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007082 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007083reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007084{
7085 int idx = findoption(name);
7086
7087 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007088 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007089 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007090 return OK;
7091 }
7092 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007093}
7094
7095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 * compatible_set() - Called when 'compatible' has been set or unset.
7097 *
7098 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7099 * flag) to a Vi compatible value.
7100 * When 'compatible' is unset: Set all options that have a different default
7101 * for Vim (without the P_VI_DEF flag) to that default.
7102 */
7103 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007104compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 int opt_idx;
7107
Bram Moolenaardac13472019-09-16 21:06:21 +02007108 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7110 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7111 set_option_default(opt_idx, OPT_FREE, p_cp);
7112 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007113 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114}
7115
Bram Moolenaardac13472019-09-16 21:06:21 +02007116#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118/*
7119 * fill_breakat_flags() -- called when 'breakat' changes value.
7120 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007121 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007122fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007124 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 int i;
7126
7127 for (i = 0; i < 256; i++)
7128 breakat_flags[i] = FALSE;
7129
7130 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007131 for (p = p_breakat; *p; p++)
7132 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134#endif
7135
7136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 * Check if backspacing over something is allowed.
7138 */
7139 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007140can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007141 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007143#ifdef FEAT_JOB_CHANNEL
7144 if (what == BS_START && bt_prompt(curbuf))
7145 return FALSE;
7146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 switch (*p_bs)
7148 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007149 case '3': return TRUE;
7150 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 case '1': return (what != BS_START);
7152 case '0': return FALSE;
7153 }
7154 return vim_strchr(p_bs, what) != NULL;
7155}
7156
7157/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007158 * Return the effective 'scrolloff' value for the current window, using the
7159 * global value when appropriate.
7160 */
7161 long
7162get_scrolloff_value(void)
7163{
7164 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7165}
7166
7167/*
7168 * Return the effective 'sidescrolloff' value for the current window, using the
7169 * global value when appropriate.
7170 */
7171 long
7172get_sidescrolloff_value(void)
7173{
7174 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7175}
7176
7177/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007178 * Get the local or global value of 'backupcopy'.
7179 */
7180 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007181get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007182{
7183 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7184}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007185
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007186#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007187/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007188 * Get the local or global value of 'formatlistpat'.
7189 */
7190 char_u *
7191get_flp_value(buf_T *buf)
7192{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007193 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7194 return p_flp;
7195 return buf->b_p_flp;
7196}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007197#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007198
7199/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007200 * Get the local or global value of the 'virtualedit' flags.
7201 */
7202 unsigned int
7203get_ve_flags(void)
7204{
Gary Johnson51ad8502021-08-03 18:33:08 +02007205 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7206 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007207}
7208
Bram Moolenaaree857022019-11-09 23:26:40 +01007209#if defined(FEAT_LINEBREAK) || defined(PROTO)
7210/*
7211 * Get the local or global value of 'showbreak'.
7212 */
7213 char_u *
7214get_showbreak_value(win_T *win)
7215{
7216 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7217 return p_sbr;
7218 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7219 return empty_option;
7220 return win->w_p_sbr;
7221}
7222#endif
7223
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007224#if defined(FEAT_EVAL) || defined(PROTO)
7225/*
7226 * Get window or buffer local options.
7227 */
7228 dict_T *
7229get_winbuf_options(int bufopt)
7230{
7231 dict_T *d;
7232 int opt_idx;
7233
7234 d = dict_alloc();
7235 if (d == NULL)
7236 return NULL;
7237
Bram Moolenaardac13472019-09-16 21:06:21 +02007238 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007239 {
7240 struct vimoption *opt = &options[opt_idx];
7241
7242 if ((bufopt && (opt->indir & PV_BUF))
7243 || (!bufopt && (opt->indir & PV_WIN)))
7244 {
7245 char_u *varp = get_varp(opt);
7246
7247 if (varp != NULL)
7248 {
7249 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007250 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007251 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007252 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007253 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007254 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007255 }
7256 }
7257 }
7258
7259 return d;
7260}
7261#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007262
Bram Moolenaardac13472019-09-16 21:06:21 +02007263#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007264/*
7265 * This is called when 'culopt' is changed
7266 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007267 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007268fill_culopt_flags(char_u *val, win_T *wp)
7269{
7270 char_u *p;
7271 char_u culopt_flags_new = 0;
7272
7273 if (val == NULL)
7274 p = wp->w_p_culopt;
7275 else
7276 p = val;
7277 while (*p != NUL)
7278 {
7279 if (STRNCMP(p, "line", 4) == 0)
7280 {
7281 p += 4;
7282 culopt_flags_new |= CULOPT_LINE;
7283 }
7284 else if (STRNCMP(p, "both", 4) == 0)
7285 {
7286 p += 4;
7287 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7288 }
7289 else if (STRNCMP(p, "number", 6) == 0)
7290 {
7291 p += 6;
7292 culopt_flags_new |= CULOPT_NBR;
7293 }
7294 else if (STRNCMP(p, "screenline", 10) == 0)
7295 {
7296 p += 10;
7297 culopt_flags_new |= CULOPT_SCRLINE;
7298 }
7299
7300 if (*p != ',' && *p != NUL)
7301 return FAIL;
7302 if (*p == ',')
7303 ++p;
7304 }
7305
7306 // Can't have both "line" and "screenline".
7307 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7308 return FAIL;
7309 wp->w_p_culopt_flags = culopt_flags_new;
7310
7311 return OK;
7312}
7313#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007314
7315/*
7316 * Get the value of 'magic' adjusted for Vim9 script.
7317 */
7318 int
7319magic_isset(void)
7320{
7321 switch (magic_overruled)
7322 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007323 case OPTION_MAGIC_ON: return TRUE;
7324 case OPTION_MAGIC_OFF: return FALSE;
7325 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007326 }
7327#ifdef FEAT_EVAL
7328 if (in_vim9script())
7329 return TRUE;
7330#endif
7331 return p_magic;
7332}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007333
7334/*
7335 * Set the callback function value for an option that accepts a function name,
7336 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7337 * Returns OK if the option is successfully set to a function, otherwise
7338 * returns FAIL.
7339 */
7340 int
7341option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7342{
7343#ifdef FEAT_EVAL
7344 typval_T *tv;
7345 callback_T cb;
7346
7347 if (optval == NULL || *optval == NUL)
7348 {
7349 free_callback(optcb);
7350 return OK;
7351 }
7352
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007353 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007354 || (STRNCMP(optval, "function(", 9) == 0)
7355 || (STRNCMP(optval, "funcref(", 8) == 0))
7356 // Lambda expression or a funcref
7357 tv = eval_expr(optval, NULL);
7358 else
7359 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007360 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007361 if (tv == NULL)
7362 return FAIL;
7363
7364 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007365 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007366 {
7367 free_tv(tv);
7368 return FAIL;
7369 }
7370
7371 free_callback(optcb);
7372 set_callback(optcb, &cb);
7373 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007374
7375 // when using Vim9 style "import.funcname" it needs to be expanded to
7376 // "import#funcname".
7377 expand_autload_callback(optcb);
7378
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007379 return OK;
7380#else
7381 return FAIL;
7382#endif
7383}