blob: 420d36d05c239831255267d928403a502d736756 [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:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000013 * - Put it in the options array in optiondefs.h (copy an existing entry).
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000016 * - Add a PV_XX macro definition to the optiondefs.h file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000019 * - For a window string option, add code to check_win_options() and
20 * clear_winopt().
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 * - For a buffer option, add some code to buf_copy_options().
22 * - For a buffer string option, add code to check_buf_options().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000023 * - If it's a numeric option, add any necessary bounds checks to
24 * set_num_option().
25 * - If it's a list of flags, add some code in did_set_string_option(), search
26 * for WW_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 * - When adding an option with expansion (P_EXPAND), but with a different
28 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020029 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * options.txt, and any other related places.
31 * - Add an entry in runtime/optwin.vim.
32 * When making changes:
33 * - Adjust the help for the option in doc/option.txt.
34 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
35 * comment at the help for the 'compatible' option.
36 */
37
38#define IN_OPTION_C
39#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020040#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010043static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static char_u *option_expand(int opt_idx, char_u *val);
46static void didset_options(void);
47static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000050#else
51# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
52#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010053static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
54static 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 +020055static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020057static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010059static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
61static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020062static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000063static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000066static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void check_winopt(winopt_T *wop);
68static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void paste_option_changed(void);
70static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/*
73 * Initialize the options, first part.
74 *
75 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010076 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 */
78 void
Bram Moolenaar07268702018-03-01 21:57:32 +010079set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000080{
81 char_u *p;
82 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000083 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000084
85#ifdef FEAT_LANGMAP
86 langmap_init();
87#endif
88
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010089 // Be Vi compatible by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 p_cp = TRUE;
91
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010092 // Use POSIX compatibility when $VIM_POSIX is set.
Bram Moolenaar4399ef42005-02-12 14:29:27 +000093 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +000094 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +000095 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020096 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000097 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000098
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 /*
100 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 */
Bram Moolenaar7c626922005-02-07 22:01:03 +0000103 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100104#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000105 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100106 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000108 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200109#if defined(MSWIN)
110 {
111 // For MS-Windows put the path in quotes instead of escaping spaces.
112 char_u *cmd;
113 size_t len;
114
115 if (vim_strchr(p, ' ') != NULL)
116 {
117 len = STRLEN(p) + 3; // two quotes and a trailing NUL
118 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200119 if (cmd != NULL)
120 {
121 vim_snprintf((char *)cmd, len, "\"%s\"", p);
122 set_string_default("sh", cmd);
123 vim_free(cmd);
124 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200125 }
126 else
127 set_string_default("sh", p);
128 }
129#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100130 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 /*
134 * Set the default for 'backupskip' to include environment variables for
135 * temp files.
136 */
137 {
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100138#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100140#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100142#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000143 int len;
144 garray_T ga;
145 int mustfree;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200146 char_u *item;
147
148 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149
150 ga_init2(&ga, 1, 100);
K.Takataeeec2542021-06-02 13:28:16 +0200151 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000153 mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100154#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100156# ifdef MACOS_X
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200157 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100158# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160# endif
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100161 else
162#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 if (p != NULL && *p != NUL)
165 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100166 // First time count the NUL, otherwise count the ','.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000167 len = (int)STRLEN(p) + 3;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200168 item = alloc(len);
169 STRCPY(item, p);
170 add_pathsep(item);
171 STRCAT(item, "*");
172 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
173 == NULL
174 && ga_grow(&ga, len) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 {
176 if (ga.ga_len > 0)
177 STRCAT(ga.ga_data, ",");
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200178 STRCAT(ga.ga_data, item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 ga.ga_len += len;
180 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200181 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 if (mustfree)
184 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 }
186 if (ga.ga_data != NULL)
187 {
188 set_string_default("bsk", ga.ga_data);
189 vim_free(ga.ga_data);
190 }
191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193 /*
194 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
195 */
196 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000197 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000199#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
200 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
201#endif
202 {
ichizok02560422022-04-05 14:18:44 +0100203#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100204 // Use amount of memory available at this moment.
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200205 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100206#elif defined(HAVE_TOTAL_MEM)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100207 // Use amount of memory available to Vim.
Bram Moolenaar914572a2007-05-01 11:37:47 +0000208 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100209#else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000210 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000213 opt_idx = findoption((char_u *)"maxmem");
214 if (opt_idx >= 0)
215 {
216#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100217 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
218 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000219#endif
220 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
221 }
222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 }
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 {
226 char_u *cdpath;
227 char_u *buf;
228 int i;
229 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000230 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100232 // Initialize the 'cdpath' option's default value.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000233 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 if (cdpath != NULL)
235 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200236 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 if (buf != NULL)
238 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100239 buf[0] = ','; // start with ",", current dir first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 j = 1;
241 for (i = 0; cdpath[i] != NUL; ++i)
242 {
243 if (vim_ispathlistsep(cdpath[i]))
244 buf[j++] = ',';
245 else
246 {
247 if (cdpath[i] == ' ' || cdpath[i] == ',')
248 buf[j++] = '\\';
249 buf[j++] = cdpath[i];
250 }
251 }
252 buf[j] = NUL;
253 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000254 if (opt_idx >= 0)
255 {
256 options[opt_idx].def_val[VI_DEFAULT] = buf;
257 options[opt_idx].flags |= P_DEF_ALLOCED;
258 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200259 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100260 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000262 if (mustfree)
263 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 }
265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
ichizok02560422022-04-05 14:18:44 +0100267#if defined(FEAT_POSTSCRIPT) && \
268 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100269 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100271# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100273# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100275# elif defined(MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100277# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# endif
280 );
281#endif
282
283#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100284 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100286# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000287 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100288# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
290
ichizok02560422022-04-05 14:18:44 +0100291# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293# endif
294 );
295#endif
296
297 /*
298 * Set all the options (except the terminal options) to their default
299 * value. Also set the global value for local options.
300 */
301 set_options_default(0);
302
matveytadbb1bf2022-02-01 17:26:12 +0000303#ifdef UNIX
304 // Force restricted-mode on for "nologin" or "false" $SHELL
305 p = get_isolated_shell_name();
306 if (p != NULL)
307 {
308 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
309 restricted = TRUE;
310 vim_free(p);
311 }
312#endif
313
Bram Moolenaar07268702018-03-01 21:57:32 +0100314#ifdef CLEAN_RUNTIMEPATH
315 if (clean_arg)
316 {
317 opt_idx = findoption((char_u *)"runtimepath");
318 if (opt_idx >= 0)
319 {
320 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
321 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
322 }
323 opt_idx = findoption((char_u *)"packpath");
324 if (opt_idx >= 0)
325 {
326 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
327 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
328 }
329 }
330#endif
331
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#ifdef FEAT_GUI
333 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100334 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#endif
336
337 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100338 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100339 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 check_buf_options(curbuf);
341 check_win_options(curwin);
342 check_options();
343
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100344 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 didset_options();
346
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000347#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100348 // Use the current chartab for the generic chartab. This is not in
349 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000350 init_spell_chartab();
351#endif
352
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /*
354 * Expand environment variables and things like "~" for the defaults.
355 * If option_expand() returns non-NULL the variable is expanded. This can
356 * only happen for non-indirect options.
357 * Also set the default to the expanded value, so ":set" does not list
358 * them.
359 * Don't set the P_ALLOCED flag, because we don't want to free the
360 * default.
361 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200362 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
364 if ((options[opt_idx].flags & P_GETTEXT)
365 && options[opt_idx].var != NULL)
366 p = (char_u *)_(*(char **)options[opt_idx].var);
367 else
368 p = option_expand(opt_idx, NULL);
369 if (p != NULL && (p = vim_strsave(p)) != NULL)
370 {
371 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100372 // VIMEXP
373 // Defaults for all expanded options are currently the same for Vi
374 // and Vim. When this changes, add some code here! Also need to
375 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (options[opt_idx].flags & P_DEF_ALLOCED)
377 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
378 options[opt_idx].def_val[VI_DEFAULT] = p;
379 options[opt_idx].flags |= P_DEF_ALLOCED;
380 }
381 }
382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386 // Detect use of mlterm.
387 // Mlterm is a terminal emulator akin to xterm that has some special
388 // abilities (bidi namely).
389 // NOTE: mlterm's author is being asked to 'set' a variable
390 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100392 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#endif
394
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200395 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Bram Moolenaar4f974752019-02-17 17:44:42 +0100397# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 /*
399 * If $LANG isn't set, try to get a good value for it. This makes the
400 * right language be used automatically. Don't do this for English.
401 */
402 if (mch_getenv((char_u *)"LANG") == NULL)
403 {
404 char buf[20];
405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100406 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
407 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
408 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
410 (LPTSTR)buf, 20);
411 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
415 STRCPY(buf, "zh_TW");
416 else if (STRNICMP(buf, "chs", 3) == 0
417 || STRNICMP(buf, "zhc", 3) == 0)
418 STRCPY(buf, "zh_CN");
419 else if (STRNICMP(buf, "jp", 2) == 0)
420 STRCPY(buf, "ja");
421 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100423 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425 }
ichizok02560422022-04-05 14:18:44 +0100426# elif defined(MACOS_CONVERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100427 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000428 mac_lang_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429# endif
430
K.Takataf883d902021-05-30 18:04:19 +0200431# ifdef MSWIN
432 // MS-Windows has builtin support for conversion to and from Unicode, using
433 // "utf-8" for 'encoding' should work best for most users.
434 p = vim_strsave((char_u *)ENC_DFLT);
435# else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100436 // enc_locale() will try to find the encoding of the current locale.
K.Takataf883d902021-05-30 18:04:19 +0200437 // This works best for properly configured systems, old and new.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 p = enc_locale();
K.Takataf883d902021-05-30 18:04:19 +0200439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 if (p != NULL)
441 {
442 char_u *save_enc;
443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100444 // Try setting 'encoding' and check if the value is valid.
K.Takataf883d902021-05-30 18:04:19 +0200445 // If not, go back to the default encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 save_enc = p_enc;
447 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000448 if (STRCMP(p_enc, "gb18030") == 0)
449 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100450 // We don't support "gb18030", but "cp936" is a good substitute
451 // for practical purposes, thus use that. It's not an alias to
452 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000453 p_enc = vim_strsave((char_u *)"cp936");
454 vim_free(p);
455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 if (mb_init() == NULL)
457 {
458 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000459 if (opt_idx >= 0)
460 {
461 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
462 options[opt_idx].flags |= P_DEF_ALLOCED;
463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464
Bram Moolenaard0573012017-10-28 21:11:06 +0200465#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100466 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000467 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100468 // Adjust the default for 'isprint' and 'iskeyword' to match
469 // latin1. Also set the defaults for when 'nocompatible' is
470 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000471 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000472 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000473 set_string_option_direct((char_u *)"isk", -1,
474 ISK_LATIN1, OPT_FREE, SID_NONE);
475 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000476 if (opt_idx >= 0)
477 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000478 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000479 if (opt_idx >= 0)
480 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000481 (void)init_chartab();
482 }
483#endif
484
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200485#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100486 // Win32 console: When GetACP() returns a different value from
487 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200488 if (
489# ifdef VIMDLL
490 (!gui.in_use && !gui.starting) &&
491# endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100492 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 {
494 char buf[50];
495
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100496 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
497 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100498 if (GetConsoleCP() == 0)
499 sprintf(buf, "cp%ld", (long)GetACP());
500 else
501 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 p_tenc = vim_strsave((char_u *)buf);
503 if (p_tenc != NULL)
504 {
505 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000506 if (opt_idx >= 0)
507 {
508 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
509 options[opt_idx].flags |= P_DEF_ALLOCED;
510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 convert_setup(&input_conv, p_tenc, p_enc);
512 convert_setup(&output_conv, p_enc, p_tenc);
513 }
514 else
515 p_tenc = empty_option;
516 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100517#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100518#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100519 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000520 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 }
523 else
524 {
525 vim_free(p_enc);
526 p_enc = save_enc;
527 }
528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
530#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100531 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 set_helplang_default(get_mess_lang());
533#endif
534}
535
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200536static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
537
538/*
539 * Set the "fileencodings" option to the default value for when 'encoding' is
540 * utf-8.
541 */
542 void
543set_fencs_unicode()
544{
545 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
546 OPT_FREE, 0);
547}
548
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549/*
550 * Set an option to its default value.
551 * This does not take care of side effects!
552 */
553 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100554set_option_default(
555 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100556 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
557 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100559 char_u *varp; // pointer to variable for current option
560 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000562 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
564
565 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
566 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100567 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
569 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
570 if (flags & P_STRING)
571 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200572 // 'fencs' default value depends on 'encoding'
573 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
574 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100575 // Use set_string_option_direct() for local options to handle
576 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200577 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200578 set_string_option_direct(NULL, opt_idx,
579 options[opt_idx].def_val[dvi], opt_flags, 0);
580 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200582 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
583 free_string_option(*(char_u **)(varp));
584 *(char_u **)varp = options[opt_idx].def_val[dvi];
585 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587 }
588 else if (flags & P_NUM)
589 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000590 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 win_comp_scroll(curwin);
592 else
593 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100594 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
595
596 if ((long *)varp == &curwin->w_p_so
597 || (long *)varp == &curwin->w_p_siso)
598 // 'scrolloff' and 'sidescrolloff' local values have a
599 // different default value than the global default.
600 *(long *)varp = -1;
601 else
602 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100603 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 if (both)
605 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100606 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
608 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100609 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 // the cast to long is required for Manx C, long_i is needed for
612 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000613 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000614#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100615 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000616 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
617 *(int *)varp = FALSE;
618#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100619 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (both)
621 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
622 *(int *)varp;
623 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100625 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000626 flagsp = insecure_flag(opt_idx, opt_flags);
627 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
629
630#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200631 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#endif
633}
634
635/*
636 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200637 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 */
639 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100641 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000645 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
Bram Moolenaardac13472019-09-16 21:06:21 +0200647 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200648 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200649 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100650 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200651# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200652 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200653 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200654# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100655 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 set_option_default(i, opt_flags, p_cp);
657
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100658 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000659 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200661 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662}
663
664/*
665 * Set the Vi-default value of a string option.
666 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100667 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100669 static void
670set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
672 char_u *p;
673 int opt_idx;
674
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100675 if (escape && vim_strchr(val, ' ') != NULL)
676 p = vim_strsave_escaped(val, (char_u *)" ");
677 else
678 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000679 if (p == NULL) // we don't want a NULL
680 return;
681
682 opt_idx = findoption((char_u *)name);
683 if (opt_idx < 0)
684 return;
685
686 if (options[opt_idx].flags & P_DEF_ALLOCED)
687 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
688 options[opt_idx].def_val[VI_DEFAULT] = p;
689 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690}
691
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100692 void
693set_string_default(char *name, char_u *val)
694{
695 set_string_default_esc(name, val, FALSE);
696}
697
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200699 * For an option value that contains comma separated items, find "newval" in
700 * "origval". Return NULL if not found.
701 */
702 static char_u *
703find_dup_item(char_u *origval, char_u *newval, long_u flags)
704{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200705 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200706 size_t newlen;
707 char_u *s;
708
709 if (origval == NULL)
710 return NULL;
711
712 newlen = STRLEN(newval);
713 for (s = origval; *s != NUL; ++s)
714 {
715 if ((!(flags & P_COMMA)
716 || s == origval
717 || (s[-1] == ',' && !(bs & 1)))
718 && STRNCMP(s, newval, newlen) == 0
719 && (!(flags & P_COMMA)
720 || s[newlen] == ','
721 || s[newlen] == NUL))
722 return s;
723 // Count backslashes. Only a comma with an even number of backslashes
724 // or a single backslash preceded by a comma before it is recognized as
725 // a separator.
726 if ((s > origval + 1
727 && s[-1] == '\\'
728 && s[-2] != ',')
729 || (s == origval + 1
730 && s[-1] == '\\'))
731 ++bs;
732 else
733 bs = 0;
734 }
735 return NULL;
736}
737
738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 * Set the Vi-default value of a number option.
740 * Used for 'lines' and 'columns'.
741 */
742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100743set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000745 int opt_idx;
746
747 opt_idx = findoption((char_u *)name);
748 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000749 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750}
751
Dominique Pelle748b3082022-01-08 12:41:16 +0000752#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200753/*
754 * Set all window-local and buffer-local options to the Vim default.
755 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200756 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200757 */
758 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200759set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200760{
761 win_T *save_curwin = curwin;
762 int i;
763
764 curwin = wp;
765 curbuf = curwin->w_buffer;
766 block_autocmds();
767
Bram Moolenaardac13472019-09-16 21:06:21 +0200768 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200769 {
770 struct vimoption *p = &(options[i]);
771 char_u *varp = get_varp_scope(p, OPT_LOCAL);
772
773 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200774 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200775 && !(options[i].flags & P_NODEFAULT)
776 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200777 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200778 }
779
780 unblock_autocmds();
781 curwin = save_curwin;
782 curbuf = curwin->w_buffer;
783}
Dominique Pelle748b3082022-01-08 12:41:16 +0000784#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200785
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000786#if defined(EXITFREE) || defined(PROTO)
787/*
788 * Free all options.
789 */
790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100791free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000792{
793 int i;
794
Bram Moolenaardac13472019-09-16 21:06:21 +0200795 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000796 {
797 if (options[i].indir == PV_NONE)
798 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100799 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100800 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000801 free_string_option(*(char_u **)options[i].var);
802 if (options[i].flags & P_DEF_ALLOCED)
803 free_string_option(options[i].def_val[VI_DEFAULT]);
804 }
805 else if (options[i].var != VAR_WIN
806 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100807 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200808 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000809 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000810 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000811 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000812}
813#endif
814
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816/*
817 * Initialize the options, part two: After getting Rows and Columns and
818 * setting 'term'.
819 */
820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100821set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000823 int idx;
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100826 * 'scroll' defaults to half the window height. The stored default is zero,
827 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000829 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000830 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000831 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 comp_col();
833
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000834 /*
835 * 'window' is only for backwards compatibility with Vi.
836 * Default is Rows - 1.
837 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000838 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000839 p_window = Rows - 1;
840 set_number_default("window", Rows - 1);
841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100842 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100843#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000844 /*
845 * If 'background' wasn't set by the user, try guessing the value,
846 * depending on the terminal name. Only need to check for terminals
847 * with a dark background, that can handle color.
848 */
849 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000850 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
851 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000853 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 // don't mark it as set, when starting the GUI it may be
855 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000856 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000859
860#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000862#endif
863#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000865#endif
866#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100867 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869}
870
871/*
872 * Initialize the options, part three: After reading the .vimrc
873 */
874 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100875set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100877#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878/*
879 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
880 * This is done after other initializations, where 'shell' might have been
881 * set, but only if they have not been set before.
882 */
883 char_u *p;
884 int idx_srr;
885 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100886# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 int idx_sp;
888 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
891 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000892 if (idx_srr < 0)
893 do_srr = FALSE;
894 else
895 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100896# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000898 if (idx_sp < 0)
899 do_sp = FALSE;
900 else
901 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100902# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200903 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (p != NULL)
905 {
906 /*
907 * Default for p_sp is "| tee", for p_srr is ">".
908 * For known shells it is changed here to include stderr.
909 */
910 if ( fnamecmp(p, "csh") == 0
911 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100912# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 || fnamecmp(p, "csh.exe") == 0
914 || fnamecmp(p, "tcsh.exe") == 0
915# endif
916 )
917 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100918# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (do_sp)
920 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100921# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100923# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
927 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (do_srr)
930 {
931 p_srr = (char_u *)">&";
932 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
933 }
934 }
Mike Williams12795022021-06-28 20:53:58 +0200935# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200936 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
937 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200938 else if ( fnamecmp(p, "powershell") == 0
939 || fnamecmp(p, "powershell.exe") == 0
940 )
941 {
942# if defined(FEAT_QUICKFIX)
943 if (do_sp)
944 {
945 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
946 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
947 }
948# endif
949 if (do_srr)
950 {
951 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
952 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
953 }
954 }
955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 else
Natanael Copa56318362021-05-06 18:46:35 +0200957 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if ( fnamecmp(p, "sh") == 0
959 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200960 || fnamecmp(p, "mksh") == 0
961 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000963 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200965 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200966 || fnamecmp(p, "ash") == 0
967 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200968 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100969# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 || fnamecmp(p, "cmd") == 0
971 || fnamecmp(p, "sh.exe") == 0
972 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200973 || fnamecmp(p, "mksh.exe") == 0
974 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000976 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 || fnamecmp(p, "bash.exe") == 0
978 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200979 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200980 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100982 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100984# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (do_sp)
986 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100987# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100989# else
Mike Williamsa3d1b292021-06-30 20:56:00 +0200990 if (fnamecmp(p, "pwsh") == 0)
991 p_sp = (char_u *)">%s 2>&1";
992 else
993 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
996 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (do_srr)
999 {
1000 p_srr = (char_u *)">%s 2>&1";
1001 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1002 }
1003 }
1004 vim_free(p);
1005 }
1006#endif
1007
Bram Moolenaar4f974752019-02-17 17:44:42 +01001008#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001010 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1011 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001013 * set, but only if they have not been set before.
1014 * Default values depend on shell (cmd.exe is default shell):
1015 *
1016 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001017 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001018 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001019 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001020 * "sh" like shells - "-c" "\""
1021 *
1022 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 */
Mike Williams12795022021-06-28 20:53:58 +02001024 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1025 {
1026 int idx_opt;
1027
1028 idx_opt = findoption((char_u *)"shcf");
1029 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1030 {
1031 p_shcf = (char_u*)"-Command";
1032 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1033 }
1034
1035 idx_opt = findoption((char_u *)"sxq");
1036 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1037 {
1038 p_sxq = (char_u*)"\"";
1039 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1040 }
1041 }
1042 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 int idx3;
1045
1046 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001047 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
1049 p_shcf = (char_u *)"-c";
1050 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1051 }
1052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001053 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001055 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
1057 p_sxq = (char_u *)"\"";
1058 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001061 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1062 {
1063 int idx3;
1064
1065 /*
1066 * cmd.exe on Windows will strip the first and last double quote given
1067 * on the command line, e.g. most of the time things like:
1068 * cmd /c "my path/to/echo" "my args to echo"
1069 * become:
1070 * my path/to/echo" "my args to echo
1071 * when executed.
1072 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001073 * To avoid this, set shellxquote to surround the command in
1074 * parenthesis. This appears to make most commands work, without
1075 * breaking commands that worked previously, such as
1076 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001077 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001078 idx3 = findoption((char_u *)"sxq");
1079 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1080 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001081 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001082 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1083 }
1084
Bram Moolenaara64ba222012-02-12 23:23:31 +01001085 idx3 = findoption((char_u *)"shcf");
1086 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1087 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001088 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001089 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1090 }
1091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#endif
1093
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001094 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001095 {
1096 int idx_ffs = findoption((char_u *)"ffs");
1097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001098 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001099 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1100 set_fileformat(default_fileformat(), OPT_LOCAL);
1101 }
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104}
1105
1106#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1107/*
1108 * When 'helplang' is still at its default value, set it to "lang".
1109 * Only the first two characters of "lang" are used.
1110 */
1111 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001112set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
1114 int idx;
1115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001116 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 return;
1118 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001119 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1120 return;
1121
1122 if (options[idx].flags & P_ALLOCED)
1123 free_string_option(p_hlg);
1124 p_hlg = vim_strsave(lang);
1125 if (p_hlg == NULL)
1126 p_hlg = empty_option;
1127 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001129 // zh_CN becomes "cn", zh_TW becomes "tw"
1130 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001131 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001132 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1133 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001134 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001135 // any C like setting, such as C.UTF-8, becomes "en"
1136 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1137 {
1138 p_hlg[0] = 'e';
1139 p_hlg[1] = 'n';
1140 }
1141 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001143 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144}
1145#endif
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147/*
1148 * 'title' and 'icon' only default to true if they have not been set or reset
1149 * in .vimrc and we can read the old value.
1150 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1151 * they can be reset. This reduces startup time when using X on a remote
1152 * machine.
1153 */
1154 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001155set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
1157 int idx1;
1158 long val;
1159
1160 /*
1161 * If GUI is (going to be) used, we can always set the window title and
1162 * icon name. Saves a bit of time, because the X11 display server does
1163 * not need to be contacted.
1164 */
1165 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001166 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
1168#ifdef FEAT_GUI
1169 if (gui.starting || gui.in_use)
1170 val = TRUE;
1171 else
1172#endif
1173 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001174 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 p_title = val;
1176 }
1177 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001178 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001180 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001182
1183#ifdef FEAT_GUI
1184 if (gui.starting || gui.in_use)
1185 val = TRUE;
1186 else
1187#endif
1188 val = mch_can_restore_icon();
1189 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1190 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001193 void
1194ex_set(exarg_T *eap)
1195{
1196 int flags = 0;
1197
1198 if (eap->cmdidx == CMD_setlocal)
1199 flags = OPT_LOCAL;
1200 else if (eap->cmdidx == CMD_setglobal)
1201 flags = OPT_GLOBAL;
1202#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001203 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001204 ex_options(eap);
1205 else
1206#endif
1207 {
1208 if (eap->forceit)
1209 flags |= OPT_ONECOLUMN;
1210 (void)do_set(eap->arg, flags);
1211 }
1212}
1213
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001214/*
1215 * :set operator types
1216 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001217typedef enum {
1218 OP_NONE = 0,
1219 OP_ADDING, // "opt+=arg"
1220 OP_PREPENDING, // "opt^=arg"
1221 OP_REMOVING, // "opt-=arg"
1222} set_op_T;
1223
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001224typedef enum {
1225 PREFIX_NO = 0, // "no" prefix
1226 PREFIX_NONE, // no prefix
1227 PREFIX_INV, // "inv" prefix
1228} set_prefix_T;
1229
1230/*
1231 * Return the prefix type for the option name in *argp.
1232 */
1233 static set_prefix_T
1234get_option_prefix(char_u **argp)
1235{
1236 int prefix = PREFIX_NONE;
1237 char_u *arg = *argp;
1238
1239 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1240 {
1241 prefix = PREFIX_NO;
1242 arg += 2;
1243 }
1244 else if (STRNCMP(arg, "inv", 3) == 0)
1245 {
1246 prefix = PREFIX_INV;
1247 arg += 3;
1248 }
1249
1250 *argp = arg;
1251 return prefix;
1252}
1253
1254/*
1255 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1256 * and the option name length in "*lenp". For a <t_xx> option, return the key
1257 * number in "*keyp".
1258 *
1259 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1260 * otherwise returns OK.
1261 */
1262 static int
1263parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1264{
1265 int key = 0;
1266 int len;
1267 int opt_idx;
1268
1269 if (*arg == '<')
1270 {
1271 opt_idx = -1;
1272 // look out for <t_>;>
1273 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1274 len = 5;
1275 else
1276 {
1277 len = 1;
1278 while (arg[len] != NUL && arg[len] != '>')
1279 ++len;
1280 }
1281 if (arg[len] != '>')
1282 return FAIL;
1283
1284 arg[len] = NUL; // put NUL after name
1285 if (arg[1] == 't' && arg[2] == '_') // could be term code
1286 opt_idx = findoption(arg + 1);
1287 arg[len++] = '>'; // restore '>'
1288 if (opt_idx == -1)
1289 key = find_key_option(arg + 1, TRUE);
1290 }
1291 else
1292 {
1293 int nextchar; // next non-white char after option name
1294
1295 len = 0;
1296 /*
1297 * The two characters after "t_" may not be alphanumeric.
1298 */
1299 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1300 len = 4;
1301 else
1302 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1303 ++len;
1304 nextchar = arg[len];
1305 arg[len] = NUL; // put NUL after name
1306 opt_idx = findoption(arg);
1307 arg[len] = nextchar; // restore nextchar
1308 if (opt_idx == -1)
1309 key = find_key_option(arg, FALSE);
1310 }
1311
1312 *keyp = key;
1313 *lenp = len;
1314 *opt_idxp = opt_idx;
1315
1316 return OK;
1317}
1318
1319/*
1320 * Get the option operator (+=, ^=, -=).
1321 */
1322 static set_op_T
1323get_opt_op(char_u *arg)
1324{
1325 set_op_T op = OP_NONE;
1326
1327 if (*arg != NUL && *(arg + 1) == '=')
1328 {
1329 if (*arg == '+')
1330 op = OP_ADDING; // "+="
1331 else if (*arg == '^')
1332 op = OP_PREPENDING; // "^="
1333 else if (*arg == '-')
1334 op = OP_REMOVING; // "-="
1335 }
1336
1337 return op;
1338}
1339
1340/*
1341 * Validate whether the value of the option in "opt_idx" can be changed.
1342 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1343 * if it can be changed.
1344 */
1345 static int
1346validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1347{
1348 // Skip all options that are not window-local (used when showing
1349 // an already loaded buffer in a window).
1350 if ((opt_flags & OPT_WINONLY)
1351 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1352 return FAIL;
1353
1354 // Skip all options that are window-local (used for :vimgrep).
1355 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1356 && options[opt_idx].var == VAR_WIN)
1357 return FAIL;
1358
1359 // Disallow changing some options from modelines.
1360 if (opt_flags & OPT_MODELINE)
1361 {
1362 if (flags & (P_SECURE | P_NO_ML))
1363 {
1364 *errmsg = e_not_allowed_in_modeline;
1365 return FAIL;
1366 }
1367 if ((flags & P_MLE) && !p_mle)
1368 {
1369 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1370 return FAIL;
1371 }
1372#ifdef FEAT_DIFF
1373 // In diff mode some options are overruled. This avoids that
1374 // 'foldmethod' becomes "marker" instead of "diff" and that
1375 // "wrap" gets set.
1376 if (curwin->w_p_diff
1377 && opt_idx >= 0 // shut up coverity warning
1378 && (
1379# ifdef FEAT_FOLDING
1380 options[opt_idx].indir == PV_FDM ||
1381# endif
1382 options[opt_idx].indir == PV_WRAP))
1383 return FAIL;
1384#endif
1385 }
1386
1387#ifdef HAVE_SANDBOX
1388 // Disallow changing some options in the sandbox
1389 if (sandbox != 0 && (flags & P_SECURE))
1390 {
1391 *errmsg = e_not_allowed_in_sandbox;
1392 return FAIL;
1393 }
1394#endif
1395
1396 return OK;
1397}
1398
Bram Moolenaar47403942022-09-21 21:12:53 +01001399/*
1400 * Part of do_set() for string options.
1401 * Returns FAIL on failure, do not process further options.
1402 */
1403 static int
1404do_set_string(
1405 int opt_idx,
1406 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001407 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001408 int nextchar,
1409 set_op_T op_arg,
1410 int flags,
1411 int cp_val,
1412 char_u *varp_arg,
1413 char *errbuf,
1414 int *value_checked,
1415 char **errmsg)
1416{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001417 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001418 set_op_T op = op_arg;
1419 char_u *varp = varp_arg;
1420 char_u *save_arg = NULL;
1421 char_u *s = NULL;
1422 char_u *oldval = NULL; // previous value if *varp
1423 char_u *newval;
1424 char_u *origval = NULL;
1425 char_u *origval_l = NULL;
1426 char_u *origval_g = NULL;
1427#if defined(FEAT_EVAL)
1428 char_u *saved_origval = NULL;
1429 char_u *saved_origval_l = NULL;
1430 char_u *saved_origval_g = NULL;
1431 char_u *saved_newval = NULL;
1432#endif
1433 unsigned newlen;
1434 int comma;
1435 char_u whichwrap[80];
1436
1437 // When using ":set opt=val" for a global option
1438 // with a local value the local value will be
1439 // reset, use the global value here.
1440 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1441 && ((int)options[opt_idx].indir & PV_BOTH))
1442 varp = options[opt_idx].var;
1443
1444 // The old value is kept until we are sure that the new value is valid.
1445 oldval = *(char_u **)varp;
1446
1447 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1448 {
1449 origval_l = *(char_u **)get_varp_scope(
1450 &(options[opt_idx]), OPT_LOCAL);
1451 origval_g = *(char_u **)get_varp_scope(
1452 &(options[opt_idx]), OPT_GLOBAL);
1453
1454 // A global-local string option might have an empty option as value to
1455 // indicate that the global value should be used.
1456 if (((int)options[opt_idx].indir & PV_BOTH)
1457 && origval_l == empty_option)
1458 origval_l = origval_g;
1459 }
1460
1461 // When setting the local value of a global option, the old value may be
1462 // the global value.
1463 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1464 origval = *(char_u **)get_varp(&options[opt_idx]);
1465 else
1466 origval = oldval;
1467
1468 if (nextchar == '&') // set to default val
1469 {
1470 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1471 ? VI_DEFAULT : VIM_DEFAULT];
1472 if ((char_u **)varp == &p_bg)
1473 {
1474 // guess the value of 'background'
1475#ifdef FEAT_GUI
1476 if (gui.in_use)
1477 newval = gui_bg_default();
1478 else
1479#endif
1480 newval = term_bg_default();
1481 }
1482 else if ((char_u **)varp == &p_fencs && enc_utf8)
1483 newval = fencs_utf8_default;
1484
1485 // expand environment variables and ~ since the default value was
1486 // already expanded, only required when an environment variable was set
1487 // later
1488 if (newval == NULL)
1489 newval = empty_option;
1490 else
1491 {
1492 s = option_expand(opt_idx, newval);
1493 if (s == NULL)
1494 s = newval;
1495 newval = vim_strsave(s);
1496 }
1497 }
1498 else if (nextchar == '<') // set to global val
1499 {
1500 newval = vim_strsave(*(char_u **)get_varp_scope(
1501 &(options[opt_idx]), OPT_GLOBAL));
1502 }
1503 else
1504 {
Bram Moolenaar6f981142022-09-22 12:48:58 +01001505 ++arg; // jump to after the '=' or ':'
Bram Moolenaar47403942022-09-21 21:12:53 +01001506
1507 /*
1508 * Set 'keywordprg' to ":help" if an empty
1509 * value was passed to :set by the user.
Bram Moolenaar47403942022-09-21 21:12:53 +01001510 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001511 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
Bram Moolenaar47403942022-09-21 21:12:53 +01001512 {
Bram Moolenaar6f981142022-09-22 12:48:58 +01001513 save_arg = arg;
zeertzjqfcba86c2022-09-22 13:57:32 +01001514 arg = (char_u *)":help";
Bram Moolenaar47403942022-09-21 21:12:53 +01001515 }
1516 /*
1517 * Convert 'backspace' number to string, for
1518 * adding, prepending and removing string.
1519 */
1520 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1521 {
1522 int i = getdigits((char_u **)varp);
1523
1524 switch (i)
1525 {
1526 case 0:
1527 *(char_u **)varp = empty_option;
1528 break;
1529 case 1:
1530 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1531 break;
1532 case 2:
1533 *(char_u **)varp = vim_strsave(
1534 (char_u *)"indent,eol,start");
1535 break;
1536 case 3:
1537 *(char_u **)varp = vim_strsave(
1538 (char_u *)"indent,eol,nostop");
1539 break;
1540 }
1541 vim_free(oldval);
1542 if (origval == oldval)
1543 origval = *(char_u **)varp;
1544 if (origval_l == oldval)
1545 origval_l = *(char_u **)varp;
1546 if (origval_g == oldval)
1547 origval_g = *(char_u **)varp;
1548 oldval = *(char_u **)varp;
1549 }
1550 /*
1551 * Convert 'whichwrap' number to string, for backwards compatibility
1552 * with Vim 3.0.
1553 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001554 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
Bram Moolenaar47403942022-09-21 21:12:53 +01001555 {
1556 *whichwrap = NUL;
Bram Moolenaar6f981142022-09-22 12:48:58 +01001557 int i = getdigits(&arg);
Bram Moolenaar47403942022-09-21 21:12:53 +01001558 if (i & 1)
1559 STRCAT(whichwrap, "b,");
1560 if (i & 2)
1561 STRCAT(whichwrap, "s,");
1562 if (i & 4)
1563 STRCAT(whichwrap, "h,l,");
1564 if (i & 8)
1565 STRCAT(whichwrap, "<,>,");
1566 if (i & 16)
1567 STRCAT(whichwrap, "[,],");
1568 if (*whichwrap != NUL) // remove trailing ,
1569 whichwrap[STRLEN(whichwrap) - 1] = NUL;
Bram Moolenaar6f981142022-09-22 12:48:58 +01001570 save_arg = arg;
1571 arg = (char_u *)whichwrap;
Bram Moolenaar47403942022-09-21 21:12:53 +01001572 }
1573 /*
1574 * Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1575 * version 3.0
1576 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001577 else if (*arg == '>' && (varp == (char_u *)&p_dir
Bram Moolenaar47403942022-09-21 21:12:53 +01001578 || varp == (char_u *)&p_bdir))
Bram Moolenaar6f981142022-09-22 12:48:58 +01001579 ++arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001580
1581 /*
1582 * Copy the new string into allocated memory.
1583 * Can't use set_string_option_direct(), because we need to remove the
1584 * backslashes.
1585 */
1586 // get a bit too much
Bram Moolenaar6f981142022-09-22 12:48:58 +01001587 newlen = (unsigned)STRLEN(arg) + 1;
Bram Moolenaar47403942022-09-21 21:12:53 +01001588 if (op != OP_NONE)
1589 newlen += (unsigned)STRLEN(origval) + 1;
1590 newval = alloc(newlen);
1591 if (newval == NULL) // out of mem, don't change
1592 return FAIL;
1593 s = newval;
1594
1595 /*
1596 * Copy the string, skip over escaped chars.
1597 * For MS-DOS and WIN32 backslashes before normal file name characters
1598 * are not removed, and keep backslash at start, for "\\machine\path",
1599 * but do remove it for "\\\\machine\\path".
1600 * The reverse is found in ExpandOldSetting().
1601 */
zeertzjqfcba86c2022-09-22 13:57:32 +01001602 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar47403942022-09-21 21:12:53 +01001603 {
1604 int i;
1605
Bram Moolenaar6f981142022-09-22 12:48:58 +01001606 if (*arg == '\\' && arg[1] != NUL
Bram Moolenaar47403942022-09-21 21:12:53 +01001607#ifdef BACKSLASH_IN_FILENAME
1608 && !((flags & P_EXPAND)
Bram Moolenaar6f981142022-09-22 12:48:58 +01001609 && vim_isfilec(arg[1])
1610 && !VIM_ISWHITE(arg[1])
1611 && (arg[1] != '\\'
zeertzjqfcba86c2022-09-22 13:57:32 +01001612 || (s == newval && arg[2] != '\\')))
Bram Moolenaar47403942022-09-21 21:12:53 +01001613#endif
1614 )
Bram Moolenaar6f981142022-09-22 12:48:58 +01001615 ++arg; // remove backslash
1616 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar47403942022-09-21 21:12:53 +01001617 {
1618 // copy multibyte char
Bram Moolenaar6f981142022-09-22 12:48:58 +01001619 mch_memmove(s, arg, (size_t)i);
1620 arg += i;
Bram Moolenaar47403942022-09-21 21:12:53 +01001621 s += i;
1622 }
1623 else
Bram Moolenaar6f981142022-09-22 12:48:58 +01001624 *s++ = *arg++;
Bram Moolenaar47403942022-09-21 21:12:53 +01001625 }
1626 *s = NUL;
1627
1628 /*
1629 * Expand environment variables and ~.
1630 * Don't do it when adding without inserting a comma.
1631 */
1632 if (op == OP_NONE || (flags & P_COMMA))
1633 {
1634 s = option_expand(opt_idx, newval);
1635 if (s != NULL)
1636 {
1637 vim_free(newval);
1638 newlen = (unsigned)STRLEN(s) + 1;
1639 if (op != OP_NONE)
1640 newlen += (unsigned)STRLEN(origval) + 1;
1641 newval = alloc(newlen);
1642 if (newval == NULL)
1643 return FAIL;
1644 STRCPY(newval, s);
1645 }
1646 }
1647
1648 // locate newval[] in origval[] when removing it
1649 // and when adding to avoid duplicates
1650 int len = 0;
1651 if (op == OP_REMOVING || (flags & P_NODUP))
1652 {
1653 len = (int)STRLEN(newval);
1654 s = find_dup_item(origval, newval, flags);
1655
1656 // do not add if already there
1657 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1658 {
1659 op = OP_NONE;
1660 STRCPY(newval, origval);
1661 }
1662
1663 // if no duplicate, move pointer to end of original value
1664 if (s == NULL)
1665 s = origval + (int)STRLEN(origval);
1666 }
1667
1668 // concatenate the two strings; add a ',' if needed
1669 if (op == OP_ADDING || op == OP_PREPENDING)
1670 {
1671 comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1672 if (op == OP_ADDING)
1673 {
1674 len = (int)STRLEN(origval);
1675 // strip a trailing comma, would get 2
1676 if (comma && len > 1
1677 && (flags & P_ONECOMMA) == P_ONECOMMA
1678 && origval[len - 1] == ','
1679 && origval[len - 2] != '\\')
1680 len--;
1681 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1682 mch_memmove(newval, origval, (size_t)len);
1683 }
1684 else
1685 {
1686 len = (int)STRLEN(newval);
1687 STRMOVE(newval + len + comma, origval);
1688 }
1689 if (comma)
1690 newval[len] = ',';
1691 }
1692
1693 // Remove newval[] from origval[]. (Note: "len" has been set above and
1694 // is used here).
1695 if (op == OP_REMOVING)
1696 {
1697 STRCPY(newval, origval);
1698 if (*s)
1699 {
1700 // may need to remove a comma
1701 if (flags & P_COMMA)
1702 {
1703 if (s == origval)
1704 {
1705 // include comma after string
1706 if (s[len] == ',')
1707 ++len;
1708 }
1709 else
1710 {
1711 // include comma before string
1712 --s;
1713 ++len;
1714 }
1715 }
1716 STRMOVE(newval + (s - origval), s + len);
1717 }
1718 }
1719
1720 if (flags & P_FLAGLIST)
1721 {
1722 // Remove flags that appear twice.
1723 for (s = newval; *s;)
1724 {
1725 // if options have P_FLAGLIST and P_ONECOMMA such as
1726 // 'whichwrap'
1727 if (flags & P_ONECOMMA)
1728 {
1729 if (*s != ',' && *(s + 1) == ','
1730 && vim_strchr(s + 2, *s) != NULL)
1731 {
1732 // Remove the duplicated value and the next comma.
1733 STRMOVE(s, s + 2);
1734 continue;
1735 }
1736 }
1737 else
1738 {
1739 if ((!(flags & P_COMMA) || *s != ',')
1740 && vim_strchr(s + 1, *s) != NULL)
1741 {
1742 STRMOVE(s, s + 1);
1743 continue;
1744 }
1745 }
1746 ++s;
1747 }
1748 }
1749
zeertzjqfcba86c2022-09-22 13:57:32 +01001750 if (save_arg != NULL)
1751 arg = save_arg; // arg was temporarily changed, restore it
Bram Moolenaar47403942022-09-21 21:12:53 +01001752 }
1753
1754 /*
1755 * Set the new value.
1756 */
1757 *(char_u **)(varp) = newval;
1758
1759#if defined(FEAT_EVAL)
1760 if (!starting
1761# ifdef FEAT_CRYPT
1762 && options[opt_idx].indir != PV_KEY
1763# endif
1764 && origval != NULL && newval != NULL)
1765 {
1766 // origval may be freed by did_set_string_option(), make a copy.
1767 saved_origval = vim_strsave(origval);
1768 // newval (and varp) may become invalid if the buffer is closed by
1769 // autocommands.
1770 saved_newval = vim_strsave(newval);
1771 if (origval_l != NULL)
1772 saved_origval_l = vim_strsave(origval_l);
1773 if (origval_g != NULL)
1774 saved_origval_g = vim_strsave(origval_g);
1775 }
1776#endif
1777
1778 {
1779 long_u *p = insecure_flag(opt_idx, opt_flags);
1780 int secure_saved = secure;
1781
1782 // When an option is set in the sandbox, from a modeline or in secure
1783 // mode, then deal with side effects in secure mode. Also when the
1784 // value was set with the P_INSECURE flag and is not completely
1785 // replaced.
1786 if ((opt_flags & OPT_MODELINE)
1787#ifdef HAVE_SANDBOX
1788 || sandbox != 0
1789#endif
1790 || (op != OP_NONE && (*p & P_INSECURE)))
1791 secure = 1;
1792
1793 // Handle side effects, and set the global value for ":set" on local
1794 // options. Note: when setting 'syntax' or 'filetype' autocommands may
1795 // be triggered that can cause havoc.
1796 *errmsg = did_set_string_option(
1797 opt_idx, (char_u **)varp, oldval, errbuf,
1798 opt_flags, value_checked);
1799
1800 secure = secure_saved;
1801 }
1802
1803#if defined(FEAT_EVAL)
1804 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00001805 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01001806 saved_origval_l, saved_origval_g, saved_newval);
1807 vim_free(saved_origval);
1808 vim_free(saved_origval_l);
1809 vim_free(saved_origval_g);
1810 vim_free(saved_newval);
1811#endif
1812
Bram Moolenaar6f981142022-09-22 12:48:58 +01001813 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001814 return *errmsg == NULL ? OK : FAIL;
1815}
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817/*
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001818 * Set a boolean option
1819 */
1820 static char *
1821do_set_option_bool(
1822 int opt_idx,
1823 int opt_flags,
1824 set_prefix_T prefix,
1825 long_u flags,
1826 char_u *varp,
1827 int nextchar,
1828 int afterchar,
1829 int cp_val)
1830
1831{
1832 varnumber_T value;
1833
1834 if (nextchar == '=' || nextchar == ':')
1835 return e_invalid_argument;
1836
1837 /*
1838 * ":set opt!": invert
1839 * ":set opt&": reset to default value
1840 * ":set opt<": reset to global value
1841 */
1842 if (nextchar == '!')
1843 value = *(int *)(varp) ^ 1;
1844 else if (nextchar == '&')
1845 value = (int)(long)(long_i)options[opt_idx].def_val[
1846 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
1847 else if (nextchar == '<')
1848 {
1849 // For 'autoread' -1 means to use global value.
1850 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
1851 value = -1;
1852 else
1853 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
1854 }
1855 else
1856 {
1857 /*
1858 * ":set invopt": invert
1859 * ":set opt" or ":set noopt": set or reset
1860 */
1861 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
1862 return e_trailing_characters;
1863 if (prefix == PREFIX_INV)
1864 value = *(int *)(varp) ^ 1;
1865 else
1866 value = prefix == PREFIX_NO ? 0 : 1;
1867 }
1868
1869 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
1870}
1871
1872/*
1873 * Set a numeric option
1874 */
1875 static char *
1876do_set_option_numeric(
1877 int opt_idx,
1878 int opt_flags,
1879 char_u **argp,
1880 int nextchar,
1881 set_op_T op,
1882 long_u flags,
1883 int cp_val,
1884 char_u *varp,
1885 char *errbuf,
1886 size_t errbuflen)
1887{
1888 char_u *arg = *argp;
1889 varnumber_T value;
1890 int i;
1891 char *errmsg = NULL;
1892
1893 /*
1894 * Different ways to set a number option:
1895 * & set to default value
1896 * < set to global value
1897 * <xx> accept special key codes for 'wildchar'
1898 * c accept any non-digit for 'wildchar'
1899 * [-]0-9 set number
1900 * other error
1901 */
1902 ++arg;
1903 if (nextchar == '&')
1904 value = (long)(long_i)options[opt_idx].def_val[
1905 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
1906 else if (nextchar == '<')
1907 {
1908 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1909 // use the global value.
1910 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
1911 value = NO_LOCAL_UNDOLEVEL;
1912 else
1913 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
1914 }
1915 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
1916 && (*arg == '<'
1917 || *arg == '^'
1918 || (*arg != NUL
1919 && (!arg[1] || VIM_ISWHITE(arg[1]))
1920 && !VIM_ISDIGIT(*arg))))
1921 {
1922 value = string_to_key(arg, FALSE);
1923 if (value == 0 && (long *)varp != &p_wcm)
1924 {
1925 errmsg = e_invalid_argument;
1926 goto skip;
1927 }
1928 }
1929 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1930 {
1931 // Allow negative (for 'undolevels'), octal and hex numbers.
1932 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE);
1933 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
1934 {
1935 errmsg = e_number_required_after_equal;
1936 goto skip;
1937 }
1938 }
1939 else
1940 {
1941 errmsg = e_number_required_after_equal;
1942 goto skip;
1943 }
1944
1945 if (op == OP_ADDING)
1946 value = *(long *)varp + value;
1947 else if (op == OP_PREPENDING)
1948 value = *(long *)varp * value;
1949 else if (op == OP_REMOVING)
1950 value = *(long *)varp - value;
1951
1952 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
1953 opt_flags);
1954
1955skip:
1956 *argp = arg;
1957 return errmsg;
1958}
1959
1960/*
1961 * Set a key code (t_xx) option
1962 */
1963 static char *
1964do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
1965{
1966 char_u *arg = *argp;
1967 char_u *p;
1968
1969 if (nextchar == '&')
1970 {
1971 if (add_termcap_entry(key_name, TRUE) == FAIL)
1972 return e_not_found_in_termcap;
1973 }
1974 else
1975 {
1976 ++arg; // jump to after the '=' or ':'
1977 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
1978 if (*p == '\\' && p[1] != NUL)
1979 ++p;
1980 nextchar = *p;
1981 *p = NUL;
1982 add_termcode(key_name, arg, FALSE);
1983 *p = nextchar;
1984 }
1985 if (full_screen)
1986 ttest(FALSE);
1987 redraw_all_later(UPD_CLEAR);
1988
1989 *argp = arg;
1990 return NULL;
1991}
1992
1993/*
1994 * Set an option to a new value.
1995 */
1996 static char *
1997do_set_option_value(
1998 int opt_idx,
1999 int opt_flags,
2000 char_u **argp,
2001 set_prefix_T prefix,
2002 set_op_T op,
2003 long_u flags,
2004 char_u *varp,
2005 char_u *key_name,
2006 int nextchar,
2007 int afterchar,
2008 int cp_val,
2009 int *stopopteval,
2010 char *errbuf,
2011 size_t errbuflen)
2012{
2013 int value_checked = FALSE;
2014 char *errmsg = NULL;
2015 char_u *arg = *argp;
2016
2017 if (flags & P_BOOL)
2018 {
2019 // boolean option
2020 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2021 nextchar, afterchar, cp_val);
2022 if (errmsg != NULL)
2023 goto skip;
2024 }
2025 else
2026 {
2027 // numeric or string option
2028 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2029 || prefix != PREFIX_NONE)
2030 {
2031 errmsg = e_invalid_argument;
2032 goto skip;
2033 }
2034
2035 if (flags & P_NUM)
2036 {
2037 // numeric option
2038 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2039 op, flags, cp_val, varp,
2040 errbuf, errbuflen);
2041 if (errmsg != NULL)
2042 goto skip;
2043 }
2044 else if (opt_idx >= 0)
2045 {
2046 // string option
2047 if (do_set_string(opt_idx, opt_flags, &arg, nextchar, op, flags,
2048 cp_val, varp, errbuf, &value_checked,
2049 &errmsg) == FAIL)
2050 {
2051 if (errmsg != NULL)
2052 goto skip;
2053 *stopopteval = TRUE;
2054 goto skip;
2055 }
2056 }
2057 else
2058 {
2059 // key code option
2060 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2061 if (errmsg != NULL)
2062 goto skip;
2063 }
2064 }
2065
2066 if (opt_idx >= 0)
2067 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2068
2069skip:
2070 *argp = arg;
2071 return errmsg;
2072}
2073
2074/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002075 * Set an option to a new value.
2076 * Return NULL if OK, return an untranslated error message when something is
2077 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2078 */
2079 static char *
2080do_set_option(
2081 int opt_flags,
2082 char_u **argp,
2083 char_u *arg_start,
2084 char_u **startarg,
2085 int *did_show,
2086 int *stopopteval,
2087 char *errbuf,
2088 size_t errbuflen)
2089{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002090 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002091 char_u *arg;
2092 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2093 set_op_T op;
2094 long_u flags; // flags for current option
2095 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002096 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002097 int nextchar; // next non-white char after option name
2098 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002099 char *errmsg = NULL;
2100 int key;
2101 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002102
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002103 prefix = get_option_prefix(argp);
2104 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002105
2106 // find end of name
2107 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002108 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2109 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002110
2111 // remember character after option name
2112 afterchar = arg[len];
2113
2114 if (in_vim9script())
2115 {
2116 char_u *p = skipwhite(arg + len);
2117
2118 // disallow white space before =val, +=val, -=val, ^=val
2119 if (p > arg + len && (p[0] == '='
2120 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2121 && p[1] == '=')))
2122 {
2123 errmsg = e_no_white_space_allowed_between_option_and;
2124 arg = p;
2125 *startarg = p;
2126 goto skip;
2127 }
2128 }
2129 else
2130 // skip white space, allow ":set ai ?", ":set hlsearch !"
2131 while (VIM_ISWHITE(arg[len]))
2132 ++len;
2133
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002134 op = get_opt_op(arg + len);
2135 if (op != OP_NONE)
2136 len++;
2137
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002138 nextchar = arg[len];
2139
2140 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2141 {
2142 if (in_vim9script() && arg > arg_start
2143 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2144 errmsg = e_no_white_space_allowed_between_option_and;
2145 else
2146 errmsg = e_unknown_option;
2147 goto skip;
2148 }
2149
2150 if (opt_idx >= 0)
2151 {
2152 if (options[opt_idx].var == NULL) // hidden option: skip
2153 {
2154 // Only give an error message when requesting the value of
2155 // a hidden option, ignore setting it.
2156 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2157 && (!(options[opt_idx].flags & P_BOOL)
2158 || nextchar == '?'))
2159 errmsg = e_option_not_supported;
2160 goto skip;
2161 }
2162
2163 flags = options[opt_idx].flags;
2164 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2165 }
2166 else
2167 {
2168 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002169 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002170 if (key < 0)
2171 {
2172 key_name[0] = KEY2TERMCAP0(key);
2173 key_name[1] = KEY2TERMCAP1(key);
2174 }
2175 else
2176 {
2177 key_name[0] = KS_KEY;
2178 key_name[1] = (key & 0xff);
2179 }
2180 }
2181
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002182 // Make sure the option value can be changed.
2183 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002184 goto skip;
2185
Bram Moolenaar40b48722023-02-05 17:04:50 +00002186 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002187 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2188 {
2189 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002190 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2191 {
2192 if (arg[3] == 'm') // "opt&vim": set to Vim default
2193 {
2194 cp_val = FALSE;
2195 arg += 3;
2196 }
2197 else // "opt&vi": set to Vi default
2198 {
2199 cp_val = TRUE;
2200 arg += 2;
2201 }
2202 }
2203 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2204 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2205 {
2206 errmsg = e_trailing_characters;
2207 goto skip;
2208 }
2209 }
2210
2211 /*
2212 * allow '=' and ':' for historical reasons (MSDOS command.com
2213 * allows only one '=' character per "set" command line. grrr. (jw)
2214 */
2215 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002216 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002217 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2218 && !(flags & P_BOOL)))
2219 {
2220 /*
2221 * print value
2222 */
2223 if (*did_show)
2224 msg_putchar('\n'); // cursor below last one
2225 else
2226 {
2227 gotocmdline(TRUE); // cursor at status line
2228 *did_show = TRUE; // remember that we did a line
2229 }
2230 if (opt_idx >= 0)
2231 {
2232 showoneopt(&options[opt_idx], opt_flags);
2233#ifdef FEAT_EVAL
2234 if (p_verbose > 0)
2235 {
2236 // Mention where the option was last set.
2237 if (varp == options[opt_idx].var)
2238 last_set_msg(options[opt_idx].script_ctx);
2239 else if ((int)options[opt_idx].indir & PV_WIN)
2240 last_set_msg(curwin->w_p_script_ctx[
2241 (int)options[opt_idx].indir & PV_MASK]);
2242 else if ((int)options[opt_idx].indir & PV_BUF)
2243 last_set_msg(curbuf->b_p_script_ctx[
2244 (int)options[opt_idx].indir & PV_MASK]);
2245 }
2246#endif
2247 }
2248 else
2249 {
2250 char_u *p;
2251
2252 p = find_termcode(key_name);
2253 if (p == NULL)
2254 {
2255 errmsg = e_key_code_not_set;
2256 goto skip;
2257 }
2258 else
2259 (void)show_one_termcode(key_name, p, TRUE);
2260 }
2261 if (nextchar != '?'
2262 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2263 errmsg = e_trailing_characters;
2264 }
2265 else
2266 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002267 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2268 flags, varp, key_name, nextchar,
2269 afterchar, cp_val, stopopteval, errbuf,
2270 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002271 }
2272
2273skip:
2274 *argp = arg;
2275 return errmsg;
2276}
2277
2278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 * Parse 'arg' for option settings.
2280 *
2281 * 'arg' may be IObuff, but only when no errors can be present and option
2282 * does not need to be expanded with option_expand().
2283 * "opt_flags":
2284 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002285 * OPT_GLOBAL for ":setglobal"
2286 * OPT_LOCAL for ":setlocal" and a modeline
2287 * OPT_MODELINE for a modeline
2288 * OPT_WINONLY to only set window-local options
2289 * OPT_NOWIN to skip setting window-local options
2290 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002292 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 */
2294 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002295do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002296 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002297 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002299 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002301 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302
2303 if (*arg == NUL)
2304 {
2305 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002306 did_show = TRUE;
2307 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
2309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002310 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002312 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002313 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 /*
2316 * ":set all" show all options.
2317 * ":set all&" set all options to their default value.
2318 */
2319 arg += 3;
2320 if (*arg == '&')
2321 {
2322 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002323 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002325 didset_options();
2326 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002327 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002332 did_show = TRUE;
2333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002335 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
2337 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002338 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002339 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 arg += 7;
2341 }
2342 else
2343 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002344 int stopopteval = FALSE;
2345 char *errmsg = NULL;
2346 char errbuf[80];
2347 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002349 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2350 &did_show, &stopopteval, errbuf,
2351 sizeof(errbuf));
2352 if (stopopteval)
2353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 /*
2356 * Advance to next argument.
2357 * - skip until a blank found, taking care of backslashes
2358 * - skip blanks
2359 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2360 */
2361 for (i = 0; i < 2 ; ++i)
2362 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002363 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (*arg++ == '\\' && *arg != NUL)
2365 ++arg;
2366 arg = skipwhite(arg);
2367 if (*arg != '=')
2368 break;
2369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002371 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002373 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2374 i = (int)STRLEN(IObuff) + 2;
2375 if (i + (arg - startarg) < IOSIZE)
2376 {
2377 // append the argument with the error
2378 STRCAT(IObuff, ": ");
2379 mch_memmove(IObuff + i, startarg, (arg - startarg));
2380 IObuff[i + (arg - startarg)] = NUL;
2381 }
2382 // make sure all characters are printable
2383 trans_characters(IObuff, IOSIZE);
2384
2385 ++no_wait_return; // wait_return() done later
2386 emsg((char *)IObuff); // show error highlighted
2387 --no_wait_return;
2388
2389 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392
2393 arg = skipwhite(arg);
2394 }
2395
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002396theend:
2397 if (silent_mode && did_show)
2398 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002399 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002400 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002401 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002402 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002403 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002404 out_flush();
2405 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002406 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002407 }
2408
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 return OK;
2410}
2411
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002412/*
2413 * Call this when an option has been given a new value through a user command.
2414 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2415 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002416 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002417did_set_option(
2418 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002419 int opt_flags, // possibly with OPT_MODELINE
2420 int new_value, // value was replaced completely
2421 int value_checked) // value was checked to be safe, no need to set the
2422 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002423{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002424 long_u *p;
2425
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002426 options[opt_idx].flags |= P_WAS_SET;
2427
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002428 // When an option is set in the sandbox, from a modeline or in secure mode
2429 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2430 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002431 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002432 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002433#ifdef HAVE_SANDBOX
2434 || sandbox != 0
2435#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002436 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002437 *p = *p | P_INSECURE;
2438 else if (new_value)
2439 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002440}
2441
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442/*
2443 * Convert a key name or string into a key value.
2444 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002445 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002447 int
2448string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449{
2450 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002451 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 if (*arg == '^')
2453 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002454 if (multi_byte)
2455 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 return *arg;
2457}
2458
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459/*
2460 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2461 * maketitle() to create and display it.
2462 * When switching the title or icon off, call mch_restore_title() to get
2463 * the old value back.
2464 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002465 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002466did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468 if (starting != NO_SCREEN
2469#ifdef FEAT_GUI
2470 && !gui.starting
2471#endif
2472 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476/*
2477 * set_options_bin - called when 'bin' changes value.
2478 */
2479 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002480set_options_bin(
2481 int oldval,
2482 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002483 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484{
2485 /*
2486 * The option values that are changed when 'bin' changes are
2487 * copied when 'bin is set and restored when 'bin' is reset.
2488 */
2489 if (newval)
2490 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002491 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 {
2493 if (!(opt_flags & OPT_GLOBAL))
2494 {
2495 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2496 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2497 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2498 curbuf->b_p_et_nobin = curbuf->b_p_et;
2499 }
2500 if (!(opt_flags & OPT_LOCAL))
2501 {
2502 p_tw_nobin = p_tw;
2503 p_wm_nobin = p_wm;
2504 p_ml_nobin = p_ml;
2505 p_et_nobin = p_et;
2506 }
2507 }
2508
2509 if (!(opt_flags & OPT_GLOBAL))
2510 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002511 curbuf->b_p_tw = 0; // no automatic line wrap
2512 curbuf->b_p_wm = 0; // no automatic line wrap
2513 curbuf->b_p_ml = 0; // no modelines
2514 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 }
2516 if (!(opt_flags & OPT_LOCAL))
2517 {
2518 p_tw = 0;
2519 p_wm = 0;
2520 p_ml = FALSE;
2521 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002522 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 }
2524 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002525 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 if (!(opt_flags & OPT_GLOBAL))
2528 {
2529 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2530 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2531 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2532 curbuf->b_p_et = curbuf->b_p_et_nobin;
2533 }
2534 if (!(opt_flags & OPT_LOCAL))
2535 {
2536 p_tw = p_tw_nobin;
2537 p_wm = p_wm_nobin;
2538 p_ml = p_ml_nobin;
2539 p_et = p_et_nobin;
2540 }
2541 }
2542}
2543
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544/*
2545 * Expand environment variables for some string options.
2546 * These string options cannot be indirect!
2547 * If "val" is NULL expand the current value of the option.
2548 * Return pointer to NameBuff, or NULL when not expanded.
2549 */
2550 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002551option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002553 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2555 return NULL;
2556
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002557 // If val is longer than MAXPATHL no meaningful expansion can be done,
2558 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (val != NULL && STRLEN(val) > MAXPATHL)
2560 return NULL;
2561
2562 if (val == NULL)
2563 val = *(char_u **)options[opt_idx].var;
2564
2565 /*
2566 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2567 * Escape spaces when expanding 'tags', they are used to separate file
2568 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002569 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 */
2571 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002572 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002573#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002574 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2575#endif
2576 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002577 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 return NULL;
2579
2580 return NameBuff;
2581}
2582
2583/*
2584 * After setting various option values: recompute variables that depend on
2585 * option values.
2586 */
2587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002588didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002590 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 (void)init_chartab();
2592
Bram Moolenaardac13472019-09-16 21:06:21 +02002593 didset_string_options();
2594
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002595#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002596 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002597 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002598 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002599 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002600#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002601 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 (void)check_cedit();
Bram Moolenaar597a4222014-06-25 14:39:50 +02002603#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002604 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002605 fill_breakat_flags();
2606#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002607 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002608}
2609
2610/*
2611 * More side effects of setting options.
2612 */
2613 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002614didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002615{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002616 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002617 (void)highlight_changed();
2618
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002619 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002620 check_opt_wim();
2621
Bram Moolenaareed9d462021-02-15 20:38:25 +01002622 // Parse default for 'listchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002623 (void)set_chars_option(curwin, &curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002625 // Parse default for 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002626 (void)set_chars_option(curwin, &curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002627
2628#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002629 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002630 (void)check_clipboard_option();
2631#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002632#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002633 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002634 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002635 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002636 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638}
2639
2640/*
2641 * Check for string options that are NULL (normally only termcap options).
2642 */
2643 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002644check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645{
2646 int opt_idx;
2647
2648 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2649 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2650 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2651}
2652
2653/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002654 * Return the option index found by a pointer into term_strings[].
2655 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002657 int
2658get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002660 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
2662 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2663 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002664 return opt_idx;
2665 return -1; // cannot happen: didn't find it!
2666}
2667
2668/*
2669 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2670 * Return the option index or -1 if not found.
2671 */
2672 int
2673set_term_option_alloced(char_u **p)
2674{
2675 int opt_idx = get_term_opt_idx(p);
2676
2677 if (opt_idx >= 0)
2678 options[opt_idx].flags |= P_ALLOCED;
2679 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680}
2681
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002682#if defined(FEAT_EVAL) || defined(PROTO)
2683/*
2684 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2685 * Return FALSE when it wasn't.
2686 * Return -1 for an unknown option.
2687 */
2688 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002689was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002690{
2691 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002692 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002693
2694 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002695 {
2696 flagp = insecure_flag(idx, opt_flags);
2697 return (*flagp & P_INSECURE) != 0;
2698 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002699 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002700 return -1;
2701}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002702
2703/*
2704 * Get a pointer to the flags used for the P_INSECURE flag of option
2705 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002706 * NOTE: Caller must make sure that "curwin" is set to the window from which
2707 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002708 */
2709 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002710insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002711{
2712 if (opt_flags & OPT_LOCAL)
2713 switch ((int)options[opt_idx].indir)
2714 {
2715#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002716 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002717#endif
2718#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002719# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002720 case PV_FDE: return &curwin->w_p_fde_flags;
2721 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002722# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002723# ifdef FEAT_BEVAL
2724 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2725# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002726 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002727 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002728# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002729 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002730# endif
2731#endif
2732 }
2733
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002734 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002735 return &options[opt_idx].flags;
2736}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002737#endif
2738
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002739/*
2740 * Redraw the window title and/or tab page text later.
2741 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002742void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002743{
2744 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002745 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002746}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002747
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002749 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2750 * characters or characters in "allowed".
2751 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002752 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002753valid_name(char_u *val, char *allowed)
2754{
2755 char_u *s;
2756
2757 for (s = val; *s != NUL; ++s)
2758 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2759 return FALSE;
2760 return TRUE;
2761}
2762
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002763#if defined(FEAT_EVAL) || defined(PROTO)
2764/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002765 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002766 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002767 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002768 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002769set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002770{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002771 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2772 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002773 sctx_T new_script_ctx = script_ctx;
2774
Bram Moolenaar51258742020-05-03 17:19:33 +02002775 // Modeline already has the line number set.
2776 if (!(opt_flags & OPT_MODELINE))
2777 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002778
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002779 // Remember where the option was set. For local options need to do that
2780 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002781 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002782 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002783 if (both || (opt_flags & OPT_LOCAL))
2784 {
2785 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002786 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002787 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002788 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002789 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002790 if (both)
2791 // also setting the "all buffers" value
2792 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2793 new_script_ctx;
2794 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002795 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002796}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002797
2798/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002799 * Get the script context of global option "name".
2800 *
2801 */
2802 sctx_T *
2803get_option_sctx(char *name)
2804{
2805 int idx = findoption((char_u *)name);
2806
2807 if (idx >= 0)
2808 return &options[idx].script_ctx;
2809 siemsg("no such option: %s", name);
2810 return NULL;
2811}
2812
2813/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002814 * Set the script_ctx for a termcap option.
2815 * "name" must be the two character code, e.g. "RV".
2816 * When "name" is NULL use "opt_idx".
2817 */
2818 void
2819set_term_option_sctx_idx(char *name, int opt_idx)
2820{
2821 char_u buf[5];
2822 int idx;
2823
2824 if (name == NULL)
2825 idx = opt_idx;
2826 else
2827 {
2828 buf[0] = 't';
2829 buf[1] = '_';
2830 buf[2] = name[0];
2831 buf[3] = name[1];
2832 buf[4] = 0;
2833 idx = findoption(buf);
2834 }
2835 if (idx >= 0)
2836 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2837}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002838#endif
2839
Bram Moolenaarf4140482020-02-15 23:06:45 +01002840#if defined(FEAT_EVAL)
2841/*
2842 * Apply the OptionSet autocommand.
2843 */
2844 static void
2845apply_optionset_autocmd(
2846 int opt_idx,
2847 long opt_flags,
2848 long oldval,
2849 long oldval_g,
2850 long newval,
2851 char *errmsg)
2852{
2853 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2854
2855 // Don't do this while starting up, failure or recursively.
2856 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2857 return;
2858
2859 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2860 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2861 oldval_g);
2862 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2863 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2864 (opt_flags & OPT_LOCAL) ? "local" : "global");
2865 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2866 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2867 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2868 if (opt_flags & OPT_LOCAL)
2869 {
2870 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2871 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2872 }
2873 if (opt_flags & OPT_GLOBAL)
2874 {
2875 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2876 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2877 }
2878 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2879 {
2880 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2881 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2882 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2883 }
2884 if (opt_flags & OPT_MODELINE)
2885 {
2886 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2887 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2888 }
2889 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2890 NULL, FALSE, NULL);
2891 reset_v_option_vars();
2892}
2893#endif
2894
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895/*
2896 * Set the value of a boolean option, and take care of side effects.
2897 * Returns NULL for success, or an error message for an error.
2898 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002899 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002900set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002901 int opt_idx, // index in options[] table
2902 char_u *varp, // pointer to the option variable
2903 int value, // new value
2904 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002907#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002908 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002909#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002910 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 if ((secure
2914#ifdef HAVE_SANDBOX
2915 || sandbox != 0
2916#endif
2917 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002918 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002920#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002921 // Save the global value before changing anything. This is needed as for
2922 // a global-only option setting the "local value" in fact sets the global
2923 // value (since there is only one value).
2924 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2925 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2926 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002927#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002929 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002931 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002932 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933#endif
2934
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002935#ifdef FEAT_GUI
2936 need_mouse_correct = TRUE;
2937#endif
2938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2941 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2942
2943 /*
2944 * Handle side effects of changing a bool option.
2945 */
2946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002947 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
Bram Moolenaar920694c2016-08-21 17:45:02 +02002951#ifdef FEAT_LANGMAP
2952 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002953 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002954 p_lnr = !p_lrm;
2955 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002957 p_lrm = !p_lnr;
2958#endif
2959
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002960#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002961 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002962 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2963 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002964 // Only take action when the option was set. When reset we do not
2965 // delete the undo file, the option may be set again without making
2966 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002967 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002968 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002969 char_u hash[UNDO_HASH_SIZE];
2970 buf_T *save_curbuf = curbuf;
2971
Bram Moolenaar29323592016-07-24 22:04:11 +02002972 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002973 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002974 // When 'undofile' is set globally: for every buffer, otherwise
2975 // only for the current buffer: Try to read in the undofile,
2976 // if one exists, the buffer wasn't changed and the buffer was
2977 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002978 if ((curbuf == save_curbuf
2979 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2980 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2981 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002982#ifdef FEAT_CRYPT
2983 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2984 continue;
2985#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002986 u_compute_hash(hash);
2987 u_read_undo(NULL, hash, curbuf->b_fname);
2988 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002989 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002990 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002991 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002992 }
2993#endif
2994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 else if ((int *)varp == &curbuf->b_p_ro)
2996 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002997 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2999 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003001 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003002 if (curbuf->b_p_ro)
3003 curbuf->b_did_warn = FALSE;
3004
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003005 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
3007
Bram Moolenaar9c449722010-07-20 18:44:27 +02003008#ifdef FEAT_GUI
3009 else if ((int *)varp == &p_mh)
3010 {
3011 if (!p_mh)
3012 gui_mch_mousehide(FALSE);
3013 }
3014#endif
3015
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003016 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003018 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02003019# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003020 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01003021 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3022 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02003023 {
3024 curbuf->b_p_ma = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003025 return e_cannot_make_terminal_with_running_job_modifiable;
Bram Moolenaar423802d2017-07-30 16:52:24 +02003026 }
3027# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003028 redraw_titles();
3029 }
K.Takata845bbb72022-11-05 18:31:19 +00003030 // redraw the window title and tab page text when 'endoffile', 'endofline',
3031 // 'fixeol' or 'bomb' is changed
3032 else if ((int *)varp == &curbuf->b_p_eof
3033 || (int *)varp == &curbuf->b_p_eol
3034 || (int *)varp == &curbuf->b_p_fixeol
3035 || (int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003036 {
3037 redraw_titles();
3038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003040 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 else if ((int *)varp == &curbuf->b_p_bin)
3042 {
3043 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003044 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 }
3046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003047 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
3049 {
3050 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3051 NULL, NULL, TRUE, curbuf);
3052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003054 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 else if ((int *)varp == &curbuf->b_p_swf)
3056 {
3057 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003058 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003060 // no need to reset curbuf->b_may_swap, ml_open_file() will check
3061 // buf->b_p_swf
3062 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
3064
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003065 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 else if ((int *)varp == &p_terse)
3067 {
3068 char_u *p;
3069
3070 p = vim_strchr(p_shm, SHM_SEARCH);
3071
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003072 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 if (p_terse && p == NULL)
3074 {
3075 STRCPY(IObuff, p_shm);
3076 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003077 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003079 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003081 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 }
3083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 else if ((int *)varp == &p_paste)
3086 {
3087 paste_option_changed();
3088 }
3089
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003090 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 else if ((int *)varp == &p_im)
3092 {
3093 if (p_im)
3094 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003095 if ((State & MODE_INSERT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 need_start_insertmode = TRUE;
3097 stop_insert_mode = FALSE;
3098 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003099 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02003100 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 {
3102 need_start_insertmode = FALSE;
3103 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003104 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003105 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 restart_edit = 0;
3107 }
3108 }
3109
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003110 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 else if ((int *)varp == &p_ic && p_hls)
3112 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003113 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 }
3115
3116#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003117 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 else if ((int *)varp == &p_hls)
3119 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02003120 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 }
3122#endif
3123
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003124 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3125 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 else if ((int *)varp == &curwin->w_p_scb)
3127 {
3128 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02003129 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02003131 curwin->w_scbind_pos = curwin->w_topline;
3132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
Bram Moolenaar4033c552017-09-16 20:54:51 +02003135#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003136 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 else if ((int *)varp == &curwin->w_p_pvw)
3138 {
3139 if (curwin->w_p_pvw)
3140 {
3141 win_T *win;
3142
Bram Moolenaar29323592016-07-24 22:04:11 +02003143 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 if (win->w_p_pvw && win != curwin)
3145 {
3146 curwin->w_p_pvw = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003147 return e_preview_window_already_exists;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 }
3149 }
3150 }
3151#endif
3152
Bram Moolenaarf6196f42022-10-02 21:29:55 +01003153 else if ((int *)varp == &curwin->w_p_sms)
3154 {
3155 if (!curwin->w_p_sms)
3156 {
3157 curwin->w_skipcol = 0;
3158 changed_line_abv_curs();
3159 }
3160 }
3161
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003162 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 else if ((int *)varp == &curbuf->b_p_tx)
3164 {
3165 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
3166 }
3167
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003168 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 set_string_option_direct((char_u *)"ffs", -1,
3172 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003173 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 /*
3177 * When 'lisp' option changes include/exclude '-' in
3178 * keyword characters.
3179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 else if (varp == (char_u *)&(curbuf->b_p_lisp))
3181 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003182 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003185 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02003186 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003188 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
3191 else if ((int *)varp == &curbuf->b_changed)
3192 {
3193 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003194 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003195 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 }
3198
3199#ifdef BACKSLASH_IN_FILENAME
3200 else if ((int *)varp == &p_ssl)
3201 {
3202 if (p_ssl)
3203 {
3204 psepc = '/';
3205 psepcN = '\\';
3206 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
3208 else
3209 {
3210 psepc = '\\';
3211 psepcN = '/';
3212 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 }
3214
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003215 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 buflist_slash_adjust();
3217 alist_slash_adjust();
3218# ifdef FEAT_EVAL
3219 scriptnames_slash_adjust();
3220# endif
3221 }
3222#endif
3223
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003224 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 else if ((int *)varp == &curwin->w_p_wrap)
3226 {
3227 if (curwin->w_p_wrap)
3228 curwin->w_leftcol = 0;
3229 }
3230
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 else if ((int *)varp == &p_ea)
3232 {
3233 if (p_ea && !old_value)
3234 win_equal(curwin, FALSE, 0);
3235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
3237 else if ((int *)varp == &p_wiv)
3238 {
3239 /*
3240 * When 'weirdinvert' changed, set/reset 't_xs'.
3241 * Then set 'weirdinvert' according to value of 't_xs'.
3242 */
3243 if (p_wiv && !old_value)
3244 T_XS = (char_u *)"y";
3245 else if (!p_wiv && old_value)
3246 T_XS = empty_option;
3247 p_wiv = (*T_XS != NUL);
3248 }
3249
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003250#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 else if ((int *)varp == &p_beval)
3252 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003253 if (!balloonEvalForTerm)
3254 {
3255 if (p_beval && !old_value)
3256 gui_mch_enable_beval_area(balloonEval);
3257 else if (!p_beval && old_value)
3258 gui_mch_disable_beval_area(balloonEval);
3259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003261#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003262#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003263 else if ((int *)varp == &p_bevalterm)
3264 {
3265 mch_bevalterm_changed();
3266 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003269#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 else if ((int *)varp == &p_acd)
3271 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003272 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003273 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 }
3275#endif
3276
3277#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003278 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 else if ((int *)varp == &curwin->w_p_diff)
3280 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003281 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003282 diff_buf_adjust(curwin);
3283# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 if (foldmethodIsDiff(curwin))
3285 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
3288#endif
3289
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003290#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003291 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 else if ((int *)varp == &p_imdisable)
3293 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003294 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (p_imdisable)
3296 im_set_active(FALSE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003297 else if (State & MODE_INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003298 // When the option is set from an autocommand, it may need to take
3299 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003300 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
3302#endif
3303
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003304#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003305 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003306 else if ((int *)varp == &curwin->w_p_spell)
3307 {
3308 if (curwin->w_p_spell)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003309 errmsg = did_set_spelllang(curwin);
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003310 }
3311#endif
3312
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313#ifdef FEAT_ARABIC
3314 if ((int *)varp == &curwin->w_p_arab)
3315 {
3316 if (curwin->w_p_arab)
3317 {
3318 /*
3319 * 'arabic' is set, handle various sub-settings.
3320 */
3321 if (!p_tbidi)
3322 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003323 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (!curwin->w_p_rl)
3325 {
3326 curwin->w_p_rl = TRUE;
3327 changed_window_setting();
3328 }
3329
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003330 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (!p_arshape)
3332 {
3333 p_arshape = TRUE;
3334 redraw_later_clear();
3335 }
3336 }
3337
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003338 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003339 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003341 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003342 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3343
Bram Moolenaar8820b482017-03-16 17:23:31 +01003344 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003345 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003346#ifdef FEAT_EVAL
3347 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3348#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003351 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
3354# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003355 // Force-set the necessary keymap for arabic
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003356 errmsg = set_option_value((char_u *)"keymap",
3357 0L, (char_u *)"arabic", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
3360 else
3361 {
3362 /*
3363 * 'arabic' is reset, handle various sub-settings.
3364 */
3365 if (!p_tbidi)
3366 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003367 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 if (curwin->w_p_rl)
3369 {
3370 curwin->w_p_rl = FALSE;
3371 changed_window_setting();
3372 }
3373
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003374 // 'arabicshape' isn't reset, it is a global option and
3375 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003378 // 'delcombine' isn't reset, it is a global option and another
3379 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
3381# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003382 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 curbuf->b_p_iminsert = B_IMODE_NONE;
3384 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3385# endif
3386 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003387 }
3388
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003389#endif
3390
Bram Moolenaar44826212019-08-22 21:23:20 +02003391#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3392 else if (((int *)varp == &curwin->w_p_nu
3393 || (int *)varp == &curwin->w_p_rnu)
3394 && gui.in_use
3395 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3396 && curbuf->b_signlist != NULL)
3397 {
3398 // If the 'number' or 'relativenumber' options are modified and
3399 // 'signcolumn' is set to 'number', then clear the screen for a full
3400 // refresh. Otherwise the sign icons are not displayed properly in the
3401 // number column. If the 'number' option is set and only the
3402 // 'relativenumber' option is toggled, then don't refresh the screen
3403 // (optimization).
3404 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003405 redraw_all_later(UPD_CLEAR);
Bram Moolenaar44826212019-08-22 21:23:20 +02003406 }
3407#endif
3408
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003409#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003410 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003411 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003412 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003413# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003414 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003415 if (
3416# ifdef VIMDLL
3417 !gui.in_use && !gui.starting &&
3418# endif
3419 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003420 {
3421 p_tgc = 0;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003422 return e_24_bit_colors_are_not_supported_on_this_environment;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003423 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003424 if (is_term_win32())
3425 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003426# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003427# ifdef FEAT_GUI
3428 if (!gui.in_use && !gui.starting)
3429# endif
3430 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003431# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003432 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003433 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003434 {
3435 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003436 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003437 init_highlight(TRUE, FALSE);
3438 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003439# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003440# ifdef FEAT_TERMINAL
3441 term_update_colors_all();
LemonBoyb2b3acb2022-05-20 10:10:34 +01003442 term_update_palette_all();
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003443 term_update_wincolor_all();
3444# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003445 }
3446#endif
3447
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 /*
3449 * End of handling side effects for bool options.
3450 */
3451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003452 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003453
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 options[opt_idx].flags |= P_WAS_SET;
3455
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003456#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003457 apply_optionset_autocmd(opt_idx, opt_flags,
3458 (long)(old_value ? TRUE : FALSE),
3459 (long)(old_global_value ? TRUE : FALSE),
3460 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003461#endif
3462
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003463 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003464 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003465 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003466 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003467
3468 if ((opt_flags & OPT_NO_REDRAW) == 0)
3469 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003471 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472}
3473
3474/*
3475 * Set the value of a number option, and take care of side effects.
3476 * Returns NULL for success, or an error message for an error.
3477 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003478 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003479set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003480 int opt_idx, // index in options[] table
3481 char_u *varp, // pointer to the option variable
3482 long value, // new value
3483 char *errbuf, // buffer for error messages
3484 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003485 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3486 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003488 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003490#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003491 long old_global_value = 0; // only used when setting a local and
3492 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003493#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003494 long old_Rows = Rows; // remember old Rows
3495 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 long *pp = (long *)varp;
3497
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003498 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003499 if ((secure
3500#ifdef HAVE_SANDBOX
3501 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003503 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003504 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003506#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003507 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003508 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003509 // value (since there is only one value).
3510 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003511 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3512 OPT_GLOBAL);
3513#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003514
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 *pp = value;
3516#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003517 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003518 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003520#ifdef FEAT_GUI
3521 need_mouse_correct = TRUE;
3522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
Bram Moolenaar14f24742012-08-08 18:01:05 +02003524 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003526 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003527#ifdef FEAT_VARTABS
3528 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3529 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003530 ? tabstop_first(curbuf->b_p_vts_array)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003531 : curbuf->b_p_ts;
3532#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536
3537 /*
3538 * Number options that need some action when changed
3539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (pp == &p_wh || pp == &p_hh)
3541 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003542 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (p_wh < 1)
3544 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003545 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 p_wh = 1;
3547 }
3548 if (p_wmh > p_wh)
3549 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003550 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 p_wh = p_wmh;
3552 }
3553 if (p_hh < 0)
3554 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003555 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 p_hh = 0;
3557 }
3558
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003559 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003560 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
3562 if (pp == &p_wh && curwin->w_height < p_wh)
3563 win_setheight((int)p_wh);
3564 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3565 win_setheight((int)p_hh);
3566 }
3567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 else if (pp == &p_wmh)
3569 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003570 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 if (p_wmh < 0)
3572 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003573 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 p_wmh = 0;
3575 }
3576 if (p_wmh > p_wh)
3577 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003578 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 p_wmh = p_wh;
3580 }
3581 win_setminheight();
3582 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003583 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003585 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 if (p_wiw < 1)
3587 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003588 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 p_wiw = 1;
3590 }
3591 if (p_wmw > p_wiw)
3592 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003593 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 p_wiw = p_wmw;
3595 }
3596
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003597 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003598 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 win_setwidth((int)p_wiw);
3600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 else if (pp == &p_wmw)
3602 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003603 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 if (p_wmw < 0)
3605 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003606 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 p_wmw = 0;
3608 }
3609 if (p_wmw > p_wiw)
3610 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003611 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 p_wmw = p_wiw;
3613 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003614 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003617 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 else if (pp == &p_ls)
3619 {
3620 last_status(FALSE);
3621 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003622
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003623 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003624 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003625 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003626 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
3629#ifdef FEAT_GUI
3630 else if (pp == &p_linespace)
3631 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003632 // Recompute gui.char_height and resize the Vim window to keep the
3633 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003634 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003635 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637#endif
3638
3639#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003640 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 else if (pp == &curwin->w_p_fdl)
3642 {
3643 if (curwin->w_p_fdl < 0)
3644 curwin->w_p_fdl = 0;
3645 newFoldLevel();
3646 }
3647
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003648 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 else if (pp == &curwin->w_p_fml)
3650 {
3651 foldUpdateAll(curwin);
3652 }
3653
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003654 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 else if (pp == &curwin->w_p_fdn)
3656 {
3657 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3658 foldUpdateAll(curwin);
3659 }
3660
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003661 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 else if (pp == &curwin->w_p_fdc)
3663 {
3664 if (curwin->w_p_fdc < 0)
3665 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003666 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 curwin->w_p_fdc = 0;
3668 }
3669 else if (curwin->w_p_fdc > 12)
3670 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003671 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 curwin->w_p_fdc = 12;
3673 }
3674 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003675#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003677 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3679 {
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003680#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 if (foldmethodIsIndent(curwin))
3682 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003683#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003684 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3685 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003686 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3687 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003690 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003691 else if (pp == &p_mco)
3692 {
3693 if (p_mco > MAX_MCO)
3694 p_mco = MAX_MCO;
3695 else if (p_mco < 0)
3696 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003697 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003698 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 else if (pp == &curbuf->b_p_iminsert)
3701 {
3702 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3703 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003704 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 curbuf->b_p_iminsert = B_IMODE_NONE;
3706 }
3707 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003708 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003710#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003711 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 status_redraw_curbuf();
3713#endif
3714 }
3715
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003716#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003717 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003718 else if (pp == &p_imst)
3719 {
3720 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003721 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003722 }
3723#endif
3724
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003725 else if (pp == &p_window)
3726 {
3727 if (p_window < 1)
3728 p_window = 1;
3729 else if (p_window >= Rows)
3730 p_window = Rows - 1;
3731 }
3732
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 else if (pp == &curbuf->b_p_imsearch)
3734 {
3735 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3736 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003737 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 curbuf->b_p_imsearch = B_IMODE_NONE;
3739 }
3740 p_imsearch = curbuf->b_p_imsearch;
3741 }
3742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003743 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 else if (pp == &p_titlelen)
3745 {
3746 if (p_titlelen < 0)
3747 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003748 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 p_titlelen = 85;
3750 }
3751 if (starting != NO_SCREEN && old_value != p_titlelen)
3752 need_maketitle = TRUE;
3753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003755 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 else if (pp == &p_ch)
3757 {
Bram Moolenaara2a89732022-08-31 14:46:18 +01003758 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003760 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 p_ch = 1;
3762 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003763 if (p_ch > Rows - min_rows() + 1)
3764 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003766 // Only compute the new window layout when startup has been
3767 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaarc9f5f732022-10-06 11:39:06 +01003768 if ((p_ch != old_value
3769 || tabline_height() + topframe->fr_height != Rows - p_ch)
Bram Moolenaar0816f472022-10-05 15:42:32 +01003770 && full_screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771#ifdef FEAT_GUI
3772 && !gui.starting
3773#endif
Bram Moolenaar0816f472022-10-05 15:42:32 +01003774 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003775 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
3777
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003778 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 else if (pp == &p_uc)
3780 {
3781 if (p_uc < 0)
3782 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003783 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 p_uc = 100;
3785 }
3786 if (p_uc && !old_value)
3787 ml_open_files();
3788 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003789#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003790 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003791 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003792 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003793 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003794 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003795 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003796 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003797 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003798 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003799 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003800 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003801 }
3802 }
3803#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003804#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003805 else if (pp == &p_mzq)
3806 mzvim_reset_timer();
3807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003809#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003810 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003811 else if (pp == &p_pyx)
3812 {
3813 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003814 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003815 }
3816#endif
3817
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003818 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 else if (pp == &p_ul)
3820 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003821 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003823 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 p_ul = value;
3825 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003826 else if (pp == &curbuf->b_p_ul)
3827 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003828 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003829 curbuf->b_p_ul = old_value;
3830 u_sync(TRUE);
3831 curbuf->b_p_ul = value;
3832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003834#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003835 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003836 else if (pp == &curwin->w_p_nuw)
3837 {
3838 if (curwin->w_p_nuw < 1)
3839 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003840 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003841 curwin->w_p_nuw = 1;
3842 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003843 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003844 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003845 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003846 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003847 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003848 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003849 }
3850#endif
3851
Bram Moolenaar1a384422010-07-14 19:53:30 +02003852 else if (pp == &curbuf->b_p_tw)
3853 {
3854 if (curbuf->b_p_tw < 0)
3855 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003856 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003857 curbuf->b_p_tw = 0;
3858 }
3859#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003860 {
3861 win_T *wp;
3862 tabpage_T *tp;
3863
3864 FOR_ALL_TAB_WINDOWS(tp, wp)
3865 check_colorcolumn(wp);
3866 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003867#endif
3868 }
3869
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 /*
3871 * Check the bounds for numeric options here
3872 */
3873 if (Rows < min_rows() && full_screen)
3874 {
3875 if (errbuf != NULL)
3876 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003877 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003878 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 errmsg = errbuf;
3880 }
3881 Rows = min_rows();
3882 }
3883 if (Columns < MIN_COLUMNS && full_screen)
3884 {
3885 if (errbuf != NULL)
3886 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003887 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003888 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 errmsg = errbuf;
3890 }
3891 Columns = MIN_COLUMNS;
3892 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003893 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 /*
3896 * If the screen (shell) height has been changed, assume it is the
3897 * physical screenheight.
3898 */
3899 if (old_Rows != Rows || old_Columns != Columns)
3900 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003901 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 if (updating_screen)
3903 *pp = old_value;
3904 else if (full_screen
3905#ifdef FEAT_GUI
3906 && !gui.starting
3907#endif
3908 )
3909 set_shellsize((int)Columns, (int)Rows, TRUE);
3910 else
3911 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003912 // Postpone the resizing; check the size and cmdline position for
3913 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 check_shellsize();
3915 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3916 cmdline_row = Rows - p_ch;
3917 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003918 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003919 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 if (curbuf->b_p_ts <= 0)
3923 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003924 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 curbuf->b_p_ts = 8;
3926 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003927 else if (curbuf->b_p_ts > TABSTOP_MAX)
3928 {
3929 errmsg = e_invalid_argument;
3930 curbuf->b_p_ts = 8;
3931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 if (p_tm < 0)
3933 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003934 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 p_tm = 0;
3936 }
3937 if ((curwin->w_p_scr <= 0
3938 || (curwin->w_p_scr > curwin->w_height
3939 && curwin->w_height > 0))
3940 && full_screen)
3941 {
3942 if (pp == &(curwin->w_p_scr))
3943 {
3944 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003945 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 win_comp_scroll(curwin);
3947 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003948 // If 'scroll' became invalid because of a side effect silently adjust
3949 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 else if (curwin->w_p_scr <= 0)
3951 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003952 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 curwin->w_p_scr = curwin->w_height;
3954 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003955 if (p_hi < 0)
3956 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003957 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003958 p_hi = 0;
3959 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003960 else if (p_hi > 10000)
3961 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003962 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003963 p_hi = 10000;
3964 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003965 if (p_re < 0 || p_re > 2)
3966 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003967 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003968 p_re = 0;
3969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 if (p_report < 0)
3971 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003972 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 p_report = 1;
3974 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003975 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003977 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 p_sj = Rows / 2;
3979 else
3980 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003981 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 p_sj = 1;
3983 }
3984 }
3985 if (p_so < 0 && full_screen)
3986 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003987 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 p_so = 0;
3989 }
3990 if (p_siso < 0 && full_screen)
3991 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003992 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 p_siso = 0;
3994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (p_cwh < 1)
3996 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003997 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 p_cwh = 1;
3999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 if (p_ut < 0)
4001 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004002 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 p_ut = 2000;
4004 }
4005 if (p_ss < 0)
4006 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004007 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 p_ss = 0;
4009 }
4010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004011 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4013 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4014
4015 options[opt_idx].flags |= P_WAS_SET;
4016
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004017#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004018 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4019 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004020#endif
4021
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004022 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004023 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004024 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004025 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004026 if ((opt_flags & OPT_NO_REDRAW) == 0)
4027 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
4029 return errmsg;
4030}
4031
4032/*
4033 * Called after an option changed: check if something needs to be redrawn.
4034 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004035 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004036check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004038 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004039 int doclear = (flags & P_RCLR) == P_RCLR;
4040 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004042 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4046 changed_window_setting();
4047 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004048 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004049 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004050 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004051 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004052 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004054 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055}
4056
4057/*
4058 * Find index for option 'arg'.
4059 * Return -1 if not found.
4060 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004061 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004062findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063{
4064 int opt_idx;
4065 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004066 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 int is_term_opt;
4068
4069 /*
4070 * For first call: Initialize the quick-access table.
4071 * It contains the index for the first option that starts with a certain
4072 * letter. There are 26 letters, plus the first "t_" option.
4073 */
4074 if (quick_tab[1] == 0)
4075 {
4076 p = options[0].fullname;
4077 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4078 {
4079 if (s[0] != p[0])
4080 {
4081 if (s[0] == 't' && s[1] == '_')
4082 quick_tab[26] = opt_idx;
4083 else
4084 quick_tab[CharOrdLow(s[0])] = opt_idx;
4085 }
4086 p = s;
4087 }
4088 }
4089
4090 /*
4091 * Check for name starting with an illegal character.
4092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 return -1;
4095
4096 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4097 if (is_term_opt)
4098 opt_idx = quick_tab[26];
4099 else
4100 opt_idx = quick_tab[CharOrdLow(arg[0])];
4101 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4102 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004103 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 break;
4105 }
4106 if (s == NULL && !is_term_opt)
4107 {
4108 opt_idx = quick_tab[CharOrdLow(arg[0])];
4109 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4110 {
4111 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004112 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 break;
4114 s = NULL;
4115 }
4116 }
4117 if (s == NULL)
4118 opt_idx = -1;
4119 return opt_idx;
4120}
4121
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004122#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4123 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124/*
4125 * Get the value for an option.
4126 *
4127 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004128 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004129 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004130 * String option: gov_string, *stringval gets allocated string.
4131 * Hidden Number option: gov_hidden_number.
4132 * Hidden Toggle option: gov_hidden_bool.
4133 * Hidden String option: gov_hidden_string.
4134 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004135 *
4136 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004138 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004139get_option_value(
4140 char_u *name,
4141 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004142 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004143 int *flagsp,
4144 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145{
4146 int opt_idx;
4147 char_u *varp;
4148
4149 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004150 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004151 {
4152 int key;
4153
4154 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004155 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004156 {
4157 char_u key_name[2];
4158 char_u *p;
4159
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004160 if (flagsp != NULL)
4161 *flagsp = 0; // terminal option has no flags
4162
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004163 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004164 if (key < 0)
4165 {
4166 key_name[0] = KEY2TERMCAP0(key);
4167 key_name[1] = KEY2TERMCAP1(key);
4168 }
4169 else
4170 {
4171 key_name[0] = KS_KEY;
4172 key_name[1] = (key & 0xff);
4173 }
4174 p = find_termcode(key_name);
4175 if (p != NULL)
4176 {
4177 if (stringval != NULL)
4178 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004179 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004180 }
4181 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004182 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004185 varp = get_varp_scope(&(options[opt_idx]), scope);
4186
4187 if (flagsp != NULL)
4188 // Return the P_xxxx option flags.
4189 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
4191 if (options[opt_idx].flags & P_STRING)
4192 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004193 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004194 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if (stringval != NULL)
4196 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004197 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004198 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4199 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004201 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004202 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004203 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004206 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 *stringval = vim_strsave(*(char_u **)(varp));
4208 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004209 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
4211
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004212 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004213 return (options[opt_idx].flags & P_NUM)
4214 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 if (options[opt_idx].flags & P_NUM)
4216 *numval = *(long *)varp;
4217 else
4218 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004219 // Special case: 'modified' is b_changed, but we also want to consider
4220 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 if ((int *)varp == &curbuf->b_changed)
4222 *numval = curbufIsChanged();
4223 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004224 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004226 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227}
4228#endif
4229
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004230#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004231/*
4232 * Returns the option attributes and its value. Unlike the above function it
4233 * will return either global value or local value of the option depending on
4234 * what was requested, but it will never return global value if it was
4235 * requested to return local one and vice versa. Neither it will return
4236 * buffer-local value if it was requested to return window-local one.
4237 *
4238 * Pretends that option is absent if it is not present in the requested scope
4239 * (i.e. has no global, window-local or buffer-local value depending on
4240 * opt_type). Uses
4241 *
4242 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004243 * 0 hidden or unknown option, also option that does not have requested
4244 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004245 * see SOPT_* in vim.h for other flags
4246 *
4247 * Possible opt_type values: see SREQ_* in vim.h
4248 */
4249 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004250get_option_value_strict(
4251 char_u *name,
4252 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004253 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004254 int opt_type,
4255 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004256{
4257 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004258 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004259 struct vimoption *p;
4260 int r = 0;
4261
4262 opt_idx = findoption(name);
4263 if (opt_idx < 0)
4264 return 0;
4265
4266 p = &(options[opt_idx]);
4267
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004268 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004269 if (p->var == NULL)
4270 return 0;
4271
4272 if (p->flags & P_BOOL)
4273 r |= SOPT_BOOL;
4274 else if (p->flags & P_NUM)
4275 r |= SOPT_NUM;
4276 else if (p->flags & P_STRING)
4277 r |= SOPT_STRING;
4278
4279 if (p->indir == PV_NONE)
4280 {
4281 if (opt_type == SREQ_GLOBAL)
4282 r |= SOPT_GLOBAL;
4283 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004284 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004285 }
4286 else
4287 {
4288 if (p->indir & PV_BOTH)
4289 r |= SOPT_GLOBAL;
4290 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004291 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004292
4293 if (p->indir & PV_WIN)
4294 {
4295 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004296 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004297 else
4298 r |= SOPT_WIN;
4299 }
4300 else if (p->indir & PV_BUF)
4301 {
4302 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004303 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004304 else
4305 r |= SOPT_BUF;
4306 }
4307 }
4308
4309 if (stringval == NULL)
4310 return r;
4311
4312 if (opt_type == SREQ_GLOBAL)
4313 varp = p->var;
4314 else
4315 {
4316 if (opt_type == SREQ_BUF)
4317 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004318 // Special case: 'modified' is b_changed, but we also want to
4319 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004320 if (p->indir == PV_MOD)
4321 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004322 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004323 varp = NULL;
4324 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004325# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004326 else if (p->indir == PV_KEY)
4327 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004328 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004329 *stringval = NULL;
4330 varp = NULL;
4331 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004332# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004333 else
4334 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004335 buf_T *save_curbuf = curbuf;
4336
4337 // only getting a pointer, no need to use aucmd_prepbuf()
4338 curbuf = (buf_T *)from;
4339 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004340 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004341 curbuf = save_curbuf;
4342 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004343 }
4344 }
4345 else if (opt_type == SREQ_WIN)
4346 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004347 win_T *save_curwin = curwin;
4348
4349 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004350 curbuf = curwin->w_buffer;
4351 varp = get_varp(p);
4352 curwin = save_curwin;
4353 curbuf = curwin->w_buffer;
4354 }
4355 if (varp == p->var)
4356 return (r | SOPT_UNSET);
4357 }
4358
4359 if (varp != NULL)
4360 {
4361 if (p->flags & P_STRING)
4362 *stringval = vim_strsave(*(char_u **)(varp));
4363 else if (p->flags & P_NUM)
4364 *numval = *(long *) varp;
4365 else
4366 *numval = *(int *)varp;
4367 }
4368
4369 return r;
4370}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004371
4372/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004373 * Iterate over options. First argument is a pointer to a pointer to a
4374 * structure inside options[] array, second is option type like in the above
4375 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004376 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004377 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004378 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004379 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004380 * first argument to NULL.
4381 *
4382 * Returns full option name for current option on each call.
4383 */
4384 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004385option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004386{
4387 struct vimoption *ret = NULL;
4388 do
4389 {
4390 if (*option == NULL)
4391 *option = (void *) options;
4392 else if (((struct vimoption *) (*option))->fullname == NULL)
4393 {
4394 *option = NULL;
4395 return NULL;
4396 }
4397 else
4398 *option = (void *) (((struct vimoption *) (*option)) + 1);
4399
4400 ret = ((struct vimoption *) (*option));
4401
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004402 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004403 if (ret->var == NULL)
4404 {
4405 ret = NULL;
4406 continue;
4407 }
4408
4409 switch (opt_type)
4410 {
4411 case SREQ_GLOBAL:
4412 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4413 ret = NULL;
4414 break;
4415 case SREQ_BUF:
4416 if (!(ret->indir & PV_BUF))
4417 ret = NULL;
4418 break;
4419 case SREQ_WIN:
4420 if (!(ret->indir & PV_WIN))
4421 ret = NULL;
4422 break;
4423 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004424 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004425 return NULL;
4426 }
4427 }
4428 while (ret == NULL);
4429
4430 return (char_u *)ret->fullname;
4431}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004432#endif
4433
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004435 * Return the flags for the option at 'opt_idx'.
4436 */
4437 long_u
4438get_option_flags(int opt_idx)
4439{
4440 return options[opt_idx].flags;
4441}
4442
4443/*
4444 * Set a flag for the option at 'opt_idx'.
4445 */
4446 void
4447set_option_flag(int opt_idx, long_u flag)
4448{
4449 options[opt_idx].flags |= flag;
4450}
4451
4452/*
4453 * Clear a flag for the option at 'opt_idx'.
4454 */
4455 void
4456clear_option_flag(int opt_idx, long_u flag)
4457{
4458 options[opt_idx].flags &= ~flag;
4459}
4460
4461/*
4462 * Returns TRUE if the option at 'opt_idx' is a global option
4463 */
4464 int
4465is_global_option(int opt_idx)
4466{
4467 return options[opt_idx].indir == PV_NONE;
4468}
4469
4470/*
4471 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4472 * local value.
4473 */
4474 int
4475is_global_local_option(int opt_idx)
4476{
4477 return options[opt_idx].indir & PV_BOTH;
4478}
4479
4480/*
4481 * Returns TRUE if the option at 'opt_idx' is a window-local option
4482 */
4483 int
4484is_window_local_option(int opt_idx)
4485{
4486 return options[opt_idx].var == VAR_WIN;
4487}
4488
4489/*
4490 * Returns TRUE if the option at 'opt_idx' is a hidden option
4491 */
4492 int
4493is_hidden_option(int opt_idx)
4494{
4495 return options[opt_idx].var == NULL;
4496}
4497
4498#if defined(FEAT_CRYPT) || defined(PROTO)
4499/*
4500 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4501 */
4502 int
4503is_crypt_key_option(int opt_idx)
4504{
4505 return options[opt_idx].indir == PV_KEY;
4506}
4507#endif
4508
4509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 * Set the value of option "name".
4511 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004512 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004513 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004515 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004516set_option_value(
4517 char_u *name,
4518 long number,
4519 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004520 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521{
4522 int opt_idx;
4523 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004524 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
4526 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004527 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004528 {
4529 int key;
4530
4531 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004532 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004533 {
4534 char_u key_name[2];
4535
4536 if (key < 0)
4537 {
4538 key_name[0] = KEY2TERMCAP0(key);
4539 key_name[1] = KEY2TERMCAP1(key);
4540 }
4541 else
4542 {
4543 key_name[0] = KS_KEY;
4544 key_name[1] = (key & 0xff);
4545 }
4546 add_termcode(key_name, string, FALSE);
4547 if (full_screen)
4548 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004549 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01004550 return NULL;
4551 }
4552
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004553 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 else
4556 {
4557 flags = options[opt_idx].flags;
4558#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004559 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004561 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004562 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004563 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004566 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004567 return set_string_option(opt_idx, string, opt_flags);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004568
4569 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
4570 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004572 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004574 int idx;
4575
4576 // Either we are given a string or we are setting option
4577 // to zero.
4578 for (idx = 0; string[idx] == '0'; ++idx)
4579 ;
4580 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004581 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004582 // There's another character after zeros or the string
4583 // is empty. In both cases, we are trying to set a
4584 // num option using a string.
4585 semsg(_(e_number_required_after_str_equal_str),
4586 name, string);
4587 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004588
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004591 if (flags & P_NUM)
4592 return set_num_option(opt_idx, varp, number,
4593 NULL, 0, opt_flags);
4594 else
4595 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004599 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600}
4601
4602/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004603 * Call set_option_value() and when an error is returned report it.
4604 */
4605 void
4606set_option_value_give_err(
4607 char_u *name,
4608 long number,
4609 char_u *string,
4610 int opt_flags) // OPT_LOCAL or 0 (both)
4611{
4612 char *errmsg = set_option_value(name, number, string, opt_flags);
4613
4614 if (errmsg != NULL)
4615 emsg(_(errmsg));
4616}
4617
4618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 * Get the terminal code for a terminal option.
4620 * Returns NULL when not found.
4621 */
4622 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004623get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624{
4625 int opt_idx;
4626 char_u *varp;
4627
4628 if (tname[0] != 't' || tname[1] != '_' ||
4629 tname[2] == NUL || tname[3] == NUL)
4630 return NULL;
4631 if ((opt_idx = findoption(tname)) >= 0)
4632 {
4633 varp = get_varp(&(options[opt_idx]));
4634 if (varp != NULL)
4635 varp = *(char_u **)(varp);
4636 return varp;
4637 }
4638 return find_termcode(tname + 2);
4639}
4640
4641 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004642get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643{
4644 int i;
4645
4646 i = findoption((char_u *)"hl");
4647 if (i >= 0)
4648 return options[i].def_val[VI_DEFAULT];
4649 return (char_u *)NULL;
4650}
4651
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004652 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004653get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004654{
4655 int i;
4656
4657 i = findoption((char_u *)"enc");
4658 if (i >= 0)
4659 return options[i].def_val[VI_DEFAULT];
4660 return (char_u *)NULL;
4661}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004662
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004663#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004664 int
4665is_option_allocated(char *name)
4666{
4667 int idx = findoption((char_u *)name);
4668
4669 return idx >= 0 && (options[idx].flags & P_ALLOCED);
4670}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004671#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004672
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004673#if defined(FEAT_EVAL) || defined(PROTO)
4674/*
4675 * Return TRUE if "name" is a string option.
4676 * Returns FALSE if option "name" does not exist.
4677 */
4678 int
4679is_string_option(char_u *name)
4680{
4681 int idx = findoption(name);
4682
4683 return idx >= 0 && (options[idx].flags & P_STRING);
4684}
4685#endif
4686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687/*
4688 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004689 * When "has_lt" is true there is a '<' before "*arg_arg".
4690 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 */
4692 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004693find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004695 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004697 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698
4699 /*
4700 * Don't use get_special_key_code() for t_xx, we don't want it to call
4701 * add_termcap_entry().
4702 */
4703 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4704 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004705 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004707 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004709 key = find_special_key(&arg, &modifiers,
4710 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004711 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 key = 0;
4713 }
4714 return key;
4715}
4716
4717/*
4718 * if 'all' == 0: show changed options
4719 * if 'all' == 1: show all normal options
4720 * if 'all' == 2: show all terminal options
4721 */
4722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004723showoptions(
4724 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004725 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
4727 struct vimoption *p;
4728 int col;
4729 int isterm;
4730 char_u *varp;
4731 struct vimoption **items;
4732 int item_count;
4733 int run;
4734 int row, rows;
4735 int cols;
4736 int i;
4737 int len;
4738
4739#define INC 20
4740#define GAP 3
4741
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004742 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 if (items == NULL)
4744 return;
4745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004746 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004748 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004750 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004752 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004754 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004757 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 * 1. display the short items
4759 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004760 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 */
4762 for (run = 1; run <= 2 && !got_int; ++run)
4763 {
4764 /*
4765 * collect the items in items[]
4766 */
4767 item_count = 0;
4768 for (p = &options[0]; p->fullname != NULL; p++)
4769 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004770 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004771 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004772 continue;
4773
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 varp = NULL;
4775 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004776 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
4778 if (p->indir != PV_NONE && !isterm)
4779 varp = get_varp_scope(p, opt_flags);
4780 }
4781 else
4782 varp = get_varp(p);
4783 if (varp != NULL
4784 && ((all == 2 && isterm)
4785 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004786 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004788 if (opt_flags & OPT_ONECOLUMN)
4789 len = Columns;
4790 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004791 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 else
4793 {
4794 option_value2string(p, opt_flags);
4795 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4796 }
4797 if ((len <= INC - GAP && run == 1) ||
4798 (len > INC - GAP && run == 2))
4799 items[item_count++] = p;
4800 }
4801 }
4802
4803 /*
4804 * display the items
4805 */
4806 if (run == 1)
4807 {
4808 cols = (Columns + GAP - 3) / INC;
4809 if (cols == 0)
4810 cols = 1;
4811 rows = (item_count + cols - 1) / cols;
4812 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004813 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 rows = item_count;
4815 for (row = 0; row < rows && !got_int; ++row)
4816 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004817 msg_putchar('\n'); // go to next line
4818 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 break;
4820 col = 0;
4821 for (i = row; i < item_count; i += rows)
4822 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004823 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 showoneopt(items[i], opt_flags);
4825 col += INC;
4826 }
4827 out_flush();
4828 ui_breakcheck();
4829 }
4830 }
4831 vim_free(items);
4832}
4833
4834/*
4835 * Return TRUE if option "p" has its default value.
4836 */
4837 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004838optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
4840 int dvi;
4841
4842 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004843 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004844 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004846 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004848 // the cast to long is required for Manx C, long_i is
4849 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004850 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004851 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4853}
4854
4855/*
4856 * showoneopt: show the value of one option
4857 * must not be called with a hidden option!
4858 */
4859 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004860showoneopt(
4861 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004862 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004864 char_u *varp;
4865 int save_silent = silent_mode;
4866
4867 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004868 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
4870 varp = get_varp_scope(p, opt_flags);
4871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004872 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4874 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004875 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004877 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004879 msg_puts(" ");
4880 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 if (!(p->flags & P_BOOL))
4882 {
4883 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004884 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 option_value2string(p, opt_flags);
4886 msg_outtrans(NameBuff);
4887 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004888
4889 silent_mode = save_silent;
4890 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891}
4892
4893/*
4894 * Write modified options as ":set" commands to a file.
4895 *
4896 * There are three values for "opt_flags":
4897 * OPT_GLOBAL: Write global option values and fresh values of
4898 * buffer-local options (used for start of a session
4899 * file).
4900 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4901 * curwin (used for a vimrc file).
4902 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4903 * and local values for window-local options of
4904 * curwin. Local values are also written when at the
4905 * default value, because a modeline or autocommand
4906 * may have set them when doing ":edit file" and the
4907 * user has set them back at the default or fresh
4908 * value.
4909 * When "local_only" is TRUE, don't write fresh
4910 * values, only local values (for ":mkview").
4911 * (fresh value = value used for a new buffer or window for a local option).
4912 *
4913 * Return FAIL on error, OK otherwise.
4914 */
4915 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004916makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917{
4918 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004919 char_u *varp; // currently used value
4920 char_u *varp_fresh; // local value
4921 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 char *cmd;
4923 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004924 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
4926 /*
4927 * The options that don't have a default (terminal name, columns, lines)
4928 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004929 * Do the loop over "options[]" twice: once for options with the
4930 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004932 for (pri = 1; pri >= 0; --pri)
4933 {
4934 for (p = &options[0]; !istermoption(p); p++)
4935 if (!(p->flags & P_NO_MKRC)
4936 && !istermoption(p)
4937 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004939 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4941 continue;
4942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004943 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4944 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4946 continue;
4947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004948 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004950 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 continue;
4952
Bram Moolenaard23b7142021-04-17 21:04:34 +02004953 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4954 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004955 continue;
4956
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 round = 2;
4958 if (p->indir != PV_NONE)
4959 {
4960 if (p->var == VAR_WIN)
4961 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004962 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 if (!(opt_flags & OPT_LOCAL))
4964 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004965 // When fresh value of window-local option is not at the
4966 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4968 {
4969 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004970 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
4972 round = 1;
4973 varp_local = varp;
4974 varp = varp_fresh;
4975 }
4976 }
4977 }
4978 }
4979
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004980 // Round 1: fresh value for window-local options.
4981 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 for ( ; round <= 2; varp = varp_local, ++round)
4983 {
4984 if (round == 1 || (opt_flags & OPT_GLOBAL))
4985 cmd = "set";
4986 else
4987 cmd = "setlocal";
4988
4989 if (p->flags & P_BOOL)
4990 {
4991 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4992 return FAIL;
4993 }
4994 else if (p->flags & P_NUM)
4995 {
4996 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4997 return FAIL;
4998 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004999 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005001 int do_endif = FALSE;
5002
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005003 // Don't set 'syntax' and 'filetype' again if the value is
5004 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005005 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005006#if defined(FEAT_SYN_HL)
5007 p->indir == PV_SYN ||
5008#endif
5009 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
5011 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5012 *(char_u **)(varp)) < 0
5013 || put_eol(fd) < 0)
5014 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005015 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005018 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005020 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
5022 if (put_line(fd, "endif") == FAIL)
5023 return FAIL;
5024 }
5025 }
5026 }
5027 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 return OK;
5030}
5031
5032#if defined(FEAT_FOLDING) || defined(PROTO)
5033/*
5034 * Generate set commands for the local fold options only. Used when
5035 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5036 */
5037 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005038makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005040 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005042 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 == FAIL
5044# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005045 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005047 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 == FAIL
5049 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5050 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5051 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5052 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5053 )
5054 return FAIL;
5055
5056 return OK;
5057}
5058#endif
5059
5060 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005061put_setstring(
5062 FILE *fd,
5063 char *cmd,
5064 char *name,
5065 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005066 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067{
5068 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005069 char_u *buf = NULL;
5070 char_u *part = NULL;
5071 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5074 return FAIL;
5075 if (*valuep != NULL)
5076 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005077 // Output 'pastetoggle' as key names. For other
5078 // options some characters have to be escaped with
5079 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 if (valuep == &p_pt)
5081 {
5082 s = *valuep;
5083 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005084 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 return FAIL;
5086 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005087 // expand the option value, replace $HOME by ~
5088 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005090 int size = (int)STRLEN(*valuep) + 1;
5091
5092 // replace home directory in the whole option value into "buf"
5093 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005094 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005095 goto fail;
5096 home_replace(NULL, *valuep, buf, size, FALSE);
5097
5098 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005099 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005100 // can be expanded when read back.
5101 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5102 && vim_strchr(*valuep, ',') != NULL)
5103 {
5104 part = alloc(size);
5105 if (part == NULL)
5106 goto fail;
5107
5108 // write line break to clear the option, e.g. ':set rtp='
5109 if (put_eol(fd) == FAIL)
5110 goto fail;
5111
5112 p = buf;
5113 while (*p != NUL)
5114 {
5115 // for each comma separated option part, append value to
5116 // the option, :set rtp+=value
5117 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5118 goto fail;
5119 (void)copy_option_part(&p, part, size, ",");
5120 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5121 goto fail;
5122 }
5123 vim_free(buf);
5124 vim_free(part);
5125 return OK;
5126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005128 {
5129 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005131 }
5132 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
5134 else if (put_escstr(fd, *valuep, 2) == FAIL)
5135 return FAIL;
5136 }
5137 if (put_eol(fd) < 0)
5138 return FAIL;
5139 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005140fail:
5141 vim_free(buf);
5142 vim_free(part);
5143 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144}
5145
5146 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005147put_setnum(
5148 FILE *fd,
5149 char *cmd,
5150 char *name,
5151 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
5153 long wc;
5154
5155 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5156 return FAIL;
5157 if (wc_use_keyname((char_u *)valuep, &wc))
5158 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005159 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5161 return FAIL;
5162 }
5163 else if (fprintf(fd, "%ld", *valuep) < 0)
5164 return FAIL;
5165 if (put_eol(fd) < 0)
5166 return FAIL;
5167 return OK;
5168}
5169
5170 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005171put_setbool(
5172 FILE *fd,
5173 char *cmd,
5174 char *name,
5175 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005177 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005178 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5180 || put_eol(fd) < 0)
5181 return FAIL;
5182 return OK;
5183}
5184
5185/*
5186 * Clear all the terminal options.
5187 * If the option has been allocated, free the memory.
5188 * Terminal options are never hidden or indirect.
5189 */
5190 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005191clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 /*
5194 * Reset a few things before clearing the old options. This may cause
5195 * outputting a few things that the terminal doesn't understand, but the
5196 * screen will be cleared later, so this is OK.
5197 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005198 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005199 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005201 // When starting the GUI close the display opened for the clipboard.
5202 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (gui.starting)
5204 clear_xterm_clip();
5205#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005206 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005208 free_termoptions();
5209}
5210
5211 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005212free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005213{
5214 struct vimoption *p;
5215
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005216 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (istermoption(p))
5218 {
5219 if (p->flags & P_ALLOCED)
5220 free_string_option(*(char_u **)(p->var));
5221 if (p->flags & P_DEF_ALLOCED)
5222 free_string_option(p->def_val[VI_DEFAULT]);
5223 *(char_u **)(p->var) = empty_option;
5224 p->def_val[VI_DEFAULT] = empty_option;
5225 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005226#ifdef FEAT_EVAL
5227 // remember where the option was cleared
5228 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 }
5231 clear_termcodes();
5232}
5233
5234/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005235 * Free the string for one term option, if it was allocated.
5236 * Set the string to empty_option and clear allocated flag.
5237 * "var" points to the option value.
5238 */
5239 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005240free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005241{
5242 struct vimoption *p;
5243
5244 for (p = &options[0]; p->fullname != NULL; p++)
5245 if (p->var == var)
5246 {
5247 if (p->flags & P_ALLOCED)
5248 free_string_option(*(char_u **)(p->var));
5249 *(char_u **)(p->var) = empty_option;
5250 p->flags &= ~P_ALLOCED;
5251 break;
5252 }
5253}
5254
5255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 * Set the terminal option defaults to the current value.
5257 * Used after setting the terminal name.
5258 */
5259 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005260set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
5262 struct vimoption *p;
5263
5264 for (p = &options[0]; p->fullname != NULL; p++)
5265 {
5266 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5267 {
5268 if (p->flags & P_DEF_ALLOCED)
5269 {
5270 free_string_option(p->def_val[VI_DEFAULT]);
5271 p->flags &= ~P_DEF_ALLOCED;
5272 }
5273 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5274 if (p->flags & P_ALLOCED)
5275 {
5276 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005277 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279 }
5280 }
5281}
5282
5283/*
5284 * return TRUE if 'p' starts with 't_'
5285 */
5286 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005287istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288{
5289 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5290}
5291
Bram Moolenaardac13472019-09-16 21:06:21 +02005292/*
5293 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5294 */
5295 int
5296istermoption_idx(int opt_idx)
5297{
5298 return istermoption(&options[opt_idx]);
5299}
5300
Bram Moolenaar113e1072019-01-20 15:30:40 +01005301#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005303 * Unset local option value, similar to ":set opt<".
5304 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005305 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005306unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005307{
5308 struct vimoption *p;
5309 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005310 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005311
5312 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005313 if (opt_idx < 0)
5314 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005315 p = &(options[opt_idx]);
5316
5317 switch ((int)p->indir)
5318 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005319 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005320 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005321 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005322 break;
5323 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005324 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005325 break;
5326 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005327 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005328 break;
5329 case PV_AR:
5330 buf->b_p_ar = -1;
5331 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005332 case PV_BKC:
5333 clear_string_option(&buf->b_p_bkc);
5334 buf->b_bkc_flags = 0;
5335 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005336 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005337 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005338 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005339 case PV_TC:
5340 clear_string_option(&buf->b_p_tc);
5341 buf->b_tc_flags = 0;
5342 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005343 case PV_SISO:
5344 curwin->w_p_siso = -1;
5345 break;
5346 case PV_SO:
5347 curwin->w_p_so = -1;
5348 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005349# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005350 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005351 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005352 break;
5353 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005354 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005355 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005356# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005357 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005358 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005359 break;
5360 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005361 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005362 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005363# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005364 case PV_TSRFU:
5365 clear_string_option(&buf->b_p_tsrfu);
5366 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005367# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005368 case PV_FP:
5369 clear_string_option(&buf->b_p_fp);
5370 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005371# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005372 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005373 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005374 break;
5375 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005376 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005377 break;
5378 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005379 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005380 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005381# endif
5382# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005383 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005384 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005385 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005386# endif
5387# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005388 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005389 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005390 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005391# endif
5392# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01005393 case PV_SBR:
5394 clear_string_option(&((win_T *)from)->w_p_sbr);
5395 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005396# endif
5397# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005398 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005399 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005400 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005401# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005402 case PV_UL:
5403 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5404 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005405 case PV_LW:
5406 clear_string_option(&buf->b_p_lw);
5407 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005408 case PV_MENC:
5409 clear_string_option(&buf->b_p_menc);
5410 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005411 case PV_LCS:
5412 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005413 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005414 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005415 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005416 case PV_FCS:
5417 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005418 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005419 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005420 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005421 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005422 clear_string_option(&((win_T *)from)->w_p_ve);
5423 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005424 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005425 }
5426}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005427#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005428
5429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005431 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 */
5433 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005434get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005436 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 {
5438 if (p->var == VAR_WIN)
5439 return (char_u *)GLOBAL_WO(get_varp(p));
5440 return p->var;
5441 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005442 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 {
5444 switch ((int)p->indir)
5445 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005446 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005448 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5449 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5450 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005452 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5453 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5454 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005455 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005456 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005457 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005458 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5459 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005461 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5462 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005464 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5465 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005466#ifdef FEAT_COMPL_FUNC
5467 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5468#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005469#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5470 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5471#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005472#if defined(FEAT_CRYPT)
5473 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5474#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005475#ifdef FEAT_LINEBREAK
5476 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5477#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005478#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005479 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005480#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005481 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005482 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005483 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005484 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005485 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005486 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005487 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005488
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005490 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 }
5492 return get_varp(p);
5493}
5494
5495/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005496 * Get pointer to option variable at 'opt_idx', depending on local or global
5497 * scope.
5498 */
5499 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005500get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005501{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005502 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005503}
5504
5505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 * Get pointer to option variable.
5507 */
5508 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005509get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005511 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 if (p->var == NULL)
5513 return NULL;
5514
5515 switch ((int)p->indir)
5516 {
5517 case PV_NONE: return p->var;
5518
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005519 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005520 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005522 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005524 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005526 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005528 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005530 case PV_TC: return *curbuf->b_p_tc != NUL
5531 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005532 case PV_BKC: return *curbuf->b_p_bkc != NUL
5533 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005534 case PV_SISO: return curwin->w_p_siso >= 0
5535 ? (char_u *)&(curwin->w_p_siso) : p->var;
5536 case PV_SO: return curwin->w_p_so >= 0
5537 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005539 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005541 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5543#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005544 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005546 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005548#ifdef FEAT_COMPL_FUNC
5549 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5550 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5551#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005552 case PV_FP: return *curbuf->b_p_fp != NUL
5553 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005555 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005557 case PV_GP: return *curbuf->b_p_gp != NUL
5558 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5559 case PV_MP: return *curbuf->b_p_mp != NUL
5560 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005562#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5563 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5564 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5565#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005566#if defined(FEAT_CRYPT)
5567 case PV_CM: return *curbuf->b_p_cm != NUL
5568 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5569#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005570#ifdef FEAT_LINEBREAK
5571 case PV_SBR: return *curwin->w_p_sbr != NUL
5572 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5573#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005574#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005575 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005576 ? (char_u *)&(curwin->w_p_stl) : p->var;
5577#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005578 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5579 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005580 case PV_LW: return *curbuf->b_p_lw != NUL
5581 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005582 case PV_MENC: return *curbuf->b_p_menc != NUL
5583 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584#ifdef FEAT_ARABIC
5585 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5586#endif
5587 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005588 case PV_LCS: return *curwin->w_p_lcs != NUL
5589 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005590 case PV_FCS: return *curwin->w_p_fcs != NUL
5591 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005592 case PV_VE: return *curwin->w_p_ve != NUL
5593 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005594#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005595 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5596#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005597#ifdef FEAT_SYN_HL
5598 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5599 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005600 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005601 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603#ifdef FEAT_DIFF
5604 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5605#endif
5606#ifdef FEAT_FOLDING
5607 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5608 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5609 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5610 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5611 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5612 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5613 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5614# ifdef FEAT_EVAL
5615 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5616 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5617# endif
5618 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5619#endif
5620 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005621 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005622#ifdef FEAT_LINEBREAK
5623 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005626 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005627#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5629#endif
5630#ifdef FEAT_RIGHTLEFT
5631 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5632 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5633#endif
5634 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01005635 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5637#ifdef FEAT_LINEBREAK
5638 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005639 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5640 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005642 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005644 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005645#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005646 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5647 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5648#endif
5649#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005650 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5651 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5652 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
5655 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5656 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5659 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5661 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5663 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5664 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01005665 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668#ifdef FEAT_FOLDING
5669 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005672#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005673 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005675#ifdef FEAT_COMPL_FUNC
5676 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005677 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005678#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005679#ifdef FEAT_EVAL
5680 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5681#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01005682 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005684 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005690 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5692 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5693 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5694 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5695#ifdef FEAT_FIND_ID
5696# ifdef FEAT_EVAL
5697 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5698# endif
5699#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01005700#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5702 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5703#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005704#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005705 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707#ifdef FEAT_CRYPT
5708 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01005711 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5713 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5714 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5715 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5716 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005718 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5725#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005726 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005727 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5728#endif
5729#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005730 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5731 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5732 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005733 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734#endif
5735 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5736 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5737 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5738 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005739#ifdef FEAT_PERSISTENT_UNDO
5740 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5743#ifdef FEAT_KEYMAP
5744 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5745#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005746#ifdef FEAT_SIGNS
5747 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5748#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005749#ifdef FEAT_VARTABS
5750 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5751 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5752#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005753 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005755 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 return (char_u *)&(curbuf->b_p_wm);
5757}
5758
5759/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005760 * Return a pointer to the variable for option at 'opt_idx'
5761 */
5762 char_u *
5763get_option_var(int opt_idx)
5764{
5765 return options[opt_idx].var;
5766}
5767
Dominique Pelle748b3082022-01-08 12:41:16 +00005768#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005769/*
5770 * Return the full name of the option at 'opt_idx'
5771 */
5772 char_u *
5773get_option_fullname(int opt_idx)
5774{
5775 return (char_u *)options[opt_idx].fullname;
5776}
Dominique Pelle748b3082022-01-08 12:41:16 +00005777#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005778
5779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 * Get the value of 'equalprg', either the buffer-local one or the global one.
5781 */
5782 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005783get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
5785 if (*curbuf->b_p_ep == NUL)
5786 return p_ep;
5787 return curbuf->b_p_ep;
5788}
5789
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790/*
5791 * Copy options from one window to another.
5792 * Used when splitting a window.
5793 */
5794 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005795win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796{
5797 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5798 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005799 after_copy_winopt(wp_to);
5800}
5801
5802/*
5803 * After copying window options: update variables depending on options.
5804 */
5805 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00005806after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005807{
5808#ifdef FEAT_LINEBREAK
5809 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005810#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005811#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005812 fill_culopt_flags(NULL, wp);
5813 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005814#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005815 set_chars_option(wp, &wp->w_p_lcs, TRUE);
5816 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005817}
5818
5819 static char_u *
5820copy_option_val(char_u *val)
5821{
5822 if (val == empty_option)
5823 return empty_option; // no need to allocate memory
5824 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
5827/*
5828 * Copy the options from one winopt_T to another.
5829 * Doesn't free the old option values in "to", use clear_winopt() for that.
5830 * The 'scroll' option is not copied, because it depends on the window height.
5831 * The 'previewwindow' option is reset, there can be only one preview window.
5832 */
5833 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005834copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836#ifdef FEAT_ARABIC
5837 to->wo_arab = from->wo_arab;
5838#endif
5839 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005840 to->wo_lcs = copy_option_val(from->wo_lcs);
5841 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005843 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005844 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02005845 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005846#ifdef FEAT_LINEBREAK
5847 to->wo_nuw = from->wo_nuw;
5848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849#ifdef FEAT_RIGHTLEFT
5850 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005851 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005853#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005854 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01005855#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005856#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005857 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005860#ifdef FEAT_DIFF
5861 to->wo_wrap_save = from->wo_wrap_save;
5862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863#ifdef FEAT_LINEBREAK
5864 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005865 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005866 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005868 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005870 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01005871 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005872 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005873 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005874#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005875 to->wo_spell = from->wo_spell;
5876#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005877#ifdef FEAT_SYN_HL
5878 to->wo_cuc = from->wo_cuc;
5879 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005880 to->wo_culopt = copy_option_val(from->wo_culopt);
5881 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883#ifdef FEAT_DIFF
5884 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005885 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005887#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005888 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005889 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005890#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005891#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005892 to->wo_twk = copy_option_val(from->wo_twk);
5893 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#ifdef FEAT_FOLDING
5896 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005897 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005899 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005900 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 to->wo_fml = from->wo_fml;
5902 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005903 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005904 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005905 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005906 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 to->wo_fdn = from->wo_fdn;
5908# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005909 to->wo_fde = copy_option_val(from->wo_fde);
5910 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005912 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005914#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005915 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005916#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005917
5918#ifdef FEAT_EVAL
5919 // Copy the script context so that we know where the value was last set.
5920 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5921 sizeof(to->wo_script_ctx));
5922#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005923 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924}
5925
5926/*
5927 * Check string options in a window for a NULL value.
5928 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005929 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005930check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931{
5932 check_winopt(&win->w_onebuf_opt);
5933 check_winopt(&win->w_allbuf_opt);
5934}
5935
5936/*
5937 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5938 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005939 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005940check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941{
5942#ifdef FEAT_FOLDING
5943 check_string_option(&wop->wo_fdi);
5944 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005945 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946# ifdef FEAT_EVAL
5947 check_string_option(&wop->wo_fde);
5948 check_string_option(&wop->wo_fdt);
5949# endif
5950 check_string_option(&wop->wo_fmr);
5951#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005952#ifdef FEAT_SIGNS
5953 check_string_option(&wop->wo_scl);
5954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955#ifdef FEAT_RIGHTLEFT
5956 check_string_option(&wop->wo_rlc);
5957#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005958#ifdef FEAT_LINEBREAK
5959 check_string_option(&wop->wo_sbr);
5960#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005961#ifdef FEAT_STL_OPT
5962 check_string_option(&wop->wo_stl);
5963#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005964#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005965 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005966 check_string_option(&wop->wo_cc);
5967#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005968#ifdef FEAT_CONCEAL
5969 check_string_option(&wop->wo_cocu);
5970#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005971#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005972 check_string_option(&wop->wo_twk);
5973 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005974#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005975#ifdef FEAT_LINEBREAK
5976 check_string_option(&wop->wo_briopt);
5977#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005978 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005979 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005980 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005981 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982}
5983
5984/*
5985 * Free the allocated memory inside a winopt_T.
5986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005988clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
5990#ifdef FEAT_FOLDING
5991 clear_string_option(&wop->wo_fdi);
5992 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005993 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994# ifdef FEAT_EVAL
5995 clear_string_option(&wop->wo_fde);
5996 clear_string_option(&wop->wo_fdt);
5997# endif
5998 clear_string_option(&wop->wo_fmr);
5999#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006000#ifdef FEAT_SIGNS
6001 clear_string_option(&wop->wo_scl);
6002#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006003#ifdef FEAT_LINEBREAK
6004 clear_string_option(&wop->wo_briopt);
6005#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006006 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007#ifdef FEAT_RIGHTLEFT
6008 clear_string_option(&wop->wo_rlc);
6009#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006010#ifdef FEAT_LINEBREAK
6011 clear_string_option(&wop->wo_sbr);
6012#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006013#ifdef FEAT_STL_OPT
6014 clear_string_option(&wop->wo_stl);
6015#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006016#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006017 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006018 clear_string_option(&wop->wo_cc);
6019#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006020#ifdef FEAT_CONCEAL
6021 clear_string_option(&wop->wo_cocu);
6022#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006023#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006024 clear_string_option(&wop->wo_twk);
6025 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006026#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006027 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006028 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006029 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030}
6031
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006032#ifdef FEAT_EVAL
6033// Index into the options table for a buffer-local option enum.
6034static int buf_opt_idx[BV_COUNT];
6035# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6036
6037/*
6038 * Initialize buf_opt_idx[] if not done already.
6039 */
6040 static void
6041init_buf_opt_idx(void)
6042{
6043 static int did_init_buf_opt_idx = FALSE;
6044 int i;
6045
6046 if (did_init_buf_opt_idx)
6047 return;
6048 did_init_buf_opt_idx = TRUE;
6049 for (i = 0; !istermoption_idx(i); i++)
6050 if (options[i].indir & PV_BUF)
6051 buf_opt_idx[options[i].indir & PV_MASK] = i;
6052}
6053#else
6054# define COPY_OPT_SCTX(buf, bv)
6055#endif
6056
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057/*
6058 * Copy global option values to local options for one buffer.
6059 * Used when creating a new buffer and sometimes when entering a buffer.
6060 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006061 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6063 * appropriate.
6064 * BCO_NOHELP Don't copy the values to a help buffer.
6065 */
6066 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006067buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
6069 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006070 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 int dont_do_help;
6072 int did_isk = FALSE;
6073
6074 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 * Skip this when the option defaults have not been set yet. Happens when
6076 * main() allocates the first buffer.
6077 */
6078 if (p_cpo != NULL)
6079 {
6080 /*
6081 * Always copy when entering and 'cpo' contains 'S'.
6082 * Don't copy when already initialized.
6083 * Don't copy when 'cpo' contains 's' and not entering.
6084 * 'S' BCO_ENTER initialized 's' should_copy
6085 * yes yes X X TRUE
6086 * yes no yes X FALSE
6087 * no X yes X FALSE
6088 * X no no yes FALSE
6089 * X no no no TRUE
6090 * no yes no X TRUE
6091 */
6092 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6093 && (buf->b_p_initialized
6094 || (!(flags & BCO_ENTER)
6095 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6096 should_copy = FALSE;
6097
6098 if (should_copy || (flags & BCO_ALWAYS))
6099 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006100#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006101 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006102 init_buf_opt_idx();
6103#endif
6104 // Don't copy the options specific to a help buffer when
6105 // BCO_NOHELP is given or the options were initialized already
6106 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6108 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006109 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 {
6111 save_p_isk = buf->b_p_isk;
6112 buf->b_p_isk = NULL;
6113 }
6114 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02006115 * Always free the allocated strings. If not already initialized,
6116 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 */
6118 if (!buf->b_p_initialized)
6119 {
6120 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006121 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006124 switch (*p_ffs)
6125 {
6126 case 'm':
6127 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6128 case 'd':
6129 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6130 case 'u':
6131 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6132 default:
6133 buf->b_p_ff = vim_strsave(p_ff);
6134 }
6135 if (buf->b_p_ff != NULL)
6136 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 buf->b_p_bh = empty_option;
6138 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 }
6140 else
6141 free_buf_options(buf, FALSE);
6142
6143 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006144 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 buf->b_p_ai_nopaste = p_ai_nopaste;
6146 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006147 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006149 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 buf->b_p_tw_nopaste = p_tw_nopaste;
6151 buf->b_p_tw_nobin = p_tw_nobin;
6152 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006153 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 buf->b_p_wm_nopaste = p_wm_nopaste;
6155 buf->b_p_wm_nobin = p_wm_nobin;
6156 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006157 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006158 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006159 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006160 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006161 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006163 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006165 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006167 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 buf->b_p_ml_nobin = p_ml_nobin;
6169 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006170 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006171 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006172 buf->b_p_swf = FALSE;
6173 else
6174 {
6175 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006176 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006179 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006180#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006181 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006182 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006184#ifdef FEAT_COMPL_FUNC
6185 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006186 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006187 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006188 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006189 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006190 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006191#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006192#ifdef FEAT_EVAL
6193 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006194 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006195 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006198 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006200#ifdef FEAT_VARTABS
6201 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006202 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006203 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006204 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006205 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006206 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006207 buf->b_p_vsts_nopaste = p_vsts_nopaste
6208 ? vim_strsave(p_vsts_nopaste) : NULL;
6209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006211 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006213 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214#ifdef FEAT_FOLDING
6215 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006216 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217#endif
6218 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006219 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006220 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006221 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006222 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6223 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006225 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006227 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006229 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006231 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006232
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006234 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006236 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006238 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006239 buf->b_p_cinsd = vim_strsave(p_cinsd);
6240 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006241 buf->b_p_lop = vim_strsave(p_lop);
6242 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006243
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006244 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006247 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006249 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006251 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006253 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006255 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006256 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006257 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006258#endif
6259#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006260 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006261 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006262 (void)compile_cap_prog(&buf->b_s);
6263 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006264 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006265 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006266 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006267 buf->b_s.b_p_spo = vim_strsave(p_spo);
6268 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006270#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006272 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006274 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006276 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006277#if defined(FEAT_EVAL)
6278 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006279 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281#ifdef FEAT_CRYPT
6282 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006283 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006286 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287#ifdef FEAT_KEYMAP
6288 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006289 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 buf->b_kmap_state |= KEYMAP_INIT;
6291#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006292#ifdef FEAT_TERMINAL
6293 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006294 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006295#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006296 // This isn't really an option, but copying the langmap and IME
6297 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006299 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006301 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006303 // options that are normally global but also have a local value
6304 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006306 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006307 buf->b_p_bkc = empty_option;
6308 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309#ifdef FEAT_QUICKFIX
6310 buf->b_p_gp = empty_option;
6311 buf->b_p_mp = empty_option;
6312 buf->b_p_efm = empty_option;
6313#endif
6314 buf->b_p_ep = empty_option;
6315 buf->b_p_kp = empty_option;
6316 buf->b_p_path = empty_option;
6317 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006318 buf->b_p_tc = empty_option;
6319 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320#ifdef FEAT_FIND_ID
6321 buf->b_p_def = empty_option;
6322 buf->b_p_inc = empty_option;
6323# ifdef FEAT_EVAL
6324 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006325 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326# endif
6327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 buf->b_p_dict = empty_option;
6329 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006330#ifdef FEAT_COMPL_FUNC
6331 buf->b_p_tsrfu = empty_option;
6332#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006333 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006334 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006335#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6336 buf->b_p_bexpr = empty_option;
6337#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006338#if defined(FEAT_CRYPT)
6339 buf->b_p_cm = empty_option;
6340#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006341#ifdef FEAT_PERSISTENT_UNDO
6342 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006343 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006344#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006345 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006346 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347
6348 /*
6349 * Don't copy the options set by ex_help(), use the saved values,
6350 * when going from a help buffer to a non-help buffer.
6351 * Don't touch these at all when BCO_NOHELP is used and going from
6352 * or to a help buffer.
6353 */
6354 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006355 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006357#ifdef FEAT_VARTABS
6358 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006359 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006360 else
6361 buf->b_p_vts_array = NULL;
6362#endif
6363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 else
6365 {
6366 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006367 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 did_isk = TRUE;
6369 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006370 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006371#ifdef FEAT_VARTABS
6372 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006373 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006374 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006375 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006376 else
6377 buf->b_p_vts_array = NULL;
6378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 if (buf->b_p_bt[0] == 'h')
6381 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006383 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 }
6385 }
6386
6387 /*
6388 * When the options should be copied (ignoring BCO_ALWAYS), set the
6389 * flag that indicates that the options have been initialized.
6390 */
6391 if (should_copy)
6392 buf->b_p_initialized = TRUE;
6393 }
6394
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006395 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 if (did_isk)
6397 (void)buf_init_chartab(buf, FALSE);
6398}
6399
6400/*
6401 * Reset the 'modifiable' option and its default value.
6402 */
6403 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006404reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405{
6406 int opt_idx;
6407
6408 curbuf->b_p_ma = FALSE;
6409 p_ma = FALSE;
6410 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006411 if (opt_idx >= 0)
6412 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413}
6414
6415/*
6416 * Set the global value for 'iminsert' to the local value.
6417 */
6418 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006419set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420{
6421 p_iminsert = curbuf->b_p_iminsert;
6422}
6423
6424/*
6425 * Set the global value for 'imsearch' to the local value.
6426 */
6427 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006428set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
6430 p_imsearch = curbuf->b_p_imsearch;
6431}
6432
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433static int expand_option_idx = -1;
6434static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6435static int expand_option_flags = 0;
6436
6437 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006438set_context_in_set_cmd(
6439 expand_T *xp,
6440 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006441 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442{
6443 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006444 long_u flags = 0; // init for GCC
6445 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 char_u *p;
6447 char_u *s;
6448 int is_term_option = FALSE;
6449 int key;
6450
6451 expand_option_flags = opt_flags;
6452
6453 xp->xp_context = EXPAND_SETTINGS;
6454 if (*arg == NUL)
6455 {
6456 xp->xp_pattern = arg;
6457 return;
6458 }
6459 p = arg + STRLEN(arg) - 1;
6460 if (*p == ' ' && *(p - 1) != '\\')
6461 {
6462 xp->xp_pattern = p + 1;
6463 return;
6464 }
6465 while (p > arg)
6466 {
6467 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006468 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 if (*p == ' ' || *p == ',')
6470 {
6471 while (s > arg && *(s - 1) == '\\')
6472 --s;
6473 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006474 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 if (*p == ' ' && ((p - s) & 1) == 0)
6476 {
6477 ++p;
6478 break;
6479 }
6480 --p;
6481 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006482 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 {
6484 xp->xp_context = EXPAND_BOOL_SETTINGS;
6485 p += 2;
6486 }
6487 if (STRNCMP(p, "inv", 3) == 0)
6488 {
6489 xp->xp_context = EXPAND_BOOL_SETTINGS;
6490 p += 3;
6491 }
6492 xp->xp_pattern = arg = p;
6493 if (*arg == '<')
6494 {
6495 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006496 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 return;
6498 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006499 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 {
6501 xp->xp_context = EXPAND_NOTHING;
6502 return;
6503 }
6504 nextchar = *++p;
6505 is_term_option = TRUE;
6506 expand_option_name[2] = KEY2TERMCAP0(key);
6507 expand_option_name[3] = KEY2TERMCAP1(key);
6508 }
6509 else
6510 {
6511 if (p[0] == 't' && p[1] == '_')
6512 {
6513 p += 2;
6514 if (*p != NUL)
6515 ++p;
6516 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006517 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 nextchar = *++p;
6519 is_term_option = TRUE;
6520 expand_option_name[2] = p[-2];
6521 expand_option_name[3] = p[-1];
6522 }
6523 else
6524 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006525 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6527 p++;
6528 if (*p == NUL)
6529 return;
6530 nextchar = *p;
6531 *p = NUL;
6532 opt_idx = findoption(arg);
6533 *p = nextchar;
6534 if (opt_idx == -1 || options[opt_idx].var == NULL)
6535 {
6536 xp->xp_context = EXPAND_NOTHING;
6537 return;
6538 }
6539 flags = options[opt_idx].flags;
6540 if (flags & P_BOOL)
6541 {
6542 xp->xp_context = EXPAND_NOTHING;
6543 return;
6544 }
6545 }
6546 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006547 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6549 {
6550 ++p;
6551 nextchar = '=';
6552 }
6553 if ((nextchar != '=' && nextchar != ':')
6554 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6555 {
6556 xp->xp_context = EXPAND_UNSUCCESSFUL;
6557 return;
6558 }
6559 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6560 {
6561 xp->xp_context = EXPAND_OLD_SETTING;
6562 if (is_term_option)
6563 expand_option_idx = -1;
6564 else
6565 expand_option_idx = opt_idx;
6566 xp->xp_pattern = p + 1;
6567 return;
6568 }
6569 xp->xp_context = EXPAND_NOTHING;
6570 if (is_term_option || (flags & P_NUM))
6571 return;
6572
6573 xp->xp_pattern = p + 1;
6574
6575 if (flags & P_EXPAND)
6576 {
6577 p = options[opt_idx].var;
6578 if (p == (char_u *)&p_bdir
6579 || p == (char_u *)&p_dir
6580 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006581 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584#ifdef FEAT_SESSION
6585 || p == (char_u *)&p_vdir
6586#endif
6587 )
6588 {
6589 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006590 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 xp->xp_backslash = XP_BS_THREE;
6592 else
6593 xp->xp_backslash = XP_BS_ONE;
6594 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006595 else if (p == (char_u *)&p_ft)
6596 {
6597 xp->xp_context = EXPAND_FILETYPE;
6598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 else
6600 {
6601 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006602 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if (p == (char_u *)&p_tags)
6604 xp->xp_backslash = XP_BS_THREE;
6605 else
6606 xp->xp_backslash = XP_BS_ONE;
6607 }
6608 }
6609
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006610 // For an option that is a list of file names, find the start of the
6611 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6613 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006614 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 if (*p == ' ' || *p == ',')
6616 {
6617 s = p;
6618 while (s > xp->xp_pattern && *(s - 1) == '\\')
6619 --s;
6620 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6621 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6622 {
6623 xp->xp_pattern = p + 1;
6624 break;
6625 }
6626 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006627
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006628#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006629 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006630 if (options[opt_idx].var == (char_u *)&p_sps
6631 && STRNCMP(p, "file:", 5) == 0)
6632 {
6633 xp->xp_pattern = p + 5;
6634 break;
6635 }
6636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638}
6639
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006640/*
6641 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
6642 *
6643 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
6644 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
6645 *
6646 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
6647 * regular expression 'regmatch', then stores the match in matches[idx] and
6648 * returns TRUE.
6649 *
6650 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6651 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
6652 *
6653 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6654 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
6655 */
6656 static int
6657match_str(
6658 char_u *str,
6659 regmatch_T *regmatch,
6660 char_u **matches,
6661 int idx,
6662 int test_only,
6663 int fuzzy,
6664 char_u *fuzzystr,
6665 fuzmatch_str_T *fuzmatch)
6666{
6667 if (!fuzzy)
6668 {
6669 if (vim_regexec(regmatch, str, (colnr_T)0))
6670 {
6671 if (!test_only)
6672 matches[idx] = vim_strsave(str);
6673 return TRUE;
6674 }
6675 }
6676 else
6677 {
6678 int score;
6679
6680 score = fuzzy_match_str(str, fuzzystr);
6681 if (score != 0)
6682 {
6683 if (!test_only)
6684 {
6685 fuzmatch[idx].idx = idx;
6686 fuzmatch[idx].str = vim_strsave(str);
6687 fuzmatch[idx].score = score;
6688 }
6689
6690 return TRUE;
6691 }
6692 }
6693
6694 return FALSE;
6695}
6696
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006698ExpandSettings(
6699 expand_T *xp,
6700 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006701 char_u *fuzzystr,
6702 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01006703 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006704 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006706 int num_normal = 0; // Nr of matching non-term-code settings
6707 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 int opt_idx;
6709 int match;
6710 int count = 0;
6711 char_u *str;
6712 int loop;
6713 int is_term_opt;
6714 char_u name_buf[MAX_KEY_NAME_LEN];
6715 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006716 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006717 int fuzzy;
6718 fuzmatch_str_T *fuzmatch = NULL;
6719
Christian Brabandtcb747892022-05-08 21:10:56 +01006720 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006722 // do this loop twice:
6723 // loop == 0: count the number of matching options
6724 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 for (loop = 0; loop <= 1; ++loop)
6726 {
6727 regmatch->rm_ic = ic;
6728 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6729 {
K.Takataeeec2542021-06-02 13:28:16 +02006730 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006731 {
6732 if (match_str((char_u *)names[match], regmatch, *matches,
6733 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {
6735 if (loop == 0)
6736 num_normal++;
6737 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006738 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 }
6742 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6743 opt_idx++)
6744 {
6745 if (options[opt_idx].var == NULL)
6746 continue;
6747 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6748 && !(options[opt_idx].flags & P_BOOL))
6749 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006750 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 if (is_term_opt && num_normal > 0)
6752 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006753
6754 if (match_str(str, regmatch, *matches, count, (loop == 0),
6755 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {
6757 if (loop == 0)
6758 {
6759 if (is_term_opt)
6760 num_term++;
6761 else
6762 num_normal++;
6763 }
6764 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006765 count++;
6766 }
6767 else if (!fuzzy && options[opt_idx].shortname != NULL
6768 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006769 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006770 {
6771 // Compare against the abbreviated option name (for regular
6772 // expression match). Fuzzy matching (previous if) already
6773 // matches against both the expanded and abbreviated names.
6774 if (loop == 0)
6775 {
6776 if (is_term_opt)
6777 num_term++;
6778 else
6779 num_normal++;
6780 }
6781 else
6782 (*matches)[count++] = vim_strsave(str);
6783 }
6784 else if (is_term_opt)
6785 {
6786 name_buf[0] = '<';
6787 name_buf[1] = 't';
6788 name_buf[2] = '_';
6789 name_buf[3] = str[2];
6790 name_buf[4] = str[3];
6791 name_buf[5] = '>';
6792 name_buf[6] = NUL;
6793
6794 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6795 fuzzy, fuzzystr, fuzmatch))
6796 {
6797 if (loop == 0)
6798 num_term++;
6799 else
6800 count++;
6801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
6803 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006804
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 /*
6806 * Check terminal key codes, these are not in the option table
6807 */
6808 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6809 {
6810 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6811 {
6812 if (!isprint(str[0]) || !isprint(str[1]))
6813 continue;
6814
6815 name_buf[0] = 't';
6816 name_buf[1] = '_';
6817 name_buf[2] = str[0];
6818 name_buf[3] = str[1];
6819 name_buf[4] = NUL;
6820
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006821 if (match_str(name_buf, regmatch, *matches, count,
6822 (loop == 0), fuzzy, fuzzystr, fuzmatch))
6823 {
6824 if (loop == 0)
6825 num_term++;
6826 else
6827 count++;
6828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 else
6830 {
6831 name_buf[0] = '<';
6832 name_buf[1] = 't';
6833 name_buf[2] = '_';
6834 name_buf[3] = str[0];
6835 name_buf[4] = str[1];
6836 name_buf[5] = '>';
6837 name_buf[6] = NUL;
6838
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006839 if (match_str(name_buf, regmatch, *matches, count,
6840 (loop == 0), fuzzy, fuzzystr,
6841 fuzmatch))
6842 {
6843 if (loop == 0)
6844 num_term++;
6845 else
6846 count++;
6847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 }
6849 }
6850
6851 /*
6852 * Check special key names.
6853 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006854 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6856 {
6857 name_buf[0] = '<';
6858 STRCPY(name_buf + 1, str);
6859 STRCAT(name_buf, ">");
6860
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006861 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6862 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 {
6864 if (loop == 0)
6865 num_term++;
6866 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006867 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 }
6869 }
6870 }
6871 if (loop == 0)
6872 {
6873 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006874 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006876 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 else
6878 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006879 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006881 *matches = ALLOC_MULT(char_u *, *numMatches);
6882 if (*matches == NULL)
6883 {
6884 *matches = (char_u **)"";
6885 return FAIL;
6886 }
6887 }
6888 else
6889 {
6890 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
6891 if (fuzmatch == NULL)
6892 {
6893 *matches = (char_u **)"";
6894 return FAIL;
6895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 }
6897 }
6898 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006899
6900 if (fuzzy &&
6901 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
6902 return FAIL;
6903
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 return OK;
6905}
6906
6907 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00006908ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006910 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 char_u *buf;
6912
zeertzjqb0d45ec2023-01-25 15:04:22 +00006913 *numMatches = 0;
6914 *matches = ALLOC_ONE(char_u *);
6915 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 return FAIL;
6917
6918 /*
6919 * For a terminal key code expand_option_idx is < 0.
6920 */
6921 if (expand_option_idx < 0)
6922 {
6923 var = find_termcode(expand_option_name + 2);
6924 if (var == NULL)
6925 expand_option_idx = findoption(expand_option_name);
6926 }
6927
6928 if (expand_option_idx >= 0)
6929 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006930 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 option_value2string(&options[expand_option_idx], expand_option_flags);
6932 var = NameBuff;
6933 }
6934 else if (var == NULL)
6935 var = (char_u *)"";
6936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006937 // A backslash is required before some characters. This is the reverse of
6938 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 buf = vim_strsave_escaped(var, escape_chars);
6940
6941 if (buf == NULL)
6942 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00006943 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 return FAIL;
6945 }
6946
6947#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006948 // For MS-Windows et al. we don't double backslashes at the start and
6949 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006950 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 if (var[0] == '\\' && var[1] == '\\'
6952 && expand_option_idx >= 0
6953 && (options[expand_option_idx].flags & P_EXPAND)
6954 && vim_isfilec(var[2])
6955 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006956 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957#endif
6958
zeertzjqb0d45ec2023-01-25 15:04:22 +00006959 *matches[0] = buf;
6960 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 return OK;
6962}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
6964/*
6965 * Get the value for the numeric or string option *opp in a nice format into
6966 * NameBuff[]. Must not be called with a hidden option!
6967 */
6968 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006969option_value2string(
6970 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006971 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972{
6973 char_u *varp;
6974
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006975 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
6977 if (opp->flags & P_NUM)
6978 {
6979 long wc = 0;
6980
6981 if (wc_use_keyname(varp, &wc))
6982 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6983 else if (wc != 0)
6984 STRCPY(NameBuff, transchar((int)wc));
6985 else
6986 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6987 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006988 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {
6990 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006991 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 NameBuff[0] = NUL;
6993#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006994 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006995 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 STRCPY(NameBuff, "*****");
6997#endif
6998 else if (opp->flags & P_EXPAND)
6999 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007000 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 else if ((char_u **)opp->var == &p_pt)
7002 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7003 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007004 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 }
7006}
7007
7008/*
7009 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7010 * printed as a keyname.
7011 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7012 */
7013 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007014wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015{
7016 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7017 {
7018 *wcp = *(long *)varp;
7019 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7020 return TRUE;
7021 }
7022 return FALSE;
7023}
7024
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 * Return TRUE if "x" is present in 'shortmess' option, or
7027 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7028 */
7029 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007030shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007032 return p_shm != NULL &&
7033 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 || (vim_strchr(p_shm, 'a') != NULL
7035 && vim_strchr((char_u *)SHM_A, x) != NULL));
7036}
7037
7038/*
7039 * paste_option_changed() - Called after p_paste was set or reset.
7040 */
7041 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007042paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043{
7044 static int old_p_paste = FALSE;
7045 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007046 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048#ifdef FEAT_RIGHTLEFT
7049 static int save_ri = 0;
7050 static int save_hkmap = 0;
7051#endif
7052 buf_T *buf;
7053
7054 if (p_paste)
7055 {
7056 /*
7057 * Paste switched from off to on.
7058 * Save the current values, so they can be restored later.
7059 */
7060 if (!old_p_paste)
7061 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007062 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007063 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {
7065 buf->b_p_tw_nopaste = buf->b_p_tw;
7066 buf->b_p_wm_nopaste = buf->b_p_wm;
7067 buf->b_p_sts_nopaste = buf->b_p_sts;
7068 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007069 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007070#ifdef FEAT_VARTABS
7071 if (buf->b_p_vsts_nopaste)
7072 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007073 buf->b_p_vsts_nopaste =
7074 buf->b_p_vsts && buf->b_p_vsts != empty_option
7075 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 }
7078
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007079 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007081 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083#ifdef FEAT_RIGHTLEFT
7084 save_ri = p_ri;
7085 save_hkmap = p_hkmap;
7086#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007087 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007088 p_ai_nopaste = p_ai;
7089 p_et_nopaste = p_et;
7090 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 p_tw_nopaste = p_tw;
7092 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007093#ifdef FEAT_VARTABS
7094 if (p_vsts_nopaste)
7095 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007096 p_vsts_nopaste = p_vsts && p_vsts != empty_option
7097 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 }
7100
7101 /*
7102 * Always set the option values, also when 'paste' is set when it is
7103 * already on.
7104 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007105 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007106 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007108 buf->b_p_tw = 0; // textwidth is 0
7109 buf->b_p_wm = 0; // wrapmargin is 0
7110 buf->b_p_sts = 0; // softtabstop is 0
7111 buf->b_p_ai = 0; // no auto-indent
7112 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007113#ifdef FEAT_VARTABS
7114 if (buf->b_p_vsts)
7115 free_string_option(buf->b_p_vsts);
7116 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007117 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 }
7120
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007121 // set global options
7122 p_sm = 0; // no showmatch
7123 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007125 status_redraw_all(); // redraw to remove the ruler
7126 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007128 p_ri = 0; // no reverse insert
7129 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007131 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 p_tw = 0;
7133 p_wm = 0;
7134 p_sts = 0;
7135 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007136#ifdef FEAT_VARTABS
7137 if (p_vsts)
7138 free_string_option(p_vsts);
7139 p_vsts = empty_option;
7140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 }
7142
7143 /*
7144 * Paste switched from on to off: Restore saved values.
7145 */
7146 else if (old_p_paste)
7147 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007148 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007149 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 {
7151 buf->b_p_tw = buf->b_p_tw_nopaste;
7152 buf->b_p_wm = buf->b_p_wm_nopaste;
7153 buf->b_p_sts = buf->b_p_sts_nopaste;
7154 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007155 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007156#ifdef FEAT_VARTABS
7157 if (buf->b_p_vsts)
7158 free_string_option(buf->b_p_vsts);
7159 buf->b_p_vsts = buf->b_p_vsts_nopaste
7160 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007161 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007162 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007163 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007164 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007165 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 }
7168
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007169 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007171 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007173 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175#ifdef FEAT_RIGHTLEFT
7176 p_ri = save_ri;
7177 p_hkmap = save_hkmap;
7178#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007179 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007180 p_ai = p_ai_nopaste;
7181 p_et = p_et_nopaste;
7182 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 p_tw = p_tw_nopaste;
7184 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007185#ifdef FEAT_VARTABS
7186 if (p_vsts)
7187 free_string_option(p_vsts);
7188 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 }
7191
7192 old_p_paste = p_paste;
7193}
7194
7195/*
7196 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7197 *
7198 * Reset 'compatible' and set the values for options that didn't get set yet
7199 * to the Vim defaults.
7200 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007201 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 */
7203 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007204vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007206 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007207 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007208 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209
7210 if (!option_was_set((char_u *)"cp"))
7211 {
7212 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007213 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7215 set_option_default(opt_idx, OPT_FREE, FALSE);
7216 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007217 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007219
7220 if (fname != NULL)
7221 {
7222 p = vim_getenv(envname, &dofree);
7223 if (p == NULL)
7224 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007225 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007226 p = FullName_save(fname, FALSE);
7227 if (p != NULL)
7228 {
7229 vim_setenv(envname, p);
7230 vim_free(p);
7231 }
7232 }
7233 else if (dofree)
7234 vim_free(p);
7235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236}
7237
7238/*
7239 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7240 */
7241 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007242change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007244 int opt_idx;
7245
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 if (p_cp != on)
7247 {
7248 p_cp = on;
7249 compatible_set();
7250 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007251 opt_idx = findoption((char_u *)"cp");
7252 if (opt_idx >= 0)
7253 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254}
7255
7256/*
7257 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007258 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 */
7260 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007261option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262{
7263 int idx;
7264
7265 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007266 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 return FALSE;
7268 if (options[idx].flags & P_WAS_SET)
7269 return TRUE;
7270 return FALSE;
7271}
7272
7273/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007274 * Reset the flag indicating option "name" was set.
7275 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007276 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007277reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007278{
7279 int idx = findoption(name);
7280
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007281 if (idx < 0)
7282 return FAIL;
7283
7284 options[idx].flags &= ~P_WAS_SET;
7285 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007286}
7287
7288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 * compatible_set() - Called when 'compatible' has been set or unset.
7290 *
7291 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7292 * flag) to a Vi compatible value.
7293 * When 'compatible' is unset: Set all options that have a different default
7294 * for Vim (without the P_VI_DEF flag) to that default.
7295 */
7296 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007297compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298{
7299 int opt_idx;
7300
Bram Moolenaardac13472019-09-16 21:06:21 +02007301 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7303 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7304 set_option_default(opt_idx, OPT_FREE, p_cp);
7305 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007306 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307}
7308
Bram Moolenaardac13472019-09-16 21:06:21 +02007309#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311/*
7312 * fill_breakat_flags() -- called when 'breakat' changes value.
7313 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007314 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007315fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007317 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 int i;
7319
7320 for (i = 0; i < 256; i++)
7321 breakat_flags[i] = FALSE;
7322
7323 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007324 for (p = p_breakat; *p; p++)
7325 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327#endif
7328
7329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 * Check if backspacing over something is allowed.
7331 */
7332 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007333can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007334 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007336#ifdef FEAT_JOB_CHANNEL
7337 if (what == BS_START && bt_prompt(curbuf))
7338 return FALSE;
7339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 switch (*p_bs)
7341 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007342 case '3': return TRUE;
7343 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 case '1': return (what != BS_START);
7345 case '0': return FALSE;
7346 }
7347 return vim_strchr(p_bs, what) != NULL;
7348}
7349
7350/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007351 * Return the effective 'scrolloff' value for the current window, using the
7352 * global value when appropriate.
7353 */
7354 long
7355get_scrolloff_value(void)
7356{
7357 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7358}
7359
7360/*
7361 * Return the effective 'sidescrolloff' value for the current window, using the
7362 * global value when appropriate.
7363 */
7364 long
7365get_sidescrolloff_value(void)
7366{
7367 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7368}
7369
7370/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007371 * Get the local or global value of 'backupcopy'.
7372 */
7373 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007374get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007375{
7376 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7377}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007378
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007379#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007380/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007381 * Get the local or global value of 'formatlistpat'.
7382 */
7383 char_u *
7384get_flp_value(buf_T *buf)
7385{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007386 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7387 return p_flp;
7388 return buf->b_p_flp;
7389}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007390#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007391
7392/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007393 * Get the local or global value of the 'virtualedit' flags.
7394 */
7395 unsigned int
7396get_ve_flags(void)
7397{
Gary Johnson51ad8502021-08-03 18:33:08 +02007398 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7399 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007400}
7401
Bram Moolenaaree857022019-11-09 23:26:40 +01007402#if defined(FEAT_LINEBREAK) || defined(PROTO)
7403/*
7404 * Get the local or global value of 'showbreak'.
7405 */
7406 char_u *
7407get_showbreak_value(win_T *win)
7408{
7409 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7410 return p_sbr;
7411 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7412 return empty_option;
7413 return win->w_p_sbr;
7414}
7415#endif
7416
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007417#if defined(FEAT_EVAL) || defined(PROTO)
7418/*
7419 * Get window or buffer local options.
7420 */
7421 dict_T *
7422get_winbuf_options(int bufopt)
7423{
7424 dict_T *d;
7425 int opt_idx;
7426
7427 d = dict_alloc();
7428 if (d == NULL)
7429 return NULL;
7430
Bram Moolenaardac13472019-09-16 21:06:21 +02007431 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007432 {
7433 struct vimoption *opt = &options[opt_idx];
7434
7435 if ((bufopt && (opt->indir & PV_BUF))
7436 || (!bufopt && (opt->indir & PV_WIN)))
7437 {
7438 char_u *varp = get_varp(opt);
7439
7440 if (varp != NULL)
7441 {
7442 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007443 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007444 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007445 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007446 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007447 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007448 }
7449 }
7450 }
7451
7452 return d;
7453}
7454#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007455
Bram Moolenaardac13472019-09-16 21:06:21 +02007456#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007457/*
7458 * This is called when 'culopt' is changed
7459 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007460 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007461fill_culopt_flags(char_u *val, win_T *wp)
7462{
7463 char_u *p;
7464 char_u culopt_flags_new = 0;
7465
7466 if (val == NULL)
7467 p = wp->w_p_culopt;
7468 else
7469 p = val;
7470 while (*p != NUL)
7471 {
7472 if (STRNCMP(p, "line", 4) == 0)
7473 {
7474 p += 4;
7475 culopt_flags_new |= CULOPT_LINE;
7476 }
7477 else if (STRNCMP(p, "both", 4) == 0)
7478 {
7479 p += 4;
7480 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7481 }
7482 else if (STRNCMP(p, "number", 6) == 0)
7483 {
7484 p += 6;
7485 culopt_flags_new |= CULOPT_NBR;
7486 }
7487 else if (STRNCMP(p, "screenline", 10) == 0)
7488 {
7489 p += 10;
7490 culopt_flags_new |= CULOPT_SCRLINE;
7491 }
7492
7493 if (*p != ',' && *p != NUL)
7494 return FAIL;
7495 if (*p == ',')
7496 ++p;
7497 }
7498
7499 // Can't have both "line" and "screenline".
7500 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7501 return FAIL;
7502 wp->w_p_culopt_flags = culopt_flags_new;
7503
7504 return OK;
7505}
7506#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007507
7508/*
7509 * Get the value of 'magic' adjusted for Vim9 script.
7510 */
7511 int
7512magic_isset(void)
7513{
7514 switch (magic_overruled)
7515 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007516 case OPTION_MAGIC_ON: return TRUE;
7517 case OPTION_MAGIC_OFF: return FALSE;
7518 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007519 }
7520#ifdef FEAT_EVAL
7521 if (in_vim9script())
7522 return TRUE;
7523#endif
7524 return p_magic;
7525}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007526
7527/*
7528 * Set the callback function value for an option that accepts a function name,
7529 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7530 * Returns OK if the option is successfully set to a function, otherwise
7531 * returns FAIL.
7532 */
7533 int
7534option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7535{
7536#ifdef FEAT_EVAL
7537 typval_T *tv;
7538 callback_T cb;
7539
7540 if (optval == NULL || *optval == NUL)
7541 {
7542 free_callback(optcb);
7543 return OK;
7544 }
7545
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007546 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007547 || (STRNCMP(optval, "function(", 9) == 0)
7548 || (STRNCMP(optval, "funcref(", 8) == 0))
7549 // Lambda expression or a funcref
7550 tv = eval_expr(optval, NULL);
7551 else
7552 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007553 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007554 if (tv == NULL)
7555 return FAIL;
7556
7557 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007558 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007559 {
7560 free_tv(tv);
7561 return FAIL;
7562 }
7563
7564 free_callback(optcb);
7565 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00007566 if (cb.cb_free_name)
7567 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007568 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007569
7570 // when using Vim9 style "import.funcname" it needs to be expanded to
7571 // "import#funcname".
7572 expand_autload_callback(optcb);
7573
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007574 return OK;
7575#else
7576 return FAIL;
7577#endif
7578}