blob: 37c81aaa48752fae619f7a0d865ada3646f5b719 [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 Moolenaar071d4272004-06-13 20:20:40 +00001208/*
1209 * Parse 'arg' for option settings.
1210 *
1211 * 'arg' may be IObuff, but only when no errors can be present and option
1212 * does not need to be expanded with option_expand().
1213 * "opt_flags":
1214 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001215 * OPT_GLOBAL for ":setglobal"
1216 * OPT_LOCAL for ":setlocal" and a modeline
1217 * OPT_MODELINE for a modeline
1218 * OPT_WINONLY to only set window-local options
1219 * OPT_NOWIN to skip setting window-local options
1220 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 *
1222 * returns FAIL if an error is detected, OK otherwise
1223 */
1224 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001225do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02001226 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001227 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228{
Bram Moolenaar1594f312021-07-08 16:40:13 +02001229 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001231 char *errmsg;
1232 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001234 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1235 int nextchar; // next non-white char after option name
1236 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 int len;
1238 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001239 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001241 long_u flags; // flags for current option
1242 char_u *varp = NULL; // pointer to variable for current option
1243 int did_show = FALSE; // already showed one value
1244 int adding; // "opt+=arg"
1245 int prepending; // "opt^=arg"
1246 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 int cp_val = 0;
1248 char_u key_name[2];
1249
1250 if (*arg == NUL)
1251 {
1252 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001253 did_show = TRUE;
1254 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 }
1256
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001257 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
1259 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001260 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001262 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1263 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
1265 /*
1266 * ":set all" show all options.
1267 * ":set all&" set all options to their default value.
1268 */
1269 arg += 3;
1270 if (*arg == '&')
1271 {
1272 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001273 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001275 didset_options();
1276 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001277 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001282 did_show = TRUE;
1283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001285 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {
1287 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001288 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001289 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 arg += 7;
1291 }
1292 else
1293 {
1294 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001295 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {
1297 prefix = 0;
1298 arg += 2;
1299 }
1300 else if (STRNCMP(arg, "inv", 3) == 0)
1301 {
1302 prefix = 2;
1303 arg += 3;
1304 }
1305
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001306 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 key = 0;
1308 if (*arg == '<')
1309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001311 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1313 len = 5;
1314 else
1315 {
1316 len = 1;
1317 while (arg[len] != NUL && arg[len] != '>')
1318 ++len;
1319 }
1320 if (arg[len] != '>')
1321 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001322 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 goto skip;
1324 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001325 arg[len] = NUL; // put NUL after name
1326 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001328 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001330 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332 else
1333 {
1334 len = 0;
1335 /*
1336 * The two characters after "t_" may not be alphanumeric.
1337 */
1338 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1339 len = 4;
1340 else
1341 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1342 ++len;
1343 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001344 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001346 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001348 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001351 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 afterchar = arg[len];
1353
Bram Moolenaardeb108b2021-07-08 17:35:36 +02001354 if (in_vim9script())
1355 {
1356 char_u *p = skipwhite(arg + len);
1357
1358 // disallow white space before =val, +=val, -=val, ^=val
1359 if (p > arg + len && (p[0] == '='
1360 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1361 && p[1] == '=')))
1362 {
1363 errmsg = e_no_white_space_allowed_between_option_and;
1364 arg = p;
1365 startarg = p;
1366 goto skip;
1367 }
1368 }
1369 else
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001370 // skip white space, allow ":set ai ?", ":set hlsearch !"
1371 while (VIM_ISWHITE(arg[len]))
1372 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373
1374 adding = FALSE;
1375 prepending = FALSE;
1376 removing = FALSE;
1377 if (arg[len] != NUL && arg[len + 1] == '=')
1378 {
1379 if (arg[len] == '+')
1380 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001381 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 ++len;
1383 }
1384 else if (arg[len] == '^')
1385 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001386 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 ++len;
1388 }
1389 else if (arg[len] == '-')
1390 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001391 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 ++len;
1393 }
1394 }
1395 nextchar = arg[len];
1396
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001397 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 {
Bram Moolenaar1594f312021-07-08 16:40:13 +02001399 if (in_vim9script() && arg > arg_start
1400 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1401 errmsg = e_no_white_space_allowed_between_option_and;
1402 else
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001403 errmsg = e_unknown_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 goto skip;
1405 }
1406
1407 if (opt_idx >= 0)
1408 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001409 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001411 // Only give an error message when requesting the value of
1412 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1414 && (!(options[opt_idx].flags & P_BOOL)
1415 || nextchar == '?'))
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001416 errmsg = e_option_not_supported;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 goto skip;
1418 }
1419
1420 flags = options[opt_idx].flags;
1421 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1422 }
1423 else
1424 {
1425 flags = P_STRING;
1426 if (key < 0)
1427 {
1428 key_name[0] = KEY2TERMCAP0(key);
1429 key_name[1] = KEY2TERMCAP1(key);
1430 }
1431 else
1432 {
1433 key_name[0] = KS_KEY;
1434 key_name[1] = (key & 0xff);
1435 }
1436 }
1437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001438 // Skip all options that are not window-local (used when showing
1439 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001440 if ((opt_flags & OPT_WINONLY)
1441 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1442 goto skip;
1443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001444 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001445 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1446 && options[opt_idx].var == VAR_WIN)
1447 goto skip;
1448
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001449 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001450 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001452 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001453 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001454 errmsg = e_not_allowed_in_modeline;
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001455 goto skip;
1456 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001457 if ((flags & P_MLE) && !p_mle)
1458 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001459 errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
Bram Moolenaar110289e2019-05-23 15:38:06 +02001460 goto skip;
1461 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001462#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001463 // In diff mode some options are overruled. This avoids that
1464 // 'foldmethod' becomes "marker" instead of "diff" and that
1465 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001466 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001467 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001468 && (
1469#ifdef FEAT_FOLDING
1470 options[opt_idx].indir == PV_FDM ||
1471#endif
1472 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001473 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 }
1476
1477#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001478 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001479 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001481 errmsg = e_not_allowed_in_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 goto skip;
1483 }
1484#endif
1485
1486 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1487 {
1488 arg += len;
1489 cp_val = p_cp;
1490 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1491 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001492 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 {
1494 cp_val = FALSE;
1495 arg += 3;
1496 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001497 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 {
1499 cp_val = TRUE;
1500 arg += 2;
1501 }
1502 }
1503 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001504 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001506 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 goto skip;
1508 }
1509 }
1510
1511 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001512 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001513 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 */
1515 if (nextchar == '?'
1516 || (prefix == 1
1517 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1518 && !(flags & P_BOOL)))
1519 {
1520 /*
1521 * print value
1522 */
1523 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001524 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 else
1526 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001527 gotocmdline(TRUE); // cursor at status line
1528 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
1530 if (opt_idx >= 0)
1531 {
1532 showoneopt(&options[opt_idx], opt_flags);
1533#ifdef FEAT_EVAL
1534 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001535 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001536 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001537 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001538 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001539 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001540 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001541 (int)options[opt_idx].indir & PV_MASK]);
1542 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001543 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001544 (int)options[opt_idx].indir & PV_MASK]);
1545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546#endif
1547 }
1548 else
1549 {
1550 char_u *p;
1551
1552 p = find_termcode(key_name);
1553 if (p == NULL)
1554 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001555 errmsg = e_key_code_not_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 goto skip;
1557 }
1558 else
1559 (void)show_one_termcode(key_name, p, TRUE);
1560 }
1561 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001562 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001563 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 }
1565 else
1566 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001567 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001568 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001569
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001570 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 {
1572 if (nextchar == '=' || nextchar == ':')
1573 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001574 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 goto skip;
1576 }
1577
1578 /*
1579 * ":set opt!": invert
1580 * ":set opt&": reset to default value
1581 * ":set opt<": reset to global value
1582 */
1583 if (nextchar == '!')
1584 value = *(int *)(varp) ^ 1;
1585 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001586 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 ((flags & P_VI_DEF) || cp_val)
1588 ? VI_DEFAULT : VIM_DEFAULT];
1589 else if (nextchar == '<')
1590 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001591 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 if ((int *)varp == &curbuf->b_p_ar
1593 && opt_flags == OPT_LOCAL)
1594 value = -1;
1595 else
1596 value = *(int *)get_varp_scope(&(options[opt_idx]),
1597 OPT_GLOBAL);
1598 }
1599 else
1600 {
1601 /*
1602 * ":set invopt": invert
1603 * ":set opt" or ":set noopt": set or reset
1604 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001605 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001607 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 goto skip;
1609 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001610 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 value = *(int *)(varp) ^ 1;
1612 else
1613 value = prefix;
1614 }
1615
1616 errmsg = set_bool_option(opt_idx, varp, (int)value,
1617 opt_flags);
1618 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001619 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {
1621 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1622 || prefix != 1)
1623 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001624 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 goto skip;
1626 }
1627
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001628 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
1630 /*
1631 * Different ways to set a number option:
1632 * & set to default value
1633 * < set to global value
1634 * <xx> accept special key codes for 'wildchar'
1635 * c accept any non-digit for 'wildchar'
1636 * [-]0-9 set number
1637 * other error
1638 */
1639 ++arg;
1640 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001641 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 ((flags & P_VI_DEF) || cp_val)
1643 ? VI_DEFAULT : VIM_DEFAULT];
1644 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001645 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001646 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1647 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001648 if ((long *)varp == &curbuf->b_p_ul
1649 && opt_flags == OPT_LOCAL)
1650 value = NO_LOCAL_UNDOLEVEL;
1651 else
1652 value = *(long *)get_varp_scope(
1653 &(options[opt_idx]), OPT_GLOBAL);
1654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 else if (((long *)varp == &p_wc
1656 || (long *)varp == &p_wcm)
1657 && (*arg == '<'
1658 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001659 || (*arg != NUL
1660 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 && !VIM_ISDIGIT(*arg))))
1662 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001663 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (value == 0 && (long *)varp != &p_wcm)
1665 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001666 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 goto skip;
1668 }
1669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1671 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001672 // Allow negative (for 'undolevels'), octal and
1673 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001674 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001675 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001676 if (i == 0 || (arg[i] != NUL
1677 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001679 errmsg = e_number_required_after_equal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 goto skip;
1681 }
1682 }
1683 else
1684 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001685 errmsg = e_number_required_after_equal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 goto skip;
1687 }
1688
1689 if (adding)
1690 value = *(long *)varp + value;
1691 if (prepending)
1692 value = *(long *)varp * value;
1693 if (removing)
1694 value = *(long *)varp - value;
1695 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001696 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001698 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001700 char_u *save_arg = NULL;
1701 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001702 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001703 char_u *newval;
1704 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001705 char_u *origval_l = NULL;
1706 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001707#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001708 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001709 char_u *saved_origval_l = NULL;
1710 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001711 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001712#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001713 unsigned newlen;
1714 int comma;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001716 // When using ":set opt=val" for a global option
1717 // with a local value the local value will be
1718 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001720 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 varp = options[opt_idx].var;
1722
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001723 // The old value is kept until we are sure that the
1724 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001726
Bram Moolenaard7c96872019-06-15 17:12:48 +02001727 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1728 {
1729 origval_l = *(char_u **)get_varp_scope(
1730 &(options[opt_idx]), OPT_LOCAL);
1731 origval_g = *(char_u **)get_varp_scope(
1732 &(options[opt_idx]), OPT_GLOBAL);
1733
1734 // A global-local string option might have an empty
1735 // option as value to indicate that the global
1736 // value should be used.
1737 if (((int)options[opt_idx].indir & PV_BOTH)
1738 && origval_l == empty_option)
1739 origval_l = origval_g;
1740 }
1741
1742 // When setting the local value of a global
1743 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001744 if (((int)options[opt_idx].indir & PV_BOTH)
1745 && (opt_flags & OPT_LOCAL))
1746 origval = *(char_u **)get_varp(
1747 &options[opt_idx]);
1748 else
1749 origval = oldval;
1750
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001751 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 {
1753 newval = options[opt_idx].def_val[
1754 ((flags & P_VI_DEF) || cp_val)
1755 ? VI_DEFAULT : VIM_DEFAULT];
1756 if ((char_u **)varp == &p_bg)
1757 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001758 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759#ifdef FEAT_GUI
1760 if (gui.in_use)
1761 newval = gui_bg_default();
1762 else
1763#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001764 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001766 else if ((char_u **)varp == &p_fencs && enc_utf8)
1767 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001769 // expand environment variables and ~ since the
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001770 // default value was already expanded, only
1771 // required when an environment variable was set
1772 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 if (newval == NULL)
1774 newval = empty_option;
1775 else
1776 {
1777 s = option_expand(opt_idx, newval);
1778 if (s == NULL)
1779 s = newval;
1780 newval = vim_strsave(s);
1781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001783 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 {
1785 newval = vim_strsave(*(char_u **)get_varp_scope(
1786 &(options[opt_idx]), OPT_GLOBAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
1788 else
1789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001790 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
1792 /*
1793 * Set 'keywordprg' to ":help" if an empty
1794 * value was passed to :set by the user.
1795 * Misuse errbuf[] for the resulting string.
1796 */
1797 if (varp == (char_u *)&p_kp
1798 && (*arg == NUL || *arg == ' '))
1799 {
1800 STRCPY(errbuf, ":help");
1801 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001802 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001805 * Convert 'backspace' number to string, for
1806 * adding, prepending and removing string.
1807 */
1808 else if (varp == (char_u *)&p_bs
1809 && VIM_ISDIGIT(**(char_u **)varp))
1810 {
1811 i = getdigits((char_u **)varp);
1812 switch (i)
1813 {
1814 case 0:
1815 *(char_u **)varp = empty_option;
1816 break;
1817 case 1:
1818 *(char_u **)varp = vim_strsave(
1819 (char_u *)"indent,eol");
1820 break;
1821 case 2:
1822 *(char_u **)varp = vim_strsave(
1823 (char_u *)"indent,eol,start");
1824 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001825 case 3:
1826 *(char_u **)varp = vim_strsave(
1827 (char_u *)"indent,eol,nostop");
1828 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001829 }
1830 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001831 if (origval == oldval)
1832 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001833 if (origval_l == oldval)
1834 origval_l = *(char_u **)varp;
1835 if (origval_g == oldval)
1836 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001837 oldval = *(char_u **)varp;
1838 }
1839 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 * Convert 'whichwrap' number to string, for
1841 * backwards compatibility with Vim 3.0.
1842 * Misuse errbuf[] for the resulting string.
1843 */
1844 else if (varp == (char_u *)&p_ww
1845 && VIM_ISDIGIT(*arg))
1846 {
1847 *errbuf = NUL;
1848 i = getdigits(&arg);
1849 if (i & 1)
1850 STRCAT(errbuf, "b,");
1851 if (i & 2)
1852 STRCAT(errbuf, "s,");
1853 if (i & 4)
1854 STRCAT(errbuf, "h,l,");
1855 if (i & 8)
1856 STRCAT(errbuf, "<,>,");
1857 if (i & 16)
1858 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001859 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 errbuf[STRLEN(errbuf) - 1] = NUL;
1861 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001862 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 }
1864 /*
1865 * Remove '>' before 'dir' and 'bdir', for
1866 * backwards compatibility with version 3.0
1867 */
1868 else if ( *arg == '>'
1869 && (varp == (char_u *)&p_dir
1870 || varp == (char_u *)&p_bdir))
1871 {
1872 ++arg;
1873 }
1874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 /*
1876 * Copy the new string into allocated memory.
1877 * Can't use set_string_option_direct(), because
1878 * we need to remove the backslashes.
1879 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001880 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 newlen = (unsigned)STRLEN(arg) + 1;
1882 if (adding || prepending || removing)
1883 newlen += (unsigned)STRLEN(origval) + 1;
1884 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001885 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 break;
1887 s = newval;
1888
1889 /*
1890 * Copy the string, skip over escaped chars.
1891 * For MS-DOS and WIN32 backslashes before normal
1892 * file name characters are not removed, and keep
1893 * backslash at start, for "\\machine\path", but
1894 * do remove it for "\\\\machine\\path".
1895 * The reverse is found in ExpandOldSetting().
1896 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001897 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
1899 if (*arg == '\\' && arg[1] != NUL
1900#ifdef BACKSLASH_IN_FILENAME
1901 && !((flags & P_EXPAND)
1902 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001903 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 && (arg[1] != '\\'
1905 || (s == newval
1906 && arg[2] != '\\')))
1907#endif
1908 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001909 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001911 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001913 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 mch_memmove(s, arg, (size_t)i);
1915 arg += i;
1916 s += i;
1917 }
1918 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 *s++ = *arg++;
1920 }
1921 *s = NUL;
1922
1923 /*
1924 * Expand environment variables and ~.
1925 * Don't do it when adding without inserting a
1926 * comma.
1927 */
1928 if (!(adding || prepending || removing)
1929 || (flags & P_COMMA))
1930 {
1931 s = option_expand(opt_idx, newval);
1932 if (s != NULL)
1933 {
1934 vim_free(newval);
1935 newlen = (unsigned)STRLEN(s) + 1;
1936 if (adding || prepending || removing)
1937 newlen += (unsigned)STRLEN(origval) + 1;
1938 newval = alloc(newlen);
1939 if (newval == NULL)
1940 break;
1941 STRCPY(newval, s);
1942 }
1943 }
1944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001945 // locate newval[] in origval[] when removing it
1946 // and when adding to avoid duplicates
1947 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (removing || (flags & P_NODUP))
1949 {
1950 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001951 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001953 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001954 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 prepending = FALSE;
1957 adding = FALSE;
1958 STRCPY(newval, origval);
1959 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001960
1961 // if no duplicate, move pointer to end of
1962 // original value
1963 if (s == NULL)
1964 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
1966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001967 // concatenate the two strings; add a ',' if
1968 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (adding || prepending)
1970 {
1971 comma = ((flags & P_COMMA) && *origval != NUL
1972 && *newval != NUL);
1973 if (adding)
1974 {
1975 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001976 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001977 if (comma && i > 1
1978 && (flags & P_ONECOMMA) == P_ONECOMMA
1979 && origval[i - 1] == ','
1980 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001981 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 mch_memmove(newval + i + comma, newval,
1983 STRLEN(newval) + 1);
1984 mch_memmove(newval, origval, (size_t)i);
1985 }
1986 else
1987 {
1988 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001989 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
1991 if (comma)
1992 newval[i] = ',';
1993 }
1994
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001995 // Remove newval[] from origval[]. (Note: "i" has
1996 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (removing)
1998 {
1999 STRCPY(newval, origval);
2000 if (*s)
2001 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002002 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (flags & P_COMMA)
2004 {
2005 if (s == origval)
2006 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002007 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (s[i] == ',')
2009 ++i;
2010 }
2011 else
2012 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002013 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 --s;
2015 ++i;
2016 }
2017 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002018 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020 }
2021
2022 if (flags & P_FLAGLIST)
2023 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002024 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002025 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002026 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002027 // if options have P_FLAGLIST and
2028 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002029 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002031 if (*s != ',' && *(s + 1) == ','
2032 && vim_strchr(s + 2, *s) != NULL)
2033 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002034 // Remove the duplicated value and
2035 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002036 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002037 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002040 else
2041 {
2042 if ((!(flags & P_COMMA) || *s != ',')
2043 && vim_strchr(s + 1, *s) != NULL)
2044 {
2045 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002046 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002047 }
2048 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002049 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
2052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002053 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 arg = save_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002057 /*
2058 * Set the new value.
2059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 *(char_u **)(varp) = newval;
2061
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002062#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002063 if (!starting
2064# ifdef FEAT_CRYPT
2065 && options[opt_idx].indir != PV_KEY
2066# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002067 && origval != NULL && newval != NULL)
2068 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002069 // origval may be freed by
2070 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002071 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002072 // newval (and varp) may become invalid if the
2073 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002074 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002075 if (origval_l != NULL)
2076 saved_origval_l = vim_strsave(origval_l);
2077 if (origval_g != NULL)
2078 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002079 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002080#endif
2081
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002082 {
2083 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002084 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002085
2086 // When an option is set in the sandbox, from a
2087 // modeline or in secure mode, then deal with side
2088 // effects in secure mode. Also when the value was
2089 // set with the P_INSECURE flag and is not
2090 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002091 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002092#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002093 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002094#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002095 || (!value_is_replaced && (*p & P_INSECURE)))
2096 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002097
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002098 // Handle side effects, and set the global value
2099 // for ":set" on local options. Note: when setting
2100 // 'syntax' or 'filetype' autocommands may be
2101 // triggered that can cause havoc.
2102 errmsg = did_set_string_option(
zeertzjqf6782732022-07-27 18:26:03 +01002103 opt_idx, (char_u **)varp, oldval, errbuf,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002104 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002105
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002106 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002109#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002110 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002111 trigger_optionsset_string(
2112 opt_idx, opt_flags, saved_origval,
2113 saved_origval_l, saved_origval_g,
2114 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002115 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002116 vim_free(saved_origval_l);
2117 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002118 vim_free(saved_newval);
2119#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002120 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 if (errmsg != NULL)
2122 goto skip;
2123 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002124 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
2126 char_u *p;
2127
2128 if (nextchar == '&')
2129 {
2130 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002131 errmsg = e_not_found_in_termcap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
2133 else
2134 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002135 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002136 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 if (*p == '\\' && p[1] != NUL)
2138 ++p;
2139 nextchar = *p;
2140 *p = NUL;
2141 add_termcode(key_name, arg, FALSE);
2142 *p = nextchar;
2143 }
2144 if (full_screen)
2145 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002146 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 }
2148 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002149
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002151 did_set_option(
2152 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154
2155skip:
2156 /*
2157 * Advance to next argument.
2158 * - skip until a blank found, taking care of backslashes
2159 * - skip blanks
2160 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2161 */
2162 for (i = 0; i < 2 ; ++i)
2163 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002164 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 if (*arg++ == '\\' && *arg != NUL)
2166 ++arg;
2167 arg = skipwhite(arg);
2168 if (*arg != '=')
2169 break;
2170 }
2171 }
2172
2173 if (errmsg != NULL)
2174 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002175 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002176 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 if (i + (arg - startarg) < IOSIZE)
2178 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002179 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 STRCAT(IObuff, ": ");
2181 mch_memmove(IObuff + i, startarg, (arg - startarg));
2182 IObuff[i + (arg - startarg)] = NUL;
2183 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002184 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 trans_characters(IObuff, IOSIZE);
2186
Bram Moolenaar13608d82022-08-29 15:06:50 +01002187 ++no_wait_return; // wait_return() done later
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002188 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 --no_wait_return;
2190
2191 return FAIL;
2192 }
2193
2194 arg = skipwhite(arg);
2195 }
2196
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002197theend:
2198 if (silent_mode && did_show)
2199 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002200 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002201 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002202 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002203 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002204 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002205 out_flush();
2206 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002207 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002208 }
2209
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 return OK;
2211}
2212
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002213/*
2214 * Call this when an option has been given a new value through a user command.
2215 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2216 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002217 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002218did_set_option(
2219 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002220 int opt_flags, // possibly with OPT_MODELINE
2221 int new_value, // value was replaced completely
2222 int value_checked) // value was checked to be safe, no need to set the
2223 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002224{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002225 long_u *p;
2226
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002227 options[opt_idx].flags |= P_WAS_SET;
2228
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002229 // When an option is set in the sandbox, from a modeline or in secure mode
2230 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2231 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002232 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002233 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002234#ifdef HAVE_SANDBOX
2235 || sandbox != 0
2236#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002237 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002238 *p = *p | P_INSECURE;
2239 else if (new_value)
2240 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002241}
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243/*
2244 * Convert a key name or string into a key value.
2245 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002246 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002248 int
2249string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
2251 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002252 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 if (*arg == '^')
2254 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002255 if (multi_byte)
2256 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 return *arg;
2258}
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260/*
2261 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2262 * maketitle() to create and display it.
2263 * When switching the title or icon off, call mch_restore_title() to get
2264 * the old value back.
2265 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002266 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002267did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
2269 if (starting != NO_SCREEN
2270#ifdef FEAT_GUI
2271 && !gui.starting
2272#endif
2273 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276
2277/*
2278 * set_options_bin - called when 'bin' changes value.
2279 */
2280 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002281set_options_bin(
2282 int oldval,
2283 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002284 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
2286 /*
2287 * The option values that are changed when 'bin' changes are
2288 * copied when 'bin is set and restored when 'bin' is reset.
2289 */
2290 if (newval)
2291 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002292 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
2294 if (!(opt_flags & OPT_GLOBAL))
2295 {
2296 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2297 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2298 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2299 curbuf->b_p_et_nobin = curbuf->b_p_et;
2300 }
2301 if (!(opt_flags & OPT_LOCAL))
2302 {
2303 p_tw_nobin = p_tw;
2304 p_wm_nobin = p_wm;
2305 p_ml_nobin = p_ml;
2306 p_et_nobin = p_et;
2307 }
2308 }
2309
2310 if (!(opt_flags & OPT_GLOBAL))
2311 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002312 curbuf->b_p_tw = 0; // no automatic line wrap
2313 curbuf->b_p_wm = 0; // no automatic line wrap
2314 curbuf->b_p_ml = 0; // no modelines
2315 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317 if (!(opt_flags & OPT_LOCAL))
2318 {
2319 p_tw = 0;
2320 p_wm = 0;
2321 p_ml = FALSE;
2322 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002323 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002326 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 if (!(opt_flags & OPT_GLOBAL))
2329 {
2330 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2331 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2332 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2333 curbuf->b_p_et = curbuf->b_p_et_nobin;
2334 }
2335 if (!(opt_flags & OPT_LOCAL))
2336 {
2337 p_tw = p_tw_nobin;
2338 p_wm = p_wm_nobin;
2339 p_ml = p_ml_nobin;
2340 p_et = p_et_nobin;
2341 }
2342 }
2343}
2344
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345/*
2346 * Expand environment variables for some string options.
2347 * These string options cannot be indirect!
2348 * If "val" is NULL expand the current value of the option.
2349 * Return pointer to NameBuff, or NULL when not expanded.
2350 */
2351 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002352option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002354 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2356 return NULL;
2357
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002358 // If val is longer than MAXPATHL no meaningful expansion can be done,
2359 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 if (val != NULL && STRLEN(val) > MAXPATHL)
2361 return NULL;
2362
2363 if (val == NULL)
2364 val = *(char_u **)options[opt_idx].var;
2365
2366 /*
2367 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2368 * Escape spaces when expanding 'tags', they are used to separate file
2369 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002370 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 */
2372 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002373 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002374#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002375 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2376#endif
2377 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002378 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 return NULL;
2380
2381 return NameBuff;
2382}
2383
2384/*
2385 * After setting various option values: recompute variables that depend on
2386 * option values.
2387 */
2388 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002389didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002391 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 (void)init_chartab();
2393
Bram Moolenaardac13472019-09-16 21:06:21 +02002394 didset_string_options();
2395
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002396#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002397 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002398 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002399 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002400 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002403 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 (void)check_cedit();
2405#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002406#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002407 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002408 fill_breakat_flags();
2409#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002410 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002411}
2412
2413/*
2414 * More side effects of setting options.
2415 */
2416 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002417didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002418{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002419 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002420 (void)highlight_changed();
2421
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002422 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002423 check_opt_wim();
2424
Bram Moolenaareed9d462021-02-15 20:38:25 +01002425 // Parse default for 'listchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002426 (void)set_chars_option(curwin, &curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002427
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002428 // Parse default for 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002429 (void)set_chars_option(curwin, &curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002430
2431#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002432 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002433 (void)check_clipboard_option();
2434#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002435#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002436 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002437 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002438 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002439 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441}
2442
2443/*
2444 * Check for string options that are NULL (normally only termcap options).
2445 */
2446 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002447check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448{
2449 int opt_idx;
2450
2451 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2452 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2453 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2454}
2455
2456/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002457 * Return the option index found by a pointer into term_strings[].
2458 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002460 int
2461get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002463 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
2465 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2466 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002467 return opt_idx;
2468 return -1; // cannot happen: didn't find it!
2469}
2470
2471/*
2472 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2473 * Return the option index or -1 if not found.
2474 */
2475 int
2476set_term_option_alloced(char_u **p)
2477{
2478 int opt_idx = get_term_opt_idx(p);
2479
2480 if (opt_idx >= 0)
2481 options[opt_idx].flags |= P_ALLOCED;
2482 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483}
2484
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002485#if defined(FEAT_EVAL) || defined(PROTO)
2486/*
2487 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2488 * Return FALSE when it wasn't.
2489 * Return -1 for an unknown option.
2490 */
2491 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002492was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002493{
2494 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002495 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002496
2497 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002498 {
2499 flagp = insecure_flag(idx, opt_flags);
2500 return (*flagp & P_INSECURE) != 0;
2501 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002502 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002503 return -1;
2504}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002505
2506/*
2507 * Get a pointer to the flags used for the P_INSECURE flag of option
2508 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002509 * NOTE: Caller must make sure that "curwin" is set to the window from which
2510 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002511 */
2512 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002513insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002514{
2515 if (opt_flags & OPT_LOCAL)
2516 switch ((int)options[opt_idx].indir)
2517 {
2518#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002519 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002520#endif
2521#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002522# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002523 case PV_FDE: return &curwin->w_p_fde_flags;
2524 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002525# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002526# ifdef FEAT_BEVAL
2527 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2528# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002529 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002530 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002531# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002532 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002533# endif
2534#endif
2535 }
2536
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002537 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002538 return &options[opt_idx].flags;
2539}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002540#endif
2541
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002542/*
2543 * Redraw the window title and/or tab page text later.
2544 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002545void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002546{
2547 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002548 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002549}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002552 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2553 * characters or characters in "allowed".
2554 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002555 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002556valid_name(char_u *val, char *allowed)
2557{
2558 char_u *s;
2559
2560 for (s = val; *s != NUL; ++s)
2561 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2562 return FALSE;
2563 return TRUE;
2564}
2565
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002566#if defined(FEAT_EVAL) || defined(PROTO)
2567/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002568 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002569 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002570 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002571 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002572set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002573{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002574 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2575 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002576 sctx_T new_script_ctx = script_ctx;
2577
Bram Moolenaar51258742020-05-03 17:19:33 +02002578 // Modeline already has the line number set.
2579 if (!(opt_flags & OPT_MODELINE))
2580 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002581
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002582 // Remember where the option was set. For local options need to do that
2583 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002584 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002585 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002586 if (both || (opt_flags & OPT_LOCAL))
2587 {
2588 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002589 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002590 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002591 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002592 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002593 if (both)
2594 // also setting the "all buffers" value
2595 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2596 new_script_ctx;
2597 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002598 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002599}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002600
2601/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002602 * Get the script context of global option "name".
2603 *
2604 */
2605 sctx_T *
2606get_option_sctx(char *name)
2607{
2608 int idx = findoption((char_u *)name);
2609
2610 if (idx >= 0)
2611 return &options[idx].script_ctx;
2612 siemsg("no such option: %s", name);
2613 return NULL;
2614}
2615
2616/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002617 * Set the script_ctx for a termcap option.
2618 * "name" must be the two character code, e.g. "RV".
2619 * When "name" is NULL use "opt_idx".
2620 */
2621 void
2622set_term_option_sctx_idx(char *name, int opt_idx)
2623{
2624 char_u buf[5];
2625 int idx;
2626
2627 if (name == NULL)
2628 idx = opt_idx;
2629 else
2630 {
2631 buf[0] = 't';
2632 buf[1] = '_';
2633 buf[2] = name[0];
2634 buf[3] = name[1];
2635 buf[4] = 0;
2636 idx = findoption(buf);
2637 }
2638 if (idx >= 0)
2639 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2640}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002641#endif
2642
Bram Moolenaarf4140482020-02-15 23:06:45 +01002643#if defined(FEAT_EVAL)
2644/*
2645 * Apply the OptionSet autocommand.
2646 */
2647 static void
2648apply_optionset_autocmd(
2649 int opt_idx,
2650 long opt_flags,
2651 long oldval,
2652 long oldval_g,
2653 long newval,
2654 char *errmsg)
2655{
2656 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2657
2658 // Don't do this while starting up, failure or recursively.
2659 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2660 return;
2661
2662 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2663 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2664 oldval_g);
2665 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2666 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2667 (opt_flags & OPT_LOCAL) ? "local" : "global");
2668 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2669 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2670 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2671 if (opt_flags & OPT_LOCAL)
2672 {
2673 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2674 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2675 }
2676 if (opt_flags & OPT_GLOBAL)
2677 {
2678 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2679 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2680 }
2681 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2682 {
2683 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2684 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2685 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2686 }
2687 if (opt_flags & OPT_MODELINE)
2688 {
2689 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2690 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2691 }
2692 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2693 NULL, FALSE, NULL);
2694 reset_v_option_vars();
2695}
2696#endif
2697
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698/*
2699 * Set the value of a boolean option, and take care of side effects.
2700 * Returns NULL for success, or an error message for an error.
2701 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002702 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002703set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002704 int opt_idx, // index in options[] table
2705 char_u *varp, // pointer to the option variable
2706 int value, // new value
2707 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002710#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002711 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002712#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002713 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002715 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 if ((secure
2717#ifdef HAVE_SANDBOX
2718 || sandbox != 0
2719#endif
2720 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002721 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002723#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002724 // Save the global value before changing anything. This is needed as for
2725 // a global-only option setting the "local value" in fact sets the global
2726 // value (since there is only one value).
2727 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2728 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2729 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002730#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002731
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002732 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002734 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002735 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736#endif
2737
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002738#ifdef FEAT_GUI
2739 need_mouse_correct = TRUE;
2740#endif
2741
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002742 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2744 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2745
2746 /*
2747 * Handle side effects of changing a bool option.
2748 */
2749
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002750 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753
Bram Moolenaar920694c2016-08-21 17:45:02 +02002754#ifdef FEAT_LANGMAP
2755 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002757 p_lnr = !p_lrm;
2758 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002759 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002760 p_lrm = !p_lnr;
2761#endif
2762
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002763#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002765 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2766 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // Only take action when the option was set. When reset we do not
2768 // delete the undo file, the option may be set again without making
2769 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002770 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002771 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002772 char_u hash[UNDO_HASH_SIZE];
2773 buf_T *save_curbuf = curbuf;
2774
Bram Moolenaar29323592016-07-24 22:04:11 +02002775 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002776 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002777 // When 'undofile' is set globally: for every buffer, otherwise
2778 // only for the current buffer: Try to read in the undofile,
2779 // if one exists, the buffer wasn't changed and the buffer was
2780 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002781 if ((curbuf == save_curbuf
2782 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2783 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2784 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002785#ifdef FEAT_CRYPT
2786 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2787 continue;
2788#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002789 u_compute_hash(hash);
2790 u_read_undo(NULL, hash, curbuf->b_fname);
2791 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002792 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002793 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002794 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002795 }
2796#endif
2797
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 else if ((int *)varp == &curbuf->b_p_ro)
2799 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002800 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2802 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002803
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002804 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002805 if (curbuf->b_p_ro)
2806 curbuf->b_did_warn = FALSE;
2807
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002808 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
2810
Bram Moolenaar9c449722010-07-20 18:44:27 +02002811#ifdef FEAT_GUI
2812 else if ((int *)varp == &p_mh)
2813 {
2814 if (!p_mh)
2815 gui_mch_mousehide(FALSE);
2816 }
2817#endif
2818
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002819 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002821 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002822# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002823 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002824 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2825 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002826 {
2827 curbuf->b_p_ma = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002828 return e_cannot_make_terminal_with_running_job_modifiable;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002829 }
2830# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002831 redraw_titles();
2832 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002833 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002835 {
2836 redraw_titles();
2837 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002838 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002839 else if ((int *)varp == &curbuf->b_p_fixeol)
2840 {
2841 redraw_titles();
2842 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002843 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002844 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002845 {
2846 redraw_titles();
2847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 else if ((int *)varp == &curbuf->b_p_bin)
2851 {
2852 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002853 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 }
2855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002856 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2858 {
2859 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2860 NULL, NULL, TRUE, curbuf);
2861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002863 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 else if ((int *)varp == &curbuf->b_p_swf)
2865 {
2866 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002867 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002869 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2870 // buf->b_p_swf
2871 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
2873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 else if ((int *)varp == &p_terse)
2876 {
2877 char_u *p;
2878
2879 p = vim_strchr(p_shm, SHM_SEARCH);
2880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 if (p_terse && p == NULL)
2883 {
2884 STRCPY(IObuff, p_shm);
2885 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002886 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002888 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002890 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
2892
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002893 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 else if ((int *)varp == &p_paste)
2895 {
2896 paste_option_changed();
2897 }
2898
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002899 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 else if ((int *)varp == &p_im)
2901 {
2902 if (p_im)
2903 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002904 if ((State & MODE_INSERT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 need_start_insertmode = TRUE;
2906 stop_insert_mode = FALSE;
2907 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002909 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
2911 need_start_insertmode = FALSE;
2912 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002913 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002914 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 restart_edit = 0;
2916 }
2917 }
2918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002919 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 else if ((int *)varp == &p_ic && p_hls)
2921 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002922 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
2924
2925#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 else if ((int *)varp == &p_hls)
2928 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002929 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
2931#endif
2932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002933 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2934 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 else if ((int *)varp == &curwin->w_p_scb)
2936 {
2937 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002940 curwin->w_scbind_pos = curwin->w_topline;
2941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
Bram Moolenaar4033c552017-09-16 20:54:51 +02002944#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 else if ((int *)varp == &curwin->w_p_pvw)
2947 {
2948 if (curwin->w_p_pvw)
2949 {
2950 win_T *win;
2951
Bram Moolenaar29323592016-07-24 22:04:11 +02002952 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 if (win->w_p_pvw && win != curwin)
2954 {
2955 curwin->w_p_pvw = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002956 return e_preview_window_already_exists;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 }
2958 }
2959 }
2960#endif
2961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002962 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 else if ((int *)varp == &curbuf->b_p_tx)
2964 {
2965 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2966 }
2967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002968 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 set_string_option_direct((char_u *)"ffs", -1,
2972 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002973 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
2976 /*
2977 * When 'lisp' option changes include/exclude '-' in
2978 * keyword characters.
2979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2981 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002986 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002988 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
2991 else if ((int *)varp == &curbuf->b_changed)
2992 {
2993 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002994 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002995 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
2998
2999#ifdef BACKSLASH_IN_FILENAME
3000 else if ((int *)varp == &p_ssl)
3001 {
3002 if (p_ssl)
3003 {
3004 psepc = '/';
3005 psepcN = '\\';
3006 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 }
3008 else
3009 {
3010 psepc = '\\';
3011 psepcN = '/';
3012 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
3014
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003015 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 buflist_slash_adjust();
3017 alist_slash_adjust();
3018# ifdef FEAT_EVAL
3019 scriptnames_slash_adjust();
3020# endif
3021 }
3022#endif
3023
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003024 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 else if ((int *)varp == &curwin->w_p_wrap)
3026 {
3027 if (curwin->w_p_wrap)
3028 curwin->w_leftcol = 0;
3029 }
3030
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 else if ((int *)varp == &p_ea)
3032 {
3033 if (p_ea && !old_value)
3034 win_equal(curwin, FALSE, 0);
3035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
3037 else if ((int *)varp == &p_wiv)
3038 {
3039 /*
3040 * When 'weirdinvert' changed, set/reset 't_xs'.
3041 * Then set 'weirdinvert' according to value of 't_xs'.
3042 */
3043 if (p_wiv && !old_value)
3044 T_XS = (char_u *)"y";
3045 else if (!p_wiv && old_value)
3046 T_XS = empty_option;
3047 p_wiv = (*T_XS != NUL);
3048 }
3049
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003050#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 else if ((int *)varp == &p_beval)
3052 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003053 if (!balloonEvalForTerm)
3054 {
3055 if (p_beval && !old_value)
3056 gui_mch_enable_beval_area(balloonEval);
3057 else if (!p_beval && old_value)
3058 gui_mch_disable_beval_area(balloonEval);
3059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003061#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003062#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003063 else if ((int *)varp == &p_bevalterm)
3064 {
3065 mch_bevalterm_changed();
3066 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003069#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 else if ((int *)varp == &p_acd)
3071 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003072 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003073 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 }
3075#endif
3076
3077#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003078 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 else if ((int *)varp == &curwin->w_p_diff)
3080 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003081 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003082 diff_buf_adjust(curwin);
3083# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 if (foldmethodIsDiff(curwin))
3085 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 }
3088#endif
3089
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003090#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003091 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 else if ((int *)varp == &p_imdisable)
3093 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 if (p_imdisable)
3096 im_set_active(FALSE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003097 else if (State & MODE_INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003098 // When the option is set from an autocommand, it may need to take
3099 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003100 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 }
3102#endif
3103
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003104#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003105 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003106 else if ((int *)varp == &curwin->w_p_spell)
3107 {
3108 if (curwin->w_p_spell)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003109 errmsg = did_set_spelllang(curwin);
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003110 }
3111#endif
3112
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113#ifdef FEAT_ARABIC
3114 if ((int *)varp == &curwin->w_p_arab)
3115 {
3116 if (curwin->w_p_arab)
3117 {
3118 /*
3119 * 'arabic' is set, handle various sub-settings.
3120 */
3121 if (!p_tbidi)
3122 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003123 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 if (!curwin->w_p_rl)
3125 {
3126 curwin->w_p_rl = TRUE;
3127 changed_window_setting();
3128 }
3129
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003130 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 if (!p_arshape)
3132 {
3133 p_arshape = TRUE;
3134 redraw_later_clear();
3135 }
3136 }
3137
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003138 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003139 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003141 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003142 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3143
Bram Moolenaar8820b482017-03-16 17:23:31 +01003144 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003145 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003146#ifdef FEAT_EVAL
3147 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3148#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003151 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
3154# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003155 // Force-set the necessary keymap for arabic
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003156 errmsg = set_option_value((char_u *)"keymap",
3157 0L, (char_u *)"arabic", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 }
3160 else
3161 {
3162 /*
3163 * 'arabic' is reset, handle various sub-settings.
3164 */
3165 if (!p_tbidi)
3166 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003167 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 if (curwin->w_p_rl)
3169 {
3170 curwin->w_p_rl = FALSE;
3171 changed_window_setting();
3172 }
3173
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003174 // 'arabicshape' isn't reset, it is a global option and
3175 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 }
3177
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003178 // 'delcombine' isn't reset, it is a global option and another
3179 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003182 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 curbuf->b_p_iminsert = B_IMODE_NONE;
3184 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3185# endif
3186 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003187 }
3188
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003189#endif
3190
Bram Moolenaar44826212019-08-22 21:23:20 +02003191#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3192 else if (((int *)varp == &curwin->w_p_nu
3193 || (int *)varp == &curwin->w_p_rnu)
3194 && gui.in_use
3195 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3196 && curbuf->b_signlist != NULL)
3197 {
3198 // If the 'number' or 'relativenumber' options are modified and
3199 // 'signcolumn' is set to 'number', then clear the screen for a full
3200 // refresh. Otherwise the sign icons are not displayed properly in the
3201 // number column. If the 'number' option is set and only the
3202 // 'relativenumber' option is toggled, then don't refresh the screen
3203 // (optimization).
3204 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003205 redraw_all_later(UPD_CLEAR);
Bram Moolenaar44826212019-08-22 21:23:20 +02003206 }
3207#endif
3208
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003209#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003210 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003211 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003212 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003213# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003214 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003215 if (
3216# ifdef VIMDLL
3217 !gui.in_use && !gui.starting &&
3218# endif
3219 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003220 {
3221 p_tgc = 0;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003222 return e_24_bit_colors_are_not_supported_on_this_environment;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003223 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003224 if (is_term_win32())
3225 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003226# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003227# ifdef FEAT_GUI
3228 if (!gui.in_use && !gui.starting)
3229# endif
3230 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003231# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003232 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003233 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003234 {
3235 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003236 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003237 init_highlight(TRUE, FALSE);
3238 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003239# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003240# ifdef FEAT_TERMINAL
3241 term_update_colors_all();
LemonBoyb2b3acb2022-05-20 10:10:34 +01003242 term_update_palette_all();
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003243 term_update_wincolor_all();
3244# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003245 }
3246#endif
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 /*
3249 * End of handling side effects for bool options.
3250 */
3251
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003252 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003253
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 options[opt_idx].flags |= P_WAS_SET;
3255
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003256#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003257 apply_optionset_autocmd(opt_idx, opt_flags,
3258 (long)(old_value ? TRUE : FALSE),
3259 (long)(old_global_value ? TRUE : FALSE),
3260 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003261#endif
3262
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003263 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003264 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003265 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003266 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003267
3268 if ((opt_flags & OPT_NO_REDRAW) == 0)
3269 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003271 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272}
3273
3274/*
3275 * Set the value of a number option, and take care of side effects.
3276 * Returns NULL for success, or an error message for an error.
3277 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003278 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003279set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003280 int opt_idx, // index in options[] table
3281 char_u *varp, // pointer to the option variable
3282 long value, // new value
3283 char *errbuf, // buffer for error messages
3284 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003285 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3286 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003288 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003290#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003291 long old_global_value = 0; // only used when setting a local and
3292 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003293#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003294 long old_Rows = Rows; // remember old Rows
3295 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 long *pp = (long *)varp;
3297
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003298 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003299 if ((secure
3300#ifdef HAVE_SANDBOX
3301 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003303 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003304 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003306#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003307 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003308 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003309 // value (since there is only one value).
3310 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003311 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3312 OPT_GLOBAL);
3313#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003314
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 *pp = value;
3316#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003317 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003318 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003320#ifdef FEAT_GUI
3321 need_mouse_correct = TRUE;
3322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
Bram Moolenaar14f24742012-08-08 18:01:05 +02003324 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003326 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003327#ifdef FEAT_VARTABS
3328 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3329 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003330 ? tabstop_first(curbuf->b_p_vts_array)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003331 : curbuf->b_p_ts;
3332#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
3336
3337 /*
3338 * Number options that need some action when changed
3339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 if (pp == &p_wh || pp == &p_hh)
3341 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003342 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 if (p_wh < 1)
3344 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003345 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 p_wh = 1;
3347 }
3348 if (p_wmh > p_wh)
3349 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003350 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 p_wh = p_wmh;
3352 }
3353 if (p_hh < 0)
3354 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003355 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 p_hh = 0;
3357 }
3358
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003359 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003360 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 {
3362 if (pp == &p_wh && curwin->w_height < p_wh)
3363 win_setheight((int)p_wh);
3364 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3365 win_setheight((int)p_hh);
3366 }
3367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 else if (pp == &p_wmh)
3369 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003370 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 if (p_wmh < 0)
3372 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003373 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 p_wmh = 0;
3375 }
3376 if (p_wmh > p_wh)
3377 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003378 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 p_wmh = p_wh;
3380 }
3381 win_setminheight();
3382 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003383 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003385 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 if (p_wiw < 1)
3387 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003388 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 p_wiw = 1;
3390 }
3391 if (p_wmw > p_wiw)
3392 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003393 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 p_wiw = p_wmw;
3395 }
3396
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003397 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003398 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 win_setwidth((int)p_wiw);
3400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 else if (pp == &p_wmw)
3402 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003403 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (p_wmw < 0)
3405 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003406 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 p_wmw = 0;
3408 }
3409 if (p_wmw > p_wiw)
3410 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003411 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 p_wmw = p_wiw;
3413 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003414 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003417 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 else if (pp == &p_ls)
3419 {
3420 last_status(FALSE);
3421 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003423 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003424 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003425 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003426 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
3429#ifdef FEAT_GUI
3430 else if (pp == &p_linespace)
3431 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003432 // Recompute gui.char_height and resize the Vim window to keep the
3433 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003434 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003435 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437#endif
3438
3439#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003440 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 else if (pp == &curwin->w_p_fdl)
3442 {
3443 if (curwin->w_p_fdl < 0)
3444 curwin->w_p_fdl = 0;
3445 newFoldLevel();
3446 }
3447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003448 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 else if (pp == &curwin->w_p_fml)
3450 {
3451 foldUpdateAll(curwin);
3452 }
3453
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003454 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 else if (pp == &curwin->w_p_fdn)
3456 {
3457 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3458 foldUpdateAll(curwin);
3459 }
3460
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003461 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 else if (pp == &curwin->w_p_fdc)
3463 {
3464 if (curwin->w_p_fdc < 0)
3465 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003466 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 curwin->w_p_fdc = 0;
3468 }
3469 else if (curwin->w_p_fdc > 12)
3470 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003471 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 curwin->w_p_fdc = 12;
3473 }
3474 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003475#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003477 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3479 {
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003480#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (foldmethodIsIndent(curwin))
3482 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003483#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003484 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3485 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003486 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3487 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003490 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003491 else if (pp == &p_mco)
3492 {
3493 if (p_mco > MAX_MCO)
3494 p_mco = MAX_MCO;
3495 else if (p_mco < 0)
3496 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003497 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003498 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 else if (pp == &curbuf->b_p_iminsert)
3501 {
3502 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3503 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003504 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 curbuf->b_p_iminsert = B_IMODE_NONE;
3506 }
3507 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003508 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003510#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003511 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 status_redraw_curbuf();
3513#endif
3514 }
3515
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003516#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003517 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003518 else if (pp == &p_imst)
3519 {
3520 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003521 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003522 }
3523#endif
3524
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003525 else if (pp == &p_window)
3526 {
3527 if (p_window < 1)
3528 p_window = 1;
3529 else if (p_window >= Rows)
3530 p_window = Rows - 1;
3531 }
3532
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 else if (pp == &curbuf->b_p_imsearch)
3534 {
3535 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3536 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003537 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 curbuf->b_p_imsearch = B_IMODE_NONE;
3539 }
3540 p_imsearch = curbuf->b_p_imsearch;
3541 }
3542
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003543 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 else if (pp == &p_titlelen)
3545 {
3546 if (p_titlelen < 0)
3547 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003548 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 p_titlelen = 85;
3550 }
3551 if (starting != NO_SCREEN && old_value != p_titlelen)
3552 need_maketitle = TRUE;
3553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003555 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 else if (pp == &p_ch)
3557 {
Bram Moolenaara2a89732022-08-31 14:46:18 +01003558 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003560 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 p_ch = 1;
3562 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003563 if (p_ch > Rows - min_rows() + 1)
3564 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003566 // Only compute the new window layout when startup has been
3567 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 if (p_ch != old_value && full_screen
3569#ifdef FEAT_GUI
3570 && !gui.starting
3571#endif
3572 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003573 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003576 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 else if (pp == &p_uc)
3578 {
3579 if (p_uc < 0)
3580 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003581 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 p_uc = 100;
3583 }
3584 if (p_uc && !old_value)
3585 ml_open_files();
3586 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003587#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003588 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003590 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003591 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003592 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003593 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003594 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003595 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003596 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003597 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003598 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003599 }
3600 }
3601#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003602#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003603 else if (pp == &p_mzq)
3604 mzvim_reset_timer();
3605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003607#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003608 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003609 else if (pp == &p_pyx)
3610 {
3611 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003612 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003613 }
3614#endif
3615
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003616 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 else if (pp == &p_ul)
3618 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003619 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003621 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 p_ul = value;
3623 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003624 else if (pp == &curbuf->b_p_ul)
3625 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003626 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003627 curbuf->b_p_ul = old_value;
3628 u_sync(TRUE);
3629 curbuf->b_p_ul = value;
3630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003632#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003633 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003634 else if (pp == &curwin->w_p_nuw)
3635 {
3636 if (curwin->w_p_nuw < 1)
3637 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003638 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003639 curwin->w_p_nuw = 1;
3640 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003641 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003642 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003643 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003644 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003645 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003646 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003647 }
3648#endif
3649
Bram Moolenaar1a384422010-07-14 19:53:30 +02003650 else if (pp == &curbuf->b_p_tw)
3651 {
3652 if (curbuf->b_p_tw < 0)
3653 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003654 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003655 curbuf->b_p_tw = 0;
3656 }
3657#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003658 {
3659 win_T *wp;
3660 tabpage_T *tp;
3661
3662 FOR_ALL_TAB_WINDOWS(tp, wp)
3663 check_colorcolumn(wp);
3664 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003665#endif
3666 }
3667
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 /*
3669 * Check the bounds for numeric options here
3670 */
3671 if (Rows < min_rows() && full_screen)
3672 {
3673 if (errbuf != NULL)
3674 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003675 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003676 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 errmsg = errbuf;
3678 }
3679 Rows = min_rows();
3680 }
3681 if (Columns < MIN_COLUMNS && full_screen)
3682 {
3683 if (errbuf != NULL)
3684 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003685 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003686 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 errmsg = errbuf;
3688 }
3689 Columns = MIN_COLUMNS;
3690 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003691 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 /*
3694 * If the screen (shell) height has been changed, assume it is the
3695 * physical screenheight.
3696 */
3697 if (old_Rows != Rows || old_Columns != Columns)
3698 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003699 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 if (updating_screen)
3701 *pp = old_value;
3702 else if (full_screen
3703#ifdef FEAT_GUI
3704 && !gui.starting
3705#endif
3706 )
3707 set_shellsize((int)Columns, (int)Rows, TRUE);
3708 else
3709 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003710 // Postpone the resizing; check the size and cmdline position for
3711 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 check_shellsize();
3713 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3714 cmdline_row = Rows - p_ch;
3715 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003716 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003717 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 if (curbuf->b_p_ts <= 0)
3721 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003722 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 curbuf->b_p_ts = 8;
3724 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003725 else if (curbuf->b_p_ts > TABSTOP_MAX)
3726 {
3727 errmsg = e_invalid_argument;
3728 curbuf->b_p_ts = 8;
3729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 if (p_tm < 0)
3731 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003732 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 p_tm = 0;
3734 }
3735 if ((curwin->w_p_scr <= 0
3736 || (curwin->w_p_scr > curwin->w_height
3737 && curwin->w_height > 0))
3738 && full_screen)
3739 {
3740 if (pp == &(curwin->w_p_scr))
3741 {
3742 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003743 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 win_comp_scroll(curwin);
3745 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003746 // If 'scroll' became invalid because of a side effect silently adjust
3747 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 else if (curwin->w_p_scr <= 0)
3749 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003750 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 curwin->w_p_scr = curwin->w_height;
3752 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003753 if (p_hi < 0)
3754 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003755 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003756 p_hi = 0;
3757 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003758 else if (p_hi > 10000)
3759 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003760 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003761 p_hi = 10000;
3762 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003763 if (p_re < 0 || p_re > 2)
3764 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003765 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003766 p_re = 0;
3767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 if (p_report < 0)
3769 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003770 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 p_report = 1;
3772 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003773 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003775 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 p_sj = Rows / 2;
3777 else
3778 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003779 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 p_sj = 1;
3781 }
3782 }
3783 if (p_so < 0 && full_screen)
3784 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003785 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 p_so = 0;
3787 }
3788 if (p_siso < 0 && full_screen)
3789 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003790 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 p_siso = 0;
3792 }
3793#ifdef FEAT_CMDWIN
3794 if (p_cwh < 1)
3795 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003796 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 p_cwh = 1;
3798 }
3799#endif
3800 if (p_ut < 0)
3801 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003802 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 p_ut = 2000;
3804 }
3805 if (p_ss < 0)
3806 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003807 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 p_ss = 0;
3809 }
3810
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003811 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3813 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3814
3815 options[opt_idx].flags |= P_WAS_SET;
3816
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003817#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003818 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3819 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003820#endif
3821
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003822 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003823 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003824 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003825 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003826 if ((opt_flags & OPT_NO_REDRAW) == 0)
3827 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
3829 return errmsg;
3830}
3831
3832/*
3833 * Called after an option changed: check if something needs to be redrawn.
3834 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003835 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003836check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003838 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003839 int doclear = (flags & P_RCLR) == P_RCLR;
3840 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003842 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
3845 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3846 changed_window_setting();
3847 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003848 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003849 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003850 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003851 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003852 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003854 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855}
3856
3857/*
3858 * Find index for option 'arg'.
3859 * Return -1 if not found.
3860 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003861 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003862findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863{
3864 int opt_idx;
3865 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003866 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 int is_term_opt;
3868
3869 /*
3870 * For first call: Initialize the quick-access table.
3871 * It contains the index for the first option that starts with a certain
3872 * letter. There are 26 letters, plus the first "t_" option.
3873 */
3874 if (quick_tab[1] == 0)
3875 {
3876 p = options[0].fullname;
3877 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3878 {
3879 if (s[0] != p[0])
3880 {
3881 if (s[0] == 't' && s[1] == '_')
3882 quick_tab[26] = opt_idx;
3883 else
3884 quick_tab[CharOrdLow(s[0])] = opt_idx;
3885 }
3886 p = s;
3887 }
3888 }
3889
3890 /*
3891 * Check for name starting with an illegal character.
3892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 return -1;
3895
3896 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3897 if (is_term_opt)
3898 opt_idx = quick_tab[26];
3899 else
3900 opt_idx = quick_tab[CharOrdLow(arg[0])];
3901 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3902 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003903 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 break;
3905 }
3906 if (s == NULL && !is_term_opt)
3907 {
3908 opt_idx = quick_tab[CharOrdLow(arg[0])];
3909 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3910 {
3911 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003912 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 break;
3914 s = NULL;
3915 }
3916 }
3917 if (s == NULL)
3918 opt_idx = -1;
3919 return opt_idx;
3920}
3921
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003922#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923/*
3924 * Get the value for an option.
3925 *
3926 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003927 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003928 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003929 * String option: gov_string, *stringval gets allocated string.
3930 * Hidden Number option: gov_hidden_number.
3931 * Hidden Toggle option: gov_hidden_bool.
3932 * Hidden String option: gov_hidden_string.
3933 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003934 *
3935 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003937 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003938get_option_value(
3939 char_u *name,
3940 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003941 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003942 int *flagsp,
3943 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944{
3945 int opt_idx;
3946 char_u *varp;
3947
3948 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003949 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003950 {
3951 int key;
3952
3953 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003954 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003955 {
3956 char_u key_name[2];
3957 char_u *p;
3958
Bram Moolenaar10c75c42021-12-28 20:53:30 +00003959 if (flagsp != NULL)
3960 *flagsp = 0; // terminal option has no flags
3961
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003962 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003963 if (key < 0)
3964 {
3965 key_name[0] = KEY2TERMCAP0(key);
3966 key_name[1] = KEY2TERMCAP1(key);
3967 }
3968 else
3969 {
3970 key_name[0] = KS_KEY;
3971 key_name[1] = (key & 0xff);
3972 }
3973 p = find_termcode(key_name);
3974 if (p != NULL)
3975 {
3976 if (stringval != NULL)
3977 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003978 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003979 }
3980 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003981 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003984 varp = get_varp_scope(&(options[opt_idx]), scope);
3985
3986 if (flagsp != NULL)
3987 // Return the P_xxxx option flags.
3988 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
3990 if (options[opt_idx].flags & P_STRING)
3991 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003992 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003993 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (stringval != NULL)
3995 {
zeertzjq51f0bc32022-05-09 13:33:39 +01003996 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01003997 *stringval = str2special_save(*(char_u **)(varp), FALSE,
3998 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004000 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004001 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004002 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 *stringval = vim_strsave(*(char_u **)(varp));
4007 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004008 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
4010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004011 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004012 return (options[opt_idx].flags & P_NUM)
4013 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 if (options[opt_idx].flags & P_NUM)
4015 *numval = *(long *)varp;
4016 else
4017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004018 // Special case: 'modified' is b_changed, but we also want to consider
4019 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 if ((int *)varp == &curbuf->b_changed)
4021 *numval = curbufIsChanged();
4022 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004023 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004025 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026}
4027#endif
4028
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004029#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004030/*
4031 * Returns the option attributes and its value. Unlike the above function it
4032 * will return either global value or local value of the option depending on
4033 * what was requested, but it will never return global value if it was
4034 * requested to return local one and vice versa. Neither it will return
4035 * buffer-local value if it was requested to return window-local one.
4036 *
4037 * Pretends that option is absent if it is not present in the requested scope
4038 * (i.e. has no global, window-local or buffer-local value depending on
4039 * opt_type). Uses
4040 *
4041 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004042 * 0 hidden or unknown option, also option that does not have requested
4043 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004044 * see SOPT_* in vim.h for other flags
4045 *
4046 * Possible opt_type values: see SREQ_* in vim.h
4047 */
4048 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004049get_option_value_strict(
4050 char_u *name,
4051 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004052 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004053 int opt_type,
4054 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004055{
4056 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004057 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004058 struct vimoption *p;
4059 int r = 0;
4060
4061 opt_idx = findoption(name);
4062 if (opt_idx < 0)
4063 return 0;
4064
4065 p = &(options[opt_idx]);
4066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004067 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004068 if (p->var == NULL)
4069 return 0;
4070
4071 if (p->flags & P_BOOL)
4072 r |= SOPT_BOOL;
4073 else if (p->flags & P_NUM)
4074 r |= SOPT_NUM;
4075 else if (p->flags & P_STRING)
4076 r |= SOPT_STRING;
4077
4078 if (p->indir == PV_NONE)
4079 {
4080 if (opt_type == SREQ_GLOBAL)
4081 r |= SOPT_GLOBAL;
4082 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004083 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004084 }
4085 else
4086 {
4087 if (p->indir & PV_BOTH)
4088 r |= SOPT_GLOBAL;
4089 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004090 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004091
4092 if (p->indir & PV_WIN)
4093 {
4094 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004095 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004096 else
4097 r |= SOPT_WIN;
4098 }
4099 else if (p->indir & PV_BUF)
4100 {
4101 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004102 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004103 else
4104 r |= SOPT_BUF;
4105 }
4106 }
4107
4108 if (stringval == NULL)
4109 return r;
4110
4111 if (opt_type == SREQ_GLOBAL)
4112 varp = p->var;
4113 else
4114 {
4115 if (opt_type == SREQ_BUF)
4116 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004117 // Special case: 'modified' is b_changed, but we also want to
4118 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004119 if (p->indir == PV_MOD)
4120 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004121 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004122 varp = NULL;
4123 }
4124#ifdef FEAT_CRYPT
4125 else if (p->indir == PV_KEY)
4126 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004127 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004128 *stringval = NULL;
4129 varp = NULL;
4130 }
4131#endif
4132 else
4133 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004134 buf_T *save_curbuf = curbuf;
4135
4136 // only getting a pointer, no need to use aucmd_prepbuf()
4137 curbuf = (buf_T *)from;
4138 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004139 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004140 curbuf = save_curbuf;
4141 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004142 }
4143 }
4144 else if (opt_type == SREQ_WIN)
4145 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004146 win_T *save_curwin = curwin;
4147
4148 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004149 curbuf = curwin->w_buffer;
4150 varp = get_varp(p);
4151 curwin = save_curwin;
4152 curbuf = curwin->w_buffer;
4153 }
4154 if (varp == p->var)
4155 return (r | SOPT_UNSET);
4156 }
4157
4158 if (varp != NULL)
4159 {
4160 if (p->flags & P_STRING)
4161 *stringval = vim_strsave(*(char_u **)(varp));
4162 else if (p->flags & P_NUM)
4163 *numval = *(long *) varp;
4164 else
4165 *numval = *(int *)varp;
4166 }
4167
4168 return r;
4169}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004170
4171/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004172 * Iterate over options. First argument is a pointer to a pointer to a
4173 * structure inside options[] array, second is option type like in the above
4174 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004175 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004176 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004177 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004178 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004179 * first argument to NULL.
4180 *
4181 * Returns full option name for current option on each call.
4182 */
4183 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004184option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004185{
4186 struct vimoption *ret = NULL;
4187 do
4188 {
4189 if (*option == NULL)
4190 *option = (void *) options;
4191 else if (((struct vimoption *) (*option))->fullname == NULL)
4192 {
4193 *option = NULL;
4194 return NULL;
4195 }
4196 else
4197 *option = (void *) (((struct vimoption *) (*option)) + 1);
4198
4199 ret = ((struct vimoption *) (*option));
4200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004201 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004202 if (ret->var == NULL)
4203 {
4204 ret = NULL;
4205 continue;
4206 }
4207
4208 switch (opt_type)
4209 {
4210 case SREQ_GLOBAL:
4211 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4212 ret = NULL;
4213 break;
4214 case SREQ_BUF:
4215 if (!(ret->indir & PV_BUF))
4216 ret = NULL;
4217 break;
4218 case SREQ_WIN:
4219 if (!(ret->indir & PV_WIN))
4220 ret = NULL;
4221 break;
4222 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004223 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004224 return NULL;
4225 }
4226 }
4227 while (ret == NULL);
4228
4229 return (char_u *)ret->fullname;
4230}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004231#endif
4232
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004234 * Return the flags for the option at 'opt_idx'.
4235 */
4236 long_u
4237get_option_flags(int opt_idx)
4238{
4239 return options[opt_idx].flags;
4240}
4241
4242/*
4243 * Set a flag for the option at 'opt_idx'.
4244 */
4245 void
4246set_option_flag(int opt_idx, long_u flag)
4247{
4248 options[opt_idx].flags |= flag;
4249}
4250
4251/*
4252 * Clear a flag for the option at 'opt_idx'.
4253 */
4254 void
4255clear_option_flag(int opt_idx, long_u flag)
4256{
4257 options[opt_idx].flags &= ~flag;
4258}
4259
4260/*
4261 * Returns TRUE if the option at 'opt_idx' is a global option
4262 */
4263 int
4264is_global_option(int opt_idx)
4265{
4266 return options[opt_idx].indir == PV_NONE;
4267}
4268
4269/*
4270 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4271 * local value.
4272 */
4273 int
4274is_global_local_option(int opt_idx)
4275{
4276 return options[opt_idx].indir & PV_BOTH;
4277}
4278
4279/*
4280 * Returns TRUE if the option at 'opt_idx' is a window-local option
4281 */
4282 int
4283is_window_local_option(int opt_idx)
4284{
4285 return options[opt_idx].var == VAR_WIN;
4286}
4287
4288/*
4289 * Returns TRUE if the option at 'opt_idx' is a hidden option
4290 */
4291 int
4292is_hidden_option(int opt_idx)
4293{
4294 return options[opt_idx].var == NULL;
4295}
4296
4297#if defined(FEAT_CRYPT) || defined(PROTO)
4298/*
4299 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4300 */
4301 int
4302is_crypt_key_option(int opt_idx)
4303{
4304 return options[opt_idx].indir == PV_KEY;
4305}
4306#endif
4307
4308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 * Set the value of option "name".
4310 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004311 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004312 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004314 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004315set_option_value(
4316 char_u *name,
4317 long number,
4318 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004319 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320{
4321 int opt_idx;
4322 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004323 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324
4325 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004326 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004327 {
4328 int key;
4329
4330 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004331 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004332 {
4333 char_u key_name[2];
4334
4335 if (key < 0)
4336 {
4337 key_name[0] = KEY2TERMCAP0(key);
4338 key_name[1] = KEY2TERMCAP1(key);
4339 }
4340 else
4341 {
4342 key_name[0] = KS_KEY;
4343 key_name[1] = (key & 0xff);
4344 }
4345 add_termcode(key_name, string, FALSE);
4346 if (full_screen)
4347 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004348 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01004349 return NULL;
4350 }
4351
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004352 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 else
4355 {
4356 flags = options[opt_idx].flags;
4357#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004358 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004360 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004361 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004362 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004365 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004366 return set_string_option(opt_idx, string, opt_flags);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004367
4368 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
4369 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004371 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004373 int idx;
4374
4375 // Either we are given a string or we are setting option
4376 // to zero.
4377 for (idx = 0; string[idx] == '0'; ++idx)
4378 ;
4379 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004380 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004381 // There's another character after zeros or the string
4382 // is empty. In both cases, we are trying to set a
4383 // num option using a string.
4384 semsg(_(e_number_required_after_str_equal_str),
4385 name, string);
4386 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004387
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004390 if (flags & P_NUM)
4391 return set_num_option(opt_idx, varp, number,
4392 NULL, 0, opt_flags);
4393 else
4394 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004396
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004398 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399}
4400
4401/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004402 * Call set_option_value() and when an error is returned report it.
4403 */
4404 void
4405set_option_value_give_err(
4406 char_u *name,
4407 long number,
4408 char_u *string,
4409 int opt_flags) // OPT_LOCAL or 0 (both)
4410{
4411 char *errmsg = set_option_value(name, number, string, opt_flags);
4412
4413 if (errmsg != NULL)
4414 emsg(_(errmsg));
4415}
4416
4417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 * Get the terminal code for a terminal option.
4419 * Returns NULL when not found.
4420 */
4421 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004422get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423{
4424 int opt_idx;
4425 char_u *varp;
4426
4427 if (tname[0] != 't' || tname[1] != '_' ||
4428 tname[2] == NUL || tname[3] == NUL)
4429 return NULL;
4430 if ((opt_idx = findoption(tname)) >= 0)
4431 {
4432 varp = get_varp(&(options[opt_idx]));
4433 if (varp != NULL)
4434 varp = *(char_u **)(varp);
4435 return varp;
4436 }
4437 return find_termcode(tname + 2);
4438}
4439
4440 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004441get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442{
4443 int i;
4444
4445 i = findoption((char_u *)"hl");
4446 if (i >= 0)
4447 return options[i].def_val[VI_DEFAULT];
4448 return (char_u *)NULL;
4449}
4450
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004451 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004452get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004453{
4454 int i;
4455
4456 i = findoption((char_u *)"enc");
4457 if (i >= 0)
4458 return options[i].def_val[VI_DEFAULT];
4459 return (char_u *)NULL;
4460}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004461
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004462#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004463 int
4464is_option_allocated(char *name)
4465{
4466 int idx = findoption((char_u *)name);
4467
4468 return idx >= 0 && (options[idx].flags & P_ALLOCED);
4469}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004470#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004471
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004472#if defined(FEAT_EVAL) || defined(PROTO)
4473/*
4474 * Return TRUE if "name" is a string option.
4475 * Returns FALSE if option "name" does not exist.
4476 */
4477 int
4478is_string_option(char_u *name)
4479{
4480 int idx = findoption(name);
4481
4482 return idx >= 0 && (options[idx].flags & P_STRING);
4483}
4484#endif
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486/*
4487 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004488 * When "has_lt" is true there is a '<' before "*arg_arg".
4489 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 */
4491 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004492find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004494 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004496 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
4498 /*
4499 * Don't use get_special_key_code() for t_xx, we don't want it to call
4500 * add_termcap_entry().
4501 */
4502 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4503 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004504 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004506 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004508 key = find_special_key(&arg, &modifiers,
4509 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004510 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 key = 0;
4512 }
4513 return key;
4514}
4515
4516/*
4517 * if 'all' == 0: show changed options
4518 * if 'all' == 1: show all normal options
4519 * if 'all' == 2: show all terminal options
4520 */
4521 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004522showoptions(
4523 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004524 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525{
4526 struct vimoption *p;
4527 int col;
4528 int isterm;
4529 char_u *varp;
4530 struct vimoption **items;
4531 int item_count;
4532 int run;
4533 int row, rows;
4534 int cols;
4535 int i;
4536 int len;
4537
4538#define INC 20
4539#define GAP 3
4540
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004541 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 if (items == NULL)
4543 return;
4544
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004545 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004547 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004549 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004551 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004553 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
4555 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004556 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 * 1. display the short items
4558 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004559 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 */
4561 for (run = 1; run <= 2 && !got_int; ++run)
4562 {
4563 /*
4564 * collect the items in items[]
4565 */
4566 item_count = 0;
4567 for (p = &options[0]; p->fullname != NULL; p++)
4568 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004569 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004570 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004571 continue;
4572
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 varp = NULL;
4574 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004575 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {
4577 if (p->indir != PV_NONE && !isterm)
4578 varp = get_varp_scope(p, opt_flags);
4579 }
4580 else
4581 varp = get_varp(p);
4582 if (varp != NULL
4583 && ((all == 2 && isterm)
4584 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004585 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004587 if (opt_flags & OPT_ONECOLUMN)
4588 len = Columns;
4589 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004590 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 else
4592 {
4593 option_value2string(p, opt_flags);
4594 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4595 }
4596 if ((len <= INC - GAP && run == 1) ||
4597 (len > INC - GAP && run == 2))
4598 items[item_count++] = p;
4599 }
4600 }
4601
4602 /*
4603 * display the items
4604 */
4605 if (run == 1)
4606 {
4607 cols = (Columns + GAP - 3) / INC;
4608 if (cols == 0)
4609 cols = 1;
4610 rows = (item_count + cols - 1) / cols;
4611 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004612 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 rows = item_count;
4614 for (row = 0; row < rows && !got_int; ++row)
4615 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004616 msg_putchar('\n'); // go to next line
4617 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 break;
4619 col = 0;
4620 for (i = row; i < item_count; i += rows)
4621 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004622 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 showoneopt(items[i], opt_flags);
4624 col += INC;
4625 }
4626 out_flush();
4627 ui_breakcheck();
4628 }
4629 }
4630 vim_free(items);
4631}
4632
4633/*
4634 * Return TRUE if option "p" has its default value.
4635 */
4636 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004637optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638{
4639 int dvi;
4640
4641 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004642 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004643 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004645 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004647 // the cast to long is required for Manx C, long_i is
4648 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004649 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004650 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4652}
4653
4654/*
4655 * showoneopt: show the value of one option
4656 * must not be called with a hidden option!
4657 */
4658 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004659showoneopt(
4660 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004661 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004663 char_u *varp;
4664 int save_silent = silent_mode;
4665
4666 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004667 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668
4669 varp = get_varp_scope(p, opt_flags);
4670
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004671 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4673 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004674 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004676 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004678 msg_puts(" ");
4679 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 if (!(p->flags & P_BOOL))
4681 {
4682 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004683 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 option_value2string(p, opt_flags);
4685 msg_outtrans(NameBuff);
4686 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004687
4688 silent_mode = save_silent;
4689 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690}
4691
4692/*
4693 * Write modified options as ":set" commands to a file.
4694 *
4695 * There are three values for "opt_flags":
4696 * OPT_GLOBAL: Write global option values and fresh values of
4697 * buffer-local options (used for start of a session
4698 * file).
4699 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4700 * curwin (used for a vimrc file).
4701 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4702 * and local values for window-local options of
4703 * curwin. Local values are also written when at the
4704 * default value, because a modeline or autocommand
4705 * may have set them when doing ":edit file" and the
4706 * user has set them back at the default or fresh
4707 * value.
4708 * When "local_only" is TRUE, don't write fresh
4709 * values, only local values (for ":mkview").
4710 * (fresh value = value used for a new buffer or window for a local option).
4711 *
4712 * Return FAIL on error, OK otherwise.
4713 */
4714 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004715makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004718 char_u *varp; // currently used value
4719 char_u *varp_fresh; // local value
4720 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 char *cmd;
4722 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004723 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724
4725 /*
4726 * The options that don't have a default (terminal name, columns, lines)
4727 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004728 * Do the loop over "options[]" twice: once for options with the
4729 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004731 for (pri = 1; pri >= 0; --pri)
4732 {
4733 for (p = &options[0]; !istermoption(p); p++)
4734 if (!(p->flags & P_NO_MKRC)
4735 && !istermoption(p)
4736 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004738 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4740 continue;
4741
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004742 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4743 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4745 continue;
4746
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004747 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004749 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 continue;
4751
Bram Moolenaard23b7142021-04-17 21:04:34 +02004752 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4753 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004754 continue;
4755
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 round = 2;
4757 if (p->indir != PV_NONE)
4758 {
4759 if (p->var == VAR_WIN)
4760 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004761 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 if (!(opt_flags & OPT_LOCAL))
4763 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004764 // When fresh value of window-local option is not at the
4765 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4767 {
4768 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004769 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
4771 round = 1;
4772 varp_local = varp;
4773 varp = varp_fresh;
4774 }
4775 }
4776 }
4777 }
4778
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004779 // Round 1: fresh value for window-local options.
4780 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 for ( ; round <= 2; varp = varp_local, ++round)
4782 {
4783 if (round == 1 || (opt_flags & OPT_GLOBAL))
4784 cmd = "set";
4785 else
4786 cmd = "setlocal";
4787
4788 if (p->flags & P_BOOL)
4789 {
4790 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4791 return FAIL;
4792 }
4793 else if (p->flags & P_NUM)
4794 {
4795 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4796 return FAIL;
4797 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004798 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004800 int do_endif = FALSE;
4801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004802 // Don't set 'syntax' and 'filetype' again if the value is
4803 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004804 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004805#if defined(FEAT_SYN_HL)
4806 p->indir == PV_SYN ||
4807#endif
4808 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
4810 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4811 *(char_u **)(varp)) < 0
4812 || put_eol(fd) < 0)
4813 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004814 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004817 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004819 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 if (put_line(fd, "endif") == FAIL)
4822 return FAIL;
4823 }
4824 }
4825 }
4826 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 return OK;
4829}
4830
4831#if defined(FEAT_FOLDING) || defined(PROTO)
4832/*
4833 * Generate set commands for the local fold options only. Used when
4834 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4835 */
4836 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004837makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004839 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004841 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 == FAIL
4843# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004844 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004846 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 == FAIL
4848 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4849 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4850 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4851 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4852 )
4853 return FAIL;
4854
4855 return OK;
4856}
4857#endif
4858
4859 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004860put_setstring(
4861 FILE *fd,
4862 char *cmd,
4863 char *name,
4864 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004865 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866{
4867 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004868 char_u *buf = NULL;
4869 char_u *part = NULL;
4870 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4873 return FAIL;
4874 if (*valuep != NULL)
4875 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004876 // Output 'pastetoggle' as key names. For other
4877 // options some characters have to be escaped with
4878 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 if (valuep == &p_pt)
4880 {
4881 s = *valuep;
4882 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01004883 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 return FAIL;
4885 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004886 // expand the option value, replace $HOME by ~
4887 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004889 int size = (int)STRLEN(*valuep) + 1;
4890
4891 // replace home directory in the whole option value into "buf"
4892 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004893 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004894 goto fail;
4895 home_replace(NULL, *valuep, buf, size, FALSE);
4896
4897 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004898 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004899 // can be expanded when read back.
4900 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4901 && vim_strchr(*valuep, ',') != NULL)
4902 {
4903 part = alloc(size);
4904 if (part == NULL)
4905 goto fail;
4906
4907 // write line break to clear the option, e.g. ':set rtp='
4908 if (put_eol(fd) == FAIL)
4909 goto fail;
4910
4911 p = buf;
4912 while (*p != NUL)
4913 {
4914 // for each comma separated option part, append value to
4915 // the option, :set rtp+=value
4916 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4917 goto fail;
4918 (void)copy_option_part(&p, part, size, ",");
4919 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4920 goto fail;
4921 }
4922 vim_free(buf);
4923 vim_free(part);
4924 return OK;
4925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004927 {
4928 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004930 }
4931 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933 else if (put_escstr(fd, *valuep, 2) == FAIL)
4934 return FAIL;
4935 }
4936 if (put_eol(fd) < 0)
4937 return FAIL;
4938 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004939fail:
4940 vim_free(buf);
4941 vim_free(part);
4942 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943}
4944
4945 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004946put_setnum(
4947 FILE *fd,
4948 char *cmd,
4949 char *name,
4950 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
4952 long wc;
4953
4954 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4955 return FAIL;
4956 if (wc_use_keyname((char_u *)valuep, &wc))
4957 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004958 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4960 return FAIL;
4961 }
4962 else if (fprintf(fd, "%ld", *valuep) < 0)
4963 return FAIL;
4964 if (put_eol(fd) < 0)
4965 return FAIL;
4966 return OK;
4967}
4968
4969 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004970put_setbool(
4971 FILE *fd,
4972 char *cmd,
4973 char *name,
4974 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004976 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004977 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4979 || put_eol(fd) < 0)
4980 return FAIL;
4981 return OK;
4982}
4983
4984/*
4985 * Clear all the terminal options.
4986 * If the option has been allocated, free the memory.
4987 * Terminal options are never hidden or indirect.
4988 */
4989 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004990clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 /*
4993 * Reset a few things before clearing the old options. This may cause
4994 * outputting a few things that the terminal doesn't understand, but the
4995 * screen will be cleared later, so this is OK.
4996 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004997 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004998 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005000 // When starting the GUI close the display opened for the clipboard.
5001 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 if (gui.starting)
5003 clear_xterm_clip();
5004#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005005 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005007 free_termoptions();
5008}
5009
5010 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005011free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005012{
5013 struct vimoption *p;
5014
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005015 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 if (istermoption(p))
5017 {
5018 if (p->flags & P_ALLOCED)
5019 free_string_option(*(char_u **)(p->var));
5020 if (p->flags & P_DEF_ALLOCED)
5021 free_string_option(p->def_val[VI_DEFAULT]);
5022 *(char_u **)(p->var) = empty_option;
5023 p->def_val[VI_DEFAULT] = empty_option;
5024 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005025#ifdef FEAT_EVAL
5026 // remember where the option was cleared
5027 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
5030 clear_termcodes();
5031}
5032
5033/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005034 * Free the string for one term option, if it was allocated.
5035 * Set the string to empty_option and clear allocated flag.
5036 * "var" points to the option value.
5037 */
5038 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005039free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005040{
5041 struct vimoption *p;
5042
5043 for (p = &options[0]; p->fullname != NULL; p++)
5044 if (p->var == var)
5045 {
5046 if (p->flags & P_ALLOCED)
5047 free_string_option(*(char_u **)(p->var));
5048 *(char_u **)(p->var) = empty_option;
5049 p->flags &= ~P_ALLOCED;
5050 break;
5051 }
5052}
5053
5054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 * Set the terminal option defaults to the current value.
5056 * Used after setting the terminal name.
5057 */
5058 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005059set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
5061 struct vimoption *p;
5062
5063 for (p = &options[0]; p->fullname != NULL; p++)
5064 {
5065 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5066 {
5067 if (p->flags & P_DEF_ALLOCED)
5068 {
5069 free_string_option(p->def_val[VI_DEFAULT]);
5070 p->flags &= ~P_DEF_ALLOCED;
5071 }
5072 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5073 if (p->flags & P_ALLOCED)
5074 {
5075 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005076 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 }
5078 }
5079 }
5080}
5081
5082/*
5083 * return TRUE if 'p' starts with 't_'
5084 */
5085 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005086istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087{
5088 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5089}
5090
Bram Moolenaardac13472019-09-16 21:06:21 +02005091/*
5092 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5093 */
5094 int
5095istermoption_idx(int opt_idx)
5096{
5097 return istermoption(&options[opt_idx]);
5098}
5099
Bram Moolenaar113e1072019-01-20 15:30:40 +01005100#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005102 * Unset local option value, similar to ":set opt<".
5103 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005104 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005105unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005106{
5107 struct vimoption *p;
5108 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005109 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005110
5111 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005112 if (opt_idx < 0)
5113 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005114 p = &(options[opt_idx]);
5115
5116 switch ((int)p->indir)
5117 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005118 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005119 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005120 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005121 break;
5122 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005123 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005124 break;
5125 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005126 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005127 break;
5128 case PV_AR:
5129 buf->b_p_ar = -1;
5130 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005131 case PV_BKC:
5132 clear_string_option(&buf->b_p_bkc);
5133 buf->b_bkc_flags = 0;
5134 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005135 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005136 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005137 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005138 case PV_TC:
5139 clear_string_option(&buf->b_p_tc);
5140 buf->b_tc_flags = 0;
5141 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005142 case PV_SISO:
5143 curwin->w_p_siso = -1;
5144 break;
5145 case PV_SO:
5146 curwin->w_p_so = -1;
5147 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005148#ifdef FEAT_FIND_ID
5149 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005150 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005151 break;
5152 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005153 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005154 break;
5155#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005156 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005157 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005158 break;
5159 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005160 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005161 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005162#ifdef FEAT_COMPL_FUNC
5163 case PV_TSRFU:
5164 clear_string_option(&buf->b_p_tsrfu);
5165 break;
5166#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005167 case PV_FP:
5168 clear_string_option(&buf->b_p_fp);
5169 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005170#ifdef FEAT_QUICKFIX
5171 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005172 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005173 break;
5174 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005175 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005176 break;
5177 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005178 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005179 break;
5180#endif
5181#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5182 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005183 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005184 break;
5185#endif
5186#if defined(FEAT_CRYPT)
5187 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005188 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005189 break;
5190#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005191#ifdef FEAT_LINEBREAK
5192 case PV_SBR:
5193 clear_string_option(&((win_T *)from)->w_p_sbr);
5194 break;
5195#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005196#ifdef FEAT_STL_OPT
5197 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005198 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005199 break;
5200#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005201 case PV_UL:
5202 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5203 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005204 case PV_LW:
5205 clear_string_option(&buf->b_p_lw);
5206 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005207 case PV_MENC:
5208 clear_string_option(&buf->b_p_menc);
5209 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005210 case PV_LCS:
5211 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005212 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005213 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005214 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005215 case PV_FCS:
5216 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005217 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005218 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005219 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005220 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005221 clear_string_option(&((win_T *)from)->w_p_ve);
5222 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005223 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005224 }
5225}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005226#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005227
5228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005230 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 */
5232 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005233get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005235 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
5237 if (p->var == VAR_WIN)
5238 return (char_u *)GLOBAL_WO(get_varp(p));
5239 return p->var;
5240 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005241 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {
5243 switch ((int)p->indir)
5244 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005245 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005247 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5248 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5249 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005251 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5252 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5253 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005254 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005255 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005256 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005257 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5258 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005260 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5261 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005263 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5264 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005265#ifdef FEAT_COMPL_FUNC
5266 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5267#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005268#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5269 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5270#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005271#if defined(FEAT_CRYPT)
5272 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5273#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005274#ifdef FEAT_LINEBREAK
5275 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5276#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005277#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005278 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005279#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005280 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005281 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005282 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005283 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005284 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005285 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005286 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005289 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
5291 return get_varp(p);
5292}
5293
5294/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005295 * Get pointer to option variable at 'opt_idx', depending on local or global
5296 * scope.
5297 */
5298 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005299get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005300{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005301 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005302}
5303
5304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 * Get pointer to option variable.
5306 */
5307 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005308get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005310 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 if (p->var == NULL)
5312 return NULL;
5313
5314 switch ((int)p->indir)
5315 {
5316 case PV_NONE: return p->var;
5317
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005318 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005319 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005321 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005323 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005325 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005327 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005329 case PV_TC: return *curbuf->b_p_tc != NUL
5330 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005331 case PV_BKC: return *curbuf->b_p_bkc != NUL
5332 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005333 case PV_SISO: return curwin->w_p_siso >= 0
5334 ? (char_u *)&(curwin->w_p_siso) : p->var;
5335 case PV_SO: return curwin->w_p_so >= 0
5336 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005338 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005340 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5342#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005343 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005345 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005347#ifdef FEAT_COMPL_FUNC
5348 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5349 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5350#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005351 case PV_FP: return *curbuf->b_p_fp != NUL
5352 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005354 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005356 case PV_GP: return *curbuf->b_p_gp != NUL
5357 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5358 case PV_MP: return *curbuf->b_p_mp != NUL
5359 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005361#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5362 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5363 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5364#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005365#if defined(FEAT_CRYPT)
5366 case PV_CM: return *curbuf->b_p_cm != NUL
5367 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5368#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005369#ifdef FEAT_LINEBREAK
5370 case PV_SBR: return *curwin->w_p_sbr != NUL
5371 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5372#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005373#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005374 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005375 ? (char_u *)&(curwin->w_p_stl) : p->var;
5376#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005377 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5378 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005379 case PV_LW: return *curbuf->b_p_lw != NUL
5380 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005381 case PV_MENC: return *curbuf->b_p_menc != NUL
5382 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383#ifdef FEAT_ARABIC
5384 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5385#endif
5386 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005387 case PV_LCS: return *curwin->w_p_lcs != NUL
5388 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005389 case PV_FCS: return *curwin->w_p_fcs != NUL
5390 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005391 case PV_VE: return *curwin->w_p_ve != NUL
5392 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005393#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005394 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5395#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005396#ifdef FEAT_SYN_HL
5397 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5398 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005399 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005400 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402#ifdef FEAT_DIFF
5403 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5404#endif
5405#ifdef FEAT_FOLDING
5406 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5407 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5408 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5409 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5410 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5411 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5412 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5413# ifdef FEAT_EVAL
5414 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5415 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5416# endif
5417 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5418#endif
5419 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005420 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005421#ifdef FEAT_LINEBREAK
5422 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005425 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005426#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5428#endif
5429#ifdef FEAT_RIGHTLEFT
5430 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5431 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5432#endif
5433 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5434 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5435#ifdef FEAT_LINEBREAK
5436 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005437 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5438 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005440 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005442 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005443#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005444 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5445 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5446#endif
5447#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005448 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5449 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5450 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452
5453 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5454 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5457 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5459 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5461 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5462 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01005463 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466#ifdef FEAT_FOLDING
5467 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005470#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005471 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005473#ifdef FEAT_COMPL_FUNC
5474 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005475 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005476#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005477#ifdef FEAT_EVAL
5478 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005481 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005487 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5489 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5490 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5491 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5492#ifdef FEAT_FIND_ID
5493# ifdef FEAT_EVAL
5494 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5495# endif
5496#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01005497#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5499 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5500#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005501#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005502 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504#ifdef FEAT_CRYPT
5505 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5509 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5510 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5511 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5512 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005514 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5521#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005522 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005523 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5524#endif
5525#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005526 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5527 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5528 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005529 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530#endif
5531 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5532 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5533 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5534 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005535#ifdef FEAT_PERSISTENT_UNDO
5536 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5539#ifdef FEAT_KEYMAP
5540 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5541#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005542#ifdef FEAT_SIGNS
5543 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5544#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005545#ifdef FEAT_VARTABS
5546 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5547 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5548#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005549 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005551 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 return (char_u *)&(curbuf->b_p_wm);
5553}
5554
5555/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005556 * Return a pointer to the variable for option at 'opt_idx'
5557 */
5558 char_u *
5559get_option_var(int opt_idx)
5560{
5561 return options[opt_idx].var;
5562}
5563
Dominique Pelle748b3082022-01-08 12:41:16 +00005564#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005565/*
5566 * Return the full name of the option at 'opt_idx'
5567 */
5568 char_u *
5569get_option_fullname(int opt_idx)
5570{
5571 return (char_u *)options[opt_idx].fullname;
5572}
Dominique Pelle748b3082022-01-08 12:41:16 +00005573#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005574
5575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 * Get the value of 'equalprg', either the buffer-local one or the global one.
5577 */
5578 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005579get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 if (*curbuf->b_p_ep == NUL)
5582 return p_ep;
5583 return curbuf->b_p_ep;
5584}
5585
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586/*
5587 * Copy options from one window to another.
5588 * Used when splitting a window.
5589 */
5590 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005591win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592{
5593 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5594 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005595 after_copy_winopt(wp_to);
5596}
5597
5598/*
5599 * After copying window options: update variables depending on options.
5600 */
5601 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00005602after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005603{
5604#ifdef FEAT_LINEBREAK
5605 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005606#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005607#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005608 fill_culopt_flags(NULL, wp);
5609 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005610#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005611 set_chars_option(wp, &wp->w_p_lcs, TRUE);
5612 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005613}
5614
5615 static char_u *
5616copy_option_val(char_u *val)
5617{
5618 if (val == empty_option)
5619 return empty_option; // no need to allocate memory
5620 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
5623/*
5624 * Copy the options from one winopt_T to another.
5625 * Doesn't free the old option values in "to", use clear_winopt() for that.
5626 * The 'scroll' option is not copied, because it depends on the window height.
5627 * The 'previewwindow' option is reset, there can be only one preview window.
5628 */
5629 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005630copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632#ifdef FEAT_ARABIC
5633 to->wo_arab = from->wo_arab;
5634#endif
5635 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005636 to->wo_lcs = copy_option_val(from->wo_lcs);
5637 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005639 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005640 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02005641 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005642#ifdef FEAT_LINEBREAK
5643 to->wo_nuw = from->wo_nuw;
5644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645#ifdef FEAT_RIGHTLEFT
5646 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005647 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005649#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005650 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01005651#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005652#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005653 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005656#ifdef FEAT_DIFF
5657 to->wo_wrap_save = from->wo_wrap_save;
5658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659#ifdef FEAT_LINEBREAK
5660 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005661 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005662 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005664 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005666 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005667 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005668 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005669#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005670 to->wo_spell = from->wo_spell;
5671#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005672#ifdef FEAT_SYN_HL
5673 to->wo_cuc = from->wo_cuc;
5674 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005675 to->wo_culopt = copy_option_val(from->wo_culopt);
5676 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678#ifdef FEAT_DIFF
5679 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005680 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005682#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005683 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005684 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005685#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005686#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005687 to->wo_twk = copy_option_val(from->wo_twk);
5688 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690#ifdef FEAT_FOLDING
5691 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005692 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005694 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005695 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 to->wo_fml = from->wo_fml;
5697 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005698 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005699 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005700 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005701 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 to->wo_fdn = from->wo_fdn;
5703# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005704 to->wo_fde = copy_option_val(from->wo_fde);
5705 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005707 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005709#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005710 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005711#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005712
5713#ifdef FEAT_EVAL
5714 // Copy the script context so that we know where the value was last set.
5715 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5716 sizeof(to->wo_script_ctx));
5717#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005718 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719}
5720
5721/*
5722 * Check string options in a window for a NULL value.
5723 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005724 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005725check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726{
5727 check_winopt(&win->w_onebuf_opt);
5728 check_winopt(&win->w_allbuf_opt);
5729}
5730
5731/*
5732 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5733 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005734 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005735check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
5737#ifdef FEAT_FOLDING
5738 check_string_option(&wop->wo_fdi);
5739 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005740 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741# ifdef FEAT_EVAL
5742 check_string_option(&wop->wo_fde);
5743 check_string_option(&wop->wo_fdt);
5744# endif
5745 check_string_option(&wop->wo_fmr);
5746#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005747#ifdef FEAT_SIGNS
5748 check_string_option(&wop->wo_scl);
5749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750#ifdef FEAT_RIGHTLEFT
5751 check_string_option(&wop->wo_rlc);
5752#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005753#ifdef FEAT_LINEBREAK
5754 check_string_option(&wop->wo_sbr);
5755#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005756#ifdef FEAT_STL_OPT
5757 check_string_option(&wop->wo_stl);
5758#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005759#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005760 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005761 check_string_option(&wop->wo_cc);
5762#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005763#ifdef FEAT_CONCEAL
5764 check_string_option(&wop->wo_cocu);
5765#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005766#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005767 check_string_option(&wop->wo_twk);
5768 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005769#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005770#ifdef FEAT_LINEBREAK
5771 check_string_option(&wop->wo_briopt);
5772#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005773 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005774 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005775 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005776 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777}
5778
5779/*
5780 * Free the allocated memory inside a winopt_T.
5781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005783clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
5785#ifdef FEAT_FOLDING
5786 clear_string_option(&wop->wo_fdi);
5787 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005788 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789# ifdef FEAT_EVAL
5790 clear_string_option(&wop->wo_fde);
5791 clear_string_option(&wop->wo_fdt);
5792# endif
5793 clear_string_option(&wop->wo_fmr);
5794#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005795#ifdef FEAT_SIGNS
5796 clear_string_option(&wop->wo_scl);
5797#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005798#ifdef FEAT_LINEBREAK
5799 clear_string_option(&wop->wo_briopt);
5800#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005801 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802#ifdef FEAT_RIGHTLEFT
5803 clear_string_option(&wop->wo_rlc);
5804#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005805#ifdef FEAT_LINEBREAK
5806 clear_string_option(&wop->wo_sbr);
5807#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005808#ifdef FEAT_STL_OPT
5809 clear_string_option(&wop->wo_stl);
5810#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005811#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005812 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005813 clear_string_option(&wop->wo_cc);
5814#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005815#ifdef FEAT_CONCEAL
5816 clear_string_option(&wop->wo_cocu);
5817#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005818#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005819 clear_string_option(&wop->wo_twk);
5820 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005821#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005822 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005823 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005824 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825}
5826
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005827#ifdef FEAT_EVAL
5828// Index into the options table for a buffer-local option enum.
5829static int buf_opt_idx[BV_COUNT];
5830# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5831
5832/*
5833 * Initialize buf_opt_idx[] if not done already.
5834 */
5835 static void
5836init_buf_opt_idx(void)
5837{
5838 static int did_init_buf_opt_idx = FALSE;
5839 int i;
5840
5841 if (did_init_buf_opt_idx)
5842 return;
5843 did_init_buf_opt_idx = TRUE;
5844 for (i = 0; !istermoption_idx(i); i++)
5845 if (options[i].indir & PV_BUF)
5846 buf_opt_idx[options[i].indir & PV_MASK] = i;
5847}
5848#else
5849# define COPY_OPT_SCTX(buf, bv)
5850#endif
5851
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852/*
5853 * Copy global option values to local options for one buffer.
5854 * Used when creating a new buffer and sometimes when entering a buffer.
5855 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005856 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5858 * appropriate.
5859 * BCO_NOHELP Don't copy the values to a help buffer.
5860 */
5861 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005862buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863{
5864 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005865 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 int dont_do_help;
5867 int did_isk = FALSE;
5868
5869 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 * Skip this when the option defaults have not been set yet. Happens when
5871 * main() allocates the first buffer.
5872 */
5873 if (p_cpo != NULL)
5874 {
5875 /*
5876 * Always copy when entering and 'cpo' contains 'S'.
5877 * Don't copy when already initialized.
5878 * Don't copy when 'cpo' contains 's' and not entering.
5879 * 'S' BCO_ENTER initialized 's' should_copy
5880 * yes yes X X TRUE
5881 * yes no yes X FALSE
5882 * no X yes X FALSE
5883 * X no no yes FALSE
5884 * X no no no TRUE
5885 * no yes no X TRUE
5886 */
5887 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5888 && (buf->b_p_initialized
5889 || (!(flags & BCO_ENTER)
5890 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5891 should_copy = FALSE;
5892
5893 if (should_copy || (flags & BCO_ALWAYS))
5894 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005895#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005896 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005897 init_buf_opt_idx();
5898#endif
5899 // Don't copy the options specific to a help buffer when
5900 // BCO_NOHELP is given or the options were initialized already
5901 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5903 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005904 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 {
5906 save_p_isk = buf->b_p_isk;
5907 buf->b_p_isk = NULL;
5908 }
5909 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005910 * Always free the allocated strings. If not already initialized,
5911 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 */
5913 if (!buf->b_p_initialized)
5914 {
5915 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005916 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005919 switch (*p_ffs)
5920 {
5921 case 'm':
5922 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5923 case 'd':
5924 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5925 case 'u':
5926 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5927 default:
5928 buf->b_p_ff = vim_strsave(p_ff);
5929 }
5930 if (buf->b_p_ff != NULL)
5931 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 buf->b_p_bh = empty_option;
5933 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 }
5935 else
5936 free_buf_options(buf, FALSE);
5937
5938 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005939 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 buf->b_p_ai_nopaste = p_ai_nopaste;
5941 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005942 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005944 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 buf->b_p_tw_nopaste = p_tw_nopaste;
5946 buf->b_p_tw_nobin = p_tw_nobin;
5947 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005948 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 buf->b_p_wm_nopaste = p_wm_nopaste;
5950 buf->b_p_wm_nobin = p_wm_nobin;
5951 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005952 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005953 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005954 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005955 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005956 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005958 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005960 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005962 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 buf->b_p_ml_nobin = p_ml_nobin;
5964 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005965 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005966 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005967 buf->b_p_swf = FALSE;
5968 else
5969 {
5970 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00005971 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005974 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005975#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005976 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005977 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005979#ifdef FEAT_COMPL_FUNC
5980 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005981 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005982 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005983 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005984 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005985 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005986#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005987#ifdef FEAT_EVAL
5988 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005989 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005990 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005993 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005995#ifdef FEAT_VARTABS
5996 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005997 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005998 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02005999 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006000 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006001 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006002 buf->b_p_vsts_nopaste = p_vsts_nopaste
6003 ? vim_strsave(p_vsts_nopaste) : NULL;
6004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006006 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006008 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009#ifdef FEAT_FOLDING
6010 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006011 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012#endif
6013 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006014 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006015 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006016 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006017 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6018 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006020 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006022 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006024 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006026 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006027
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006029 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006031 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006033 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006034 buf->b_p_cinsd = vim_strsave(p_cinsd);
6035 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006036
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006037 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006040 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006042 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006044 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006046 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006048 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006049 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006050 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006051#endif
6052#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006053 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006054 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006055 (void)compile_cap_prog(&buf->b_s);
6056 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006057 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006058 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006059 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006060 buf->b_s.b_p_spo = vim_strsave(p_spo);
6061 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006063#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006065 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006067 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006069 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006070#if defined(FEAT_EVAL)
6071 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006072 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074#ifdef FEAT_CRYPT
6075 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006076 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006079 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080#ifdef FEAT_KEYMAP
6081 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006082 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 buf->b_kmap_state |= KEYMAP_INIT;
6084#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006085#ifdef FEAT_TERMINAL
6086 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006087 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006088#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006089 // This isn't really an option, but copying the langmap and IME
6090 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006092 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006094 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006096 // options that are normally global but also have a local value
6097 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006099 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006100 buf->b_p_bkc = empty_option;
6101 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102#ifdef FEAT_QUICKFIX
6103 buf->b_p_gp = empty_option;
6104 buf->b_p_mp = empty_option;
6105 buf->b_p_efm = empty_option;
6106#endif
6107 buf->b_p_ep = empty_option;
6108 buf->b_p_kp = empty_option;
6109 buf->b_p_path = empty_option;
6110 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006111 buf->b_p_tc = empty_option;
6112 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113#ifdef FEAT_FIND_ID
6114 buf->b_p_def = empty_option;
6115 buf->b_p_inc = empty_option;
6116# ifdef FEAT_EVAL
6117 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006118 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119# endif
6120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 buf->b_p_dict = empty_option;
6122 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006123#ifdef FEAT_COMPL_FUNC
6124 buf->b_p_tsrfu = empty_option;
6125#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006126 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006127 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006128#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6129 buf->b_p_bexpr = empty_option;
6130#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006131#if defined(FEAT_CRYPT)
6132 buf->b_p_cm = empty_option;
6133#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006134#ifdef FEAT_PERSISTENT_UNDO
6135 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006136 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006137#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006138 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006139 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140
6141 /*
6142 * Don't copy the options set by ex_help(), use the saved values,
6143 * when going from a help buffer to a non-help buffer.
6144 * Don't touch these at all when BCO_NOHELP is used and going from
6145 * or to a help buffer.
6146 */
6147 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006150#ifdef FEAT_VARTABS
6151 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006152 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006153 else
6154 buf->b_p_vts_array = NULL;
6155#endif
6156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 else
6158 {
6159 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006160 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 did_isk = TRUE;
6162 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006163 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006164#ifdef FEAT_VARTABS
6165 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006166 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006167 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006168 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006169 else
6170 buf->b_p_vts_array = NULL;
6171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 if (buf->b_p_bt[0] == 'h')
6174 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006176 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 }
6178 }
6179
6180 /*
6181 * When the options should be copied (ignoring BCO_ALWAYS), set the
6182 * flag that indicates that the options have been initialized.
6183 */
6184 if (should_copy)
6185 buf->b_p_initialized = TRUE;
6186 }
6187
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006188 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 if (did_isk)
6190 (void)buf_init_chartab(buf, FALSE);
6191}
6192
6193/*
6194 * Reset the 'modifiable' option and its default value.
6195 */
6196 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006197reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
6199 int opt_idx;
6200
6201 curbuf->b_p_ma = FALSE;
6202 p_ma = FALSE;
6203 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006204 if (opt_idx >= 0)
6205 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206}
6207
6208/*
6209 * Set the global value for 'iminsert' to the local value.
6210 */
6211 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006212set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213{
6214 p_iminsert = curbuf->b_p_iminsert;
6215}
6216
6217/*
6218 * Set the global value for 'imsearch' to the local value.
6219 */
6220 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006221set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 p_imsearch = curbuf->b_p_imsearch;
6224}
6225
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226static int expand_option_idx = -1;
6227static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6228static int expand_option_flags = 0;
6229
6230 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006231set_context_in_set_cmd(
6232 expand_T *xp,
6233 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006234 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006237 long_u flags = 0; // init for GCC
6238 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 char_u *p;
6240 char_u *s;
6241 int is_term_option = FALSE;
6242 int key;
6243
6244 expand_option_flags = opt_flags;
6245
6246 xp->xp_context = EXPAND_SETTINGS;
6247 if (*arg == NUL)
6248 {
6249 xp->xp_pattern = arg;
6250 return;
6251 }
6252 p = arg + STRLEN(arg) - 1;
6253 if (*p == ' ' && *(p - 1) != '\\')
6254 {
6255 xp->xp_pattern = p + 1;
6256 return;
6257 }
6258 while (p > arg)
6259 {
6260 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006261 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 if (*p == ' ' || *p == ',')
6263 {
6264 while (s > arg && *(s - 1) == '\\')
6265 --s;
6266 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006267 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 if (*p == ' ' && ((p - s) & 1) == 0)
6269 {
6270 ++p;
6271 break;
6272 }
6273 --p;
6274 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006275 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 {
6277 xp->xp_context = EXPAND_BOOL_SETTINGS;
6278 p += 2;
6279 }
6280 if (STRNCMP(p, "inv", 3) == 0)
6281 {
6282 xp->xp_context = EXPAND_BOOL_SETTINGS;
6283 p += 3;
6284 }
6285 xp->xp_pattern = arg = p;
6286 if (*arg == '<')
6287 {
6288 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006289 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 return;
6291 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006292 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 {
6294 xp->xp_context = EXPAND_NOTHING;
6295 return;
6296 }
6297 nextchar = *++p;
6298 is_term_option = TRUE;
6299 expand_option_name[2] = KEY2TERMCAP0(key);
6300 expand_option_name[3] = KEY2TERMCAP1(key);
6301 }
6302 else
6303 {
6304 if (p[0] == 't' && p[1] == '_')
6305 {
6306 p += 2;
6307 if (*p != NUL)
6308 ++p;
6309 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006310 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 nextchar = *++p;
6312 is_term_option = TRUE;
6313 expand_option_name[2] = p[-2];
6314 expand_option_name[3] = p[-1];
6315 }
6316 else
6317 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006318 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6320 p++;
6321 if (*p == NUL)
6322 return;
6323 nextchar = *p;
6324 *p = NUL;
6325 opt_idx = findoption(arg);
6326 *p = nextchar;
6327 if (opt_idx == -1 || options[opt_idx].var == NULL)
6328 {
6329 xp->xp_context = EXPAND_NOTHING;
6330 return;
6331 }
6332 flags = options[opt_idx].flags;
6333 if (flags & P_BOOL)
6334 {
6335 xp->xp_context = EXPAND_NOTHING;
6336 return;
6337 }
6338 }
6339 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006340 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6342 {
6343 ++p;
6344 nextchar = '=';
6345 }
6346 if ((nextchar != '=' && nextchar != ':')
6347 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6348 {
6349 xp->xp_context = EXPAND_UNSUCCESSFUL;
6350 return;
6351 }
6352 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6353 {
6354 xp->xp_context = EXPAND_OLD_SETTING;
6355 if (is_term_option)
6356 expand_option_idx = -1;
6357 else
6358 expand_option_idx = opt_idx;
6359 xp->xp_pattern = p + 1;
6360 return;
6361 }
6362 xp->xp_context = EXPAND_NOTHING;
6363 if (is_term_option || (flags & P_NUM))
6364 return;
6365
6366 xp->xp_pattern = p + 1;
6367
6368 if (flags & P_EXPAND)
6369 {
6370 p = options[opt_idx].var;
6371 if (p == (char_u *)&p_bdir
6372 || p == (char_u *)&p_dir
6373 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006374 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377#ifdef FEAT_SESSION
6378 || p == (char_u *)&p_vdir
6379#endif
6380 )
6381 {
6382 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006383 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 xp->xp_backslash = XP_BS_THREE;
6385 else
6386 xp->xp_backslash = XP_BS_ONE;
6387 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006388 else if (p == (char_u *)&p_ft)
6389 {
6390 xp->xp_context = EXPAND_FILETYPE;
6391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 else
6393 {
6394 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006395 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 if (p == (char_u *)&p_tags)
6397 xp->xp_backslash = XP_BS_THREE;
6398 else
6399 xp->xp_backslash = XP_BS_ONE;
6400 }
6401 }
6402
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006403 // For an option that is a list of file names, find the start of the
6404 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6406 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006407 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 if (*p == ' ' || *p == ',')
6409 {
6410 s = p;
6411 while (s > xp->xp_pattern && *(s - 1) == '\\')
6412 --s;
6413 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6414 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6415 {
6416 xp->xp_pattern = p + 1;
6417 break;
6418 }
6419 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006420
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006421#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006422 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006423 if (options[opt_idx].var == (char_u *)&p_sps
6424 && STRNCMP(p, "file:", 5) == 0)
6425 {
6426 xp->xp_pattern = p + 5;
6427 break;
6428 }
6429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431}
6432
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006433/*
6434 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
6435 *
6436 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
6437 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
6438 *
6439 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
6440 * regular expression 'regmatch', then stores the match in matches[idx] and
6441 * returns TRUE.
6442 *
6443 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6444 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
6445 *
6446 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6447 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
6448 */
6449 static int
6450match_str(
6451 char_u *str,
6452 regmatch_T *regmatch,
6453 char_u **matches,
6454 int idx,
6455 int test_only,
6456 int fuzzy,
6457 char_u *fuzzystr,
6458 fuzmatch_str_T *fuzmatch)
6459{
6460 if (!fuzzy)
6461 {
6462 if (vim_regexec(regmatch, str, (colnr_T)0))
6463 {
6464 if (!test_only)
6465 matches[idx] = vim_strsave(str);
6466 return TRUE;
6467 }
6468 }
6469 else
6470 {
6471 int score;
6472
6473 score = fuzzy_match_str(str, fuzzystr);
6474 if (score != 0)
6475 {
6476 if (!test_only)
6477 {
6478 fuzmatch[idx].idx = idx;
6479 fuzmatch[idx].str = vim_strsave(str);
6480 fuzmatch[idx].score = score;
6481 }
6482
6483 return TRUE;
6484 }
6485 }
6486
6487 return FALSE;
6488}
6489
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006491ExpandSettings(
6492 expand_T *xp,
6493 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006494 char_u *fuzzystr,
6495 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01006496 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006497 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006499 int num_normal = 0; // Nr of matching non-term-code settings
6500 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 int opt_idx;
6502 int match;
6503 int count = 0;
6504 char_u *str;
6505 int loop;
6506 int is_term_opt;
6507 char_u name_buf[MAX_KEY_NAME_LEN];
6508 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006509 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006510 int fuzzy;
6511 fuzmatch_str_T *fuzmatch = NULL;
6512
Christian Brabandtcb747892022-05-08 21:10:56 +01006513 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006515 // do this loop twice:
6516 // loop == 0: count the number of matching options
6517 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 for (loop = 0; loop <= 1; ++loop)
6519 {
6520 regmatch->rm_ic = ic;
6521 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6522 {
K.Takataeeec2542021-06-02 13:28:16 +02006523 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006524 {
6525 if (match_str((char_u *)names[match], regmatch, *matches,
6526 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 {
6528 if (loop == 0)
6529 num_normal++;
6530 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006531 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 }
6535 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6536 opt_idx++)
6537 {
6538 if (options[opt_idx].var == NULL)
6539 continue;
6540 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6541 && !(options[opt_idx].flags & P_BOOL))
6542 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006543 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 if (is_term_opt && num_normal > 0)
6545 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006546
6547 if (match_str(str, regmatch, *matches, count, (loop == 0),
6548 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 {
6550 if (loop == 0)
6551 {
6552 if (is_term_opt)
6553 num_term++;
6554 else
6555 num_normal++;
6556 }
6557 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006558 count++;
6559 }
6560 else if (!fuzzy && options[opt_idx].shortname != NULL
6561 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006562 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006563 {
6564 // Compare against the abbreviated option name (for regular
6565 // expression match). Fuzzy matching (previous if) already
6566 // matches against both the expanded and abbreviated names.
6567 if (loop == 0)
6568 {
6569 if (is_term_opt)
6570 num_term++;
6571 else
6572 num_normal++;
6573 }
6574 else
6575 (*matches)[count++] = vim_strsave(str);
6576 }
6577 else if (is_term_opt)
6578 {
6579 name_buf[0] = '<';
6580 name_buf[1] = 't';
6581 name_buf[2] = '_';
6582 name_buf[3] = str[2];
6583 name_buf[4] = str[3];
6584 name_buf[5] = '>';
6585 name_buf[6] = NUL;
6586
6587 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6588 fuzzy, fuzzystr, fuzmatch))
6589 {
6590 if (loop == 0)
6591 num_term++;
6592 else
6593 count++;
6594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 }
6596 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006597
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 /*
6599 * Check terminal key codes, these are not in the option table
6600 */
6601 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6602 {
6603 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6604 {
6605 if (!isprint(str[0]) || !isprint(str[1]))
6606 continue;
6607
6608 name_buf[0] = 't';
6609 name_buf[1] = '_';
6610 name_buf[2] = str[0];
6611 name_buf[3] = str[1];
6612 name_buf[4] = NUL;
6613
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006614 if (match_str(name_buf, regmatch, *matches, count,
6615 (loop == 0), fuzzy, fuzzystr, fuzmatch))
6616 {
6617 if (loop == 0)
6618 num_term++;
6619 else
6620 count++;
6621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 else
6623 {
6624 name_buf[0] = '<';
6625 name_buf[1] = 't';
6626 name_buf[2] = '_';
6627 name_buf[3] = str[0];
6628 name_buf[4] = str[1];
6629 name_buf[5] = '>';
6630 name_buf[6] = NUL;
6631
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006632 if (match_str(name_buf, regmatch, *matches, count,
6633 (loop == 0), fuzzy, fuzzystr,
6634 fuzmatch))
6635 {
6636 if (loop == 0)
6637 num_term++;
6638 else
6639 count++;
6640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 }
6642 }
6643
6644 /*
6645 * Check special key names.
6646 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006647 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6649 {
6650 name_buf[0] = '<';
6651 STRCPY(name_buf + 1, str);
6652 STRCAT(name_buf, ">");
6653
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006654 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6655 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 {
6657 if (loop == 0)
6658 num_term++;
6659 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006660 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 }
6662 }
6663 }
6664 if (loop == 0)
6665 {
6666 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006667 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006669 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 else
6671 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006672 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006674 *matches = ALLOC_MULT(char_u *, *numMatches);
6675 if (*matches == NULL)
6676 {
6677 *matches = (char_u **)"";
6678 return FAIL;
6679 }
6680 }
6681 else
6682 {
6683 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
6684 if (fuzmatch == NULL)
6685 {
6686 *matches = (char_u **)"";
6687 return FAIL;
6688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 }
6690 }
6691 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006692
6693 if (fuzzy &&
6694 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
6695 return FAIL;
6696
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 return OK;
6698}
6699
6700 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006701ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006703 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 char_u *buf;
6705
6706 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006707 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 if (*file == NULL)
6709 return FAIL;
6710
6711 /*
6712 * For a terminal key code expand_option_idx is < 0.
6713 */
6714 if (expand_option_idx < 0)
6715 {
6716 var = find_termcode(expand_option_name + 2);
6717 if (var == NULL)
6718 expand_option_idx = findoption(expand_option_name);
6719 }
6720
6721 if (expand_option_idx >= 0)
6722 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006723 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 option_value2string(&options[expand_option_idx], expand_option_flags);
6725 var = NameBuff;
6726 }
6727 else if (var == NULL)
6728 var = (char_u *)"";
6729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006730 // A backslash is required before some characters. This is the reverse of
6731 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 buf = vim_strsave_escaped(var, escape_chars);
6733
6734 if (buf == NULL)
6735 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006736 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 return FAIL;
6738 }
6739
6740#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006741 // For MS-Windows et al. we don't double backslashes at the start and
6742 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006743 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 if (var[0] == '\\' && var[1] == '\\'
6745 && expand_option_idx >= 0
6746 && (options[expand_option_idx].flags & P_EXPAND)
6747 && vim_isfilec(var[2])
6748 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006749 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750#endif
6751
6752 *file[0] = buf;
6753 *num_file = 1;
6754 return OK;
6755}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756
6757/*
6758 * Get the value for the numeric or string option *opp in a nice format into
6759 * NameBuff[]. Must not be called with a hidden option!
6760 */
6761 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006762option_value2string(
6763 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006764 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
6766 char_u *varp;
6767
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006768 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769
6770 if (opp->flags & P_NUM)
6771 {
6772 long wc = 0;
6773
6774 if (wc_use_keyname(varp, &wc))
6775 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6776 else if (wc != 0)
6777 STRCPY(NameBuff, transchar((int)wc));
6778 else
6779 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6780 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006781 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {
6783 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006784 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 NameBuff[0] = NUL;
6786#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006787 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006788 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 STRCPY(NameBuff, "*****");
6790#endif
6791 else if (opp->flags & P_EXPAND)
6792 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006793 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 else if ((char_u **)opp->var == &p_pt)
6795 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6796 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006797 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 }
6799}
6800
6801/*
6802 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6803 * printed as a keyname.
6804 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6805 */
6806 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006807wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808{
6809 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6810 {
6811 *wcp = *(long *)varp;
6812 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6813 return TRUE;
6814 }
6815 return FALSE;
6816}
6817
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 * Return TRUE if "x" is present in 'shortmess' option, or
6820 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6821 */
6822 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006823shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006825 return p_shm != NULL &&
6826 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 || (vim_strchr(p_shm, 'a') != NULL
6828 && vim_strchr((char_u *)SHM_A, x) != NULL));
6829}
6830
6831/*
6832 * paste_option_changed() - Called after p_paste was set or reset.
6833 */
6834 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006835paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836{
6837 static int old_p_paste = FALSE;
6838 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006839 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840#ifdef FEAT_CMDL_INFO
6841 static int save_ru = 0;
6842#endif
6843#ifdef FEAT_RIGHTLEFT
6844 static int save_ri = 0;
6845 static int save_hkmap = 0;
6846#endif
6847 buf_T *buf;
6848
6849 if (p_paste)
6850 {
6851 /*
6852 * Paste switched from off to on.
6853 * Save the current values, so they can be restored later.
6854 */
6855 if (!old_p_paste)
6856 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006857 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006858 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {
6860 buf->b_p_tw_nopaste = buf->b_p_tw;
6861 buf->b_p_wm_nopaste = buf->b_p_wm;
6862 buf->b_p_sts_nopaste = buf->b_p_sts;
6863 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006864 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006865#ifdef FEAT_VARTABS
6866 if (buf->b_p_vsts_nopaste)
6867 vim_free(buf->b_p_vsts_nopaste);
6868 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6869 ? vim_strsave(buf->b_p_vsts) : NULL;
6870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 }
6872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006873 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006875 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876#ifdef FEAT_CMDL_INFO
6877 save_ru = p_ru;
6878#endif
6879#ifdef FEAT_RIGHTLEFT
6880 save_ri = p_ri;
6881 save_hkmap = p_hkmap;
6882#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006883 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006884 p_ai_nopaste = p_ai;
6885 p_et_nopaste = p_et;
6886 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 p_tw_nopaste = p_tw;
6888 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006889#ifdef FEAT_VARTABS
6890 if (p_vsts_nopaste)
6891 vim_free(p_vsts_nopaste);
6892 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 }
6895
6896 /*
6897 * Always set the option values, also when 'paste' is set when it is
6898 * already on.
6899 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006900 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006901 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006903 buf->b_p_tw = 0; // textwidth is 0
6904 buf->b_p_wm = 0; // wrapmargin is 0
6905 buf->b_p_sts = 0; // softtabstop is 0
6906 buf->b_p_ai = 0; // no auto-indent
6907 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006908#ifdef FEAT_VARTABS
6909 if (buf->b_p_vsts)
6910 free_string_option(buf->b_p_vsts);
6911 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006912 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 }
6915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006916 // set global options
6917 p_sm = 0; // no showmatch
6918 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006921 status_redraw_all(); // redraw to remove the ruler
6922 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923#endif
6924#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006925 p_ri = 0; // no reverse insert
6926 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006928 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 p_tw = 0;
6930 p_wm = 0;
6931 p_sts = 0;
6932 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006933#ifdef FEAT_VARTABS
6934 if (p_vsts)
6935 free_string_option(p_vsts);
6936 p_vsts = empty_option;
6937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 }
6939
6940 /*
6941 * Paste switched from on to off: Restore saved values.
6942 */
6943 else if (old_p_paste)
6944 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006945 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006946 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 {
6948 buf->b_p_tw = buf->b_p_tw_nopaste;
6949 buf->b_p_wm = buf->b_p_wm_nopaste;
6950 buf->b_p_sts = buf->b_p_sts_nopaste;
6951 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006952 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006953#ifdef FEAT_VARTABS
6954 if (buf->b_p_vsts)
6955 free_string_option(buf->b_p_vsts);
6956 buf->b_p_vsts = buf->b_p_vsts_nopaste
6957 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006958 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006959 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006960 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006961 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006962 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 }
6965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006966 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006968 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006971 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 p_ru = save_ru;
6973#endif
6974#ifdef FEAT_RIGHTLEFT
6975 p_ri = save_ri;
6976 p_hkmap = save_hkmap;
6977#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006978 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006979 p_ai = p_ai_nopaste;
6980 p_et = p_et_nopaste;
6981 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 p_tw = p_tw_nopaste;
6983 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006984#ifdef FEAT_VARTABS
6985 if (p_vsts)
6986 free_string_option(p_vsts);
6987 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 }
6990
6991 old_p_paste = p_paste;
6992}
6993
6994/*
6995 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6996 *
6997 * Reset 'compatible' and set the values for options that didn't get set yet
6998 * to the Vim defaults.
6999 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007000 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 */
7002 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007003vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007005 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007006 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007007 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008
7009 if (!option_was_set((char_u *)"cp"))
7010 {
7011 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007012 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7014 set_option_default(opt_idx, OPT_FREE, FALSE);
7015 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007016 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007018
7019 if (fname != NULL)
7020 {
7021 p = vim_getenv(envname, &dofree);
7022 if (p == NULL)
7023 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007024 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007025 p = FullName_save(fname, FALSE);
7026 if (p != NULL)
7027 {
7028 vim_setenv(envname, p);
7029 vim_free(p);
7030 }
7031 }
7032 else if (dofree)
7033 vim_free(p);
7034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035}
7036
7037/*
7038 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7039 */
7040 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007041change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007043 int opt_idx;
7044
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 if (p_cp != on)
7046 {
7047 p_cp = on;
7048 compatible_set();
7049 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007050 opt_idx = findoption((char_u *)"cp");
7051 if (opt_idx >= 0)
7052 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053}
7054
7055/*
7056 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007057 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 */
7059 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007060option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061{
7062 int idx;
7063
7064 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007065 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 return FALSE;
7067 if (options[idx].flags & P_WAS_SET)
7068 return TRUE;
7069 return FALSE;
7070}
7071
7072/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007073 * Reset the flag indicating option "name" was set.
7074 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007075 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007076reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007077{
7078 int idx = findoption(name);
7079
7080 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007081 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007082 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007083 return OK;
7084 }
7085 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007086}
7087
7088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 * compatible_set() - Called when 'compatible' has been set or unset.
7090 *
7091 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7092 * flag) to a Vi compatible value.
7093 * When 'compatible' is unset: Set all options that have a different default
7094 * for Vim (without the P_VI_DEF flag) to that default.
7095 */
7096 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007097compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098{
7099 int opt_idx;
7100
Bram Moolenaardac13472019-09-16 21:06:21 +02007101 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7103 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7104 set_option_default(opt_idx, OPT_FREE, p_cp);
7105 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007106 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107}
7108
Bram Moolenaardac13472019-09-16 21:06:21 +02007109#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111/*
7112 * fill_breakat_flags() -- called when 'breakat' changes value.
7113 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007114 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007115fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007117 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 int i;
7119
7120 for (i = 0; i < 256; i++)
7121 breakat_flags[i] = FALSE;
7122
7123 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007124 for (p = p_breakat; *p; p++)
7125 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127#endif
7128
7129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 * Check if backspacing over something is allowed.
7131 */
7132 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007133can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007134 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007136#ifdef FEAT_JOB_CHANNEL
7137 if (what == BS_START && bt_prompt(curbuf))
7138 return FALSE;
7139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 switch (*p_bs)
7141 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007142 case '3': return TRUE;
7143 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 case '1': return (what != BS_START);
7145 case '0': return FALSE;
7146 }
7147 return vim_strchr(p_bs, what) != NULL;
7148}
7149
7150/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007151 * Return the effective 'scrolloff' value for the current window, using the
7152 * global value when appropriate.
7153 */
7154 long
7155get_scrolloff_value(void)
7156{
7157 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7158}
7159
7160/*
7161 * Return the effective 'sidescrolloff' value for the current window, using the
7162 * global value when appropriate.
7163 */
7164 long
7165get_sidescrolloff_value(void)
7166{
7167 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7168}
7169
7170/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007171 * Get the local or global value of 'backupcopy'.
7172 */
7173 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007174get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007175{
7176 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7177}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007178
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007179#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007180/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007181 * Get the local or global value of 'formatlistpat'.
7182 */
7183 char_u *
7184get_flp_value(buf_T *buf)
7185{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007186 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7187 return p_flp;
7188 return buf->b_p_flp;
7189}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007190#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007191
7192/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007193 * Get the local or global value of the 'virtualedit' flags.
7194 */
7195 unsigned int
7196get_ve_flags(void)
7197{
Gary Johnson51ad8502021-08-03 18:33:08 +02007198 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7199 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007200}
7201
Bram Moolenaaree857022019-11-09 23:26:40 +01007202#if defined(FEAT_LINEBREAK) || defined(PROTO)
7203/*
7204 * Get the local or global value of 'showbreak'.
7205 */
7206 char_u *
7207get_showbreak_value(win_T *win)
7208{
7209 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7210 return p_sbr;
7211 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7212 return empty_option;
7213 return win->w_p_sbr;
7214}
7215#endif
7216
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007217#if defined(FEAT_EVAL) || defined(PROTO)
7218/*
7219 * Get window or buffer local options.
7220 */
7221 dict_T *
7222get_winbuf_options(int bufopt)
7223{
7224 dict_T *d;
7225 int opt_idx;
7226
7227 d = dict_alloc();
7228 if (d == NULL)
7229 return NULL;
7230
Bram Moolenaardac13472019-09-16 21:06:21 +02007231 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007232 {
7233 struct vimoption *opt = &options[opt_idx];
7234
7235 if ((bufopt && (opt->indir & PV_BUF))
7236 || (!bufopt && (opt->indir & PV_WIN)))
7237 {
7238 char_u *varp = get_varp(opt);
7239
7240 if (varp != NULL)
7241 {
7242 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007243 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007244 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007245 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007246 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007247 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007248 }
7249 }
7250 }
7251
7252 return d;
7253}
7254#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007255
Bram Moolenaardac13472019-09-16 21:06:21 +02007256#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007257/*
7258 * This is called when 'culopt' is changed
7259 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007260 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007261fill_culopt_flags(char_u *val, win_T *wp)
7262{
7263 char_u *p;
7264 char_u culopt_flags_new = 0;
7265
7266 if (val == NULL)
7267 p = wp->w_p_culopt;
7268 else
7269 p = val;
7270 while (*p != NUL)
7271 {
7272 if (STRNCMP(p, "line", 4) == 0)
7273 {
7274 p += 4;
7275 culopt_flags_new |= CULOPT_LINE;
7276 }
7277 else if (STRNCMP(p, "both", 4) == 0)
7278 {
7279 p += 4;
7280 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7281 }
7282 else if (STRNCMP(p, "number", 6) == 0)
7283 {
7284 p += 6;
7285 culopt_flags_new |= CULOPT_NBR;
7286 }
7287 else if (STRNCMP(p, "screenline", 10) == 0)
7288 {
7289 p += 10;
7290 culopt_flags_new |= CULOPT_SCRLINE;
7291 }
7292
7293 if (*p != ',' && *p != NUL)
7294 return FAIL;
7295 if (*p == ',')
7296 ++p;
7297 }
7298
7299 // Can't have both "line" and "screenline".
7300 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7301 return FAIL;
7302 wp->w_p_culopt_flags = culopt_flags_new;
7303
7304 return OK;
7305}
7306#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007307
7308/*
7309 * Get the value of 'magic' adjusted for Vim9 script.
7310 */
7311 int
7312magic_isset(void)
7313{
7314 switch (magic_overruled)
7315 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007316 case OPTION_MAGIC_ON: return TRUE;
7317 case OPTION_MAGIC_OFF: return FALSE;
7318 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007319 }
7320#ifdef FEAT_EVAL
7321 if (in_vim9script())
7322 return TRUE;
7323#endif
7324 return p_magic;
7325}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007326
7327/*
7328 * Set the callback function value for an option that accepts a function name,
7329 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7330 * Returns OK if the option is successfully set to a function, otherwise
7331 * returns FAIL.
7332 */
7333 int
7334option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7335{
7336#ifdef FEAT_EVAL
7337 typval_T *tv;
7338 callback_T cb;
7339
7340 if (optval == NULL || *optval == NUL)
7341 {
7342 free_callback(optcb);
7343 return OK;
7344 }
7345
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007346 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007347 || (STRNCMP(optval, "function(", 9) == 0)
7348 || (STRNCMP(optval, "funcref(", 8) == 0))
7349 // Lambda expression or a funcref
7350 tv = eval_expr(optval, NULL);
7351 else
7352 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007353 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007354 if (tv == NULL)
7355 return FAIL;
7356
7357 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007358 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007359 {
7360 free_tv(tv);
7361 return FAIL;
7362 }
7363
7364 free_callback(optcb);
7365 set_callback(optcb, &cb);
7366 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007367
7368 // when using Vim9 style "import.funcname" it needs to be expanded to
7369 // "import#funcname".
7370 expand_autload_callback(optcb);
7371
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007372 return OK;
7373#else
7374 return FAIL;
7375#endif
7376}