blob: c95082d4e712830e9caea0c1894ad18e5cf5dd52 [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
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000543set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200544{
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/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00001818 * Set a boolean option.
1819 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001820 */
1821 static char *
1822do_set_option_bool(
1823 int opt_idx,
1824 int opt_flags,
1825 set_prefix_T prefix,
1826 long_u flags,
1827 char_u *varp,
1828 int nextchar,
1829 int afterchar,
1830 int cp_val)
1831
1832{
1833 varnumber_T value;
1834
1835 if (nextchar == '=' || nextchar == ':')
1836 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00001837 if (opt_idx < 0 || varp == NULL)
1838 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001839
1840 /*
1841 * ":set opt!": invert
1842 * ":set opt&": reset to default value
1843 * ":set opt<": reset to global value
1844 */
1845 if (nextchar == '!')
1846 value = *(int *)(varp) ^ 1;
1847 else if (nextchar == '&')
1848 value = (int)(long)(long_i)options[opt_idx].def_val[
1849 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
1850 else if (nextchar == '<')
1851 {
1852 // For 'autoread' -1 means to use global value.
1853 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
1854 value = -1;
1855 else
1856 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
1857 }
1858 else
1859 {
1860 /*
1861 * ":set invopt": invert
1862 * ":set opt" or ":set noopt": set or reset
1863 */
1864 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
1865 return e_trailing_characters;
1866 if (prefix == PREFIX_INV)
1867 value = *(int *)(varp) ^ 1;
1868 else
1869 value = prefix == PREFIX_NO ? 0 : 1;
1870 }
1871
1872 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
1873}
1874
1875/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00001876 * Set a numeric option.
1877 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001878 */
1879 static char *
1880do_set_option_numeric(
1881 int opt_idx,
1882 int opt_flags,
1883 char_u **argp,
1884 int nextchar,
1885 set_op_T op,
1886 long_u flags,
1887 int cp_val,
1888 char_u *varp,
1889 char *errbuf,
1890 size_t errbuflen)
1891{
1892 char_u *arg = *argp;
1893 varnumber_T value;
1894 int i;
1895 char *errmsg = NULL;
1896
Bram Moolenaar546933f2023-02-06 16:40:49 +00001897 if (opt_idx < 0 || varp == NULL)
1898 return NULL; // "cannot happen"
1899 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001900 /*
1901 * Different ways to set a number option:
1902 * & set to default value
1903 * < set to global value
1904 * <xx> accept special key codes for 'wildchar'
1905 * c accept any non-digit for 'wildchar'
1906 * [-]0-9 set number
1907 * other error
1908 */
1909 ++arg;
1910 if (nextchar == '&')
1911 value = (long)(long_i)options[opt_idx].def_val[
1912 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
1913 else if (nextchar == '<')
1914 {
1915 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1916 // use the global value.
1917 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
1918 value = NO_LOCAL_UNDOLEVEL;
1919 else
1920 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
1921 }
1922 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
1923 && (*arg == '<'
1924 || *arg == '^'
1925 || (*arg != NUL
1926 && (!arg[1] || VIM_ISWHITE(arg[1]))
1927 && !VIM_ISDIGIT(*arg))))
1928 {
1929 value = string_to_key(arg, FALSE);
1930 if (value == 0 && (long *)varp != &p_wcm)
1931 {
1932 errmsg = e_invalid_argument;
1933 goto skip;
1934 }
1935 }
1936 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1937 {
1938 // Allow negative (for 'undolevels'), octal and hex numbers.
1939 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE);
1940 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
1941 {
1942 errmsg = e_number_required_after_equal;
1943 goto skip;
1944 }
1945 }
1946 else
1947 {
1948 errmsg = e_number_required_after_equal;
1949 goto skip;
1950 }
1951
1952 if (op == OP_ADDING)
1953 value = *(long *)varp + value;
1954 else if (op == OP_PREPENDING)
1955 value = *(long *)varp * value;
1956 else if (op == OP_REMOVING)
1957 value = *(long *)varp - value;
1958
1959 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
1960 opt_flags);
1961
1962skip:
1963 *argp = arg;
1964 return errmsg;
1965}
1966
1967/*
1968 * Set a key code (t_xx) option
1969 */
1970 static char *
1971do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
1972{
1973 char_u *arg = *argp;
1974 char_u *p;
1975
1976 if (nextchar == '&')
1977 {
1978 if (add_termcap_entry(key_name, TRUE) == FAIL)
1979 return e_not_found_in_termcap;
1980 }
1981 else
1982 {
1983 ++arg; // jump to after the '=' or ':'
1984 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
1985 if (*p == '\\' && p[1] != NUL)
1986 ++p;
1987 nextchar = *p;
1988 *p = NUL;
1989 add_termcode(key_name, arg, FALSE);
1990 *p = nextchar;
1991 }
1992 if (full_screen)
1993 ttest(FALSE);
1994 redraw_all_later(UPD_CLEAR);
1995
1996 *argp = arg;
1997 return NULL;
1998}
1999
2000/*
2001 * Set an option to a new value.
2002 */
2003 static char *
2004do_set_option_value(
2005 int opt_idx,
2006 int opt_flags,
2007 char_u **argp,
2008 set_prefix_T prefix,
2009 set_op_T op,
2010 long_u flags,
2011 char_u *varp,
2012 char_u *key_name,
2013 int nextchar,
2014 int afterchar,
2015 int cp_val,
2016 int *stopopteval,
2017 char *errbuf,
2018 size_t errbuflen)
2019{
2020 int value_checked = FALSE;
2021 char *errmsg = NULL;
2022 char_u *arg = *argp;
2023
2024 if (flags & P_BOOL)
2025 {
2026 // boolean option
2027 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2028 nextchar, afterchar, cp_val);
2029 if (errmsg != NULL)
2030 goto skip;
2031 }
2032 else
2033 {
2034 // numeric or string option
2035 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2036 || prefix != PREFIX_NONE)
2037 {
2038 errmsg = e_invalid_argument;
2039 goto skip;
2040 }
2041
2042 if (flags & P_NUM)
2043 {
2044 // numeric option
2045 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2046 op, flags, cp_val, varp,
2047 errbuf, errbuflen);
2048 if (errmsg != NULL)
2049 goto skip;
2050 }
2051 else if (opt_idx >= 0)
2052 {
2053 // string option
2054 if (do_set_string(opt_idx, opt_flags, &arg, nextchar, op, flags,
2055 cp_val, varp, errbuf, &value_checked,
2056 &errmsg) == FAIL)
2057 {
2058 if (errmsg != NULL)
2059 goto skip;
2060 *stopopteval = TRUE;
2061 goto skip;
2062 }
2063 }
2064 else
2065 {
2066 // key code option
2067 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2068 if (errmsg != NULL)
2069 goto skip;
2070 }
2071 }
2072
2073 if (opt_idx >= 0)
2074 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2075
2076skip:
2077 *argp = arg;
2078 return errmsg;
2079}
2080
2081/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002082 * Set an option to a new value.
2083 * Return NULL if OK, return an untranslated error message when something is
2084 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2085 */
2086 static char *
2087do_set_option(
2088 int opt_flags,
2089 char_u **argp,
2090 char_u *arg_start,
2091 char_u **startarg,
2092 int *did_show,
2093 int *stopopteval,
2094 char *errbuf,
2095 size_t errbuflen)
2096{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002097 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002098 char_u *arg;
2099 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2100 set_op_T op;
2101 long_u flags; // flags for current option
2102 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002103 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002104 int nextchar; // next non-white char after option name
2105 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002106 char *errmsg = NULL;
2107 int key;
2108 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002109
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002110 prefix = get_option_prefix(argp);
2111 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002112
2113 // find end of name
2114 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002115 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2116 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002117
2118 // remember character after option name
2119 afterchar = arg[len];
2120
2121 if (in_vim9script())
2122 {
2123 char_u *p = skipwhite(arg + len);
2124
2125 // disallow white space before =val, +=val, -=val, ^=val
2126 if (p > arg + len && (p[0] == '='
2127 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2128 && p[1] == '=')))
2129 {
2130 errmsg = e_no_white_space_allowed_between_option_and;
2131 arg = p;
2132 *startarg = p;
2133 goto skip;
2134 }
2135 }
2136 else
2137 // skip white space, allow ":set ai ?", ":set hlsearch !"
2138 while (VIM_ISWHITE(arg[len]))
2139 ++len;
2140
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002141 op = get_opt_op(arg + len);
2142 if (op != OP_NONE)
2143 len++;
2144
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002145 nextchar = arg[len];
2146
2147 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2148 {
2149 if (in_vim9script() && arg > arg_start
2150 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2151 errmsg = e_no_white_space_allowed_between_option_and;
2152 else
2153 errmsg = e_unknown_option;
2154 goto skip;
2155 }
2156
2157 if (opt_idx >= 0)
2158 {
2159 if (options[opt_idx].var == NULL) // hidden option: skip
2160 {
2161 // Only give an error message when requesting the value of
2162 // a hidden option, ignore setting it.
2163 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2164 && (!(options[opt_idx].flags & P_BOOL)
2165 || nextchar == '?'))
2166 errmsg = e_option_not_supported;
2167 goto skip;
2168 }
2169
2170 flags = options[opt_idx].flags;
2171 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2172 }
2173 else
2174 {
2175 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002176 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002177 if (key < 0)
2178 {
2179 key_name[0] = KEY2TERMCAP0(key);
2180 key_name[1] = KEY2TERMCAP1(key);
2181 }
2182 else
2183 {
2184 key_name[0] = KS_KEY;
2185 key_name[1] = (key & 0xff);
2186 }
2187 }
2188
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002189 // Make sure the option value can be changed.
2190 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002191 goto skip;
2192
Bram Moolenaar40b48722023-02-05 17:04:50 +00002193 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002194 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2195 {
2196 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002197 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2198 {
2199 if (arg[3] == 'm') // "opt&vim": set to Vim default
2200 {
2201 cp_val = FALSE;
2202 arg += 3;
2203 }
2204 else // "opt&vi": set to Vi default
2205 {
2206 cp_val = TRUE;
2207 arg += 2;
2208 }
2209 }
2210 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2211 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2212 {
2213 errmsg = e_trailing_characters;
2214 goto skip;
2215 }
2216 }
2217
2218 /*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002219 * Allow '=' and ':' for historical reasons (MSDOS command.com).
2220 * Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002221 */
2222 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002223 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002224 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2225 && !(flags & P_BOOL)))
2226 {
2227 /*
2228 * print value
2229 */
2230 if (*did_show)
2231 msg_putchar('\n'); // cursor below last one
2232 else
2233 {
2234 gotocmdline(TRUE); // cursor at status line
2235 *did_show = TRUE; // remember that we did a line
2236 }
2237 if (opt_idx >= 0)
2238 {
2239 showoneopt(&options[opt_idx], opt_flags);
2240#ifdef FEAT_EVAL
2241 if (p_verbose > 0)
2242 {
2243 // Mention where the option was last set.
2244 if (varp == options[opt_idx].var)
2245 last_set_msg(options[opt_idx].script_ctx);
2246 else if ((int)options[opt_idx].indir & PV_WIN)
2247 last_set_msg(curwin->w_p_script_ctx[
2248 (int)options[opt_idx].indir & PV_MASK]);
2249 else if ((int)options[opt_idx].indir & PV_BUF)
2250 last_set_msg(curbuf->b_p_script_ctx[
2251 (int)options[opt_idx].indir & PV_MASK]);
2252 }
2253#endif
2254 }
2255 else
2256 {
2257 char_u *p;
2258
2259 p = find_termcode(key_name);
2260 if (p == NULL)
2261 {
2262 errmsg = e_key_code_not_set;
2263 goto skip;
2264 }
2265 else
2266 (void)show_one_termcode(key_name, p, TRUE);
2267 }
2268 if (nextchar != '?'
2269 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2270 errmsg = e_trailing_characters;
2271 }
2272 else
2273 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002274 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2275 flags, varp, key_name, nextchar,
2276 afterchar, cp_val, stopopteval, errbuf,
2277 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002278 }
2279
2280skip:
2281 *argp = arg;
2282 return errmsg;
2283}
2284
2285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 * Parse 'arg' for option settings.
2287 *
2288 * 'arg' may be IObuff, but only when no errors can be present and option
2289 * does not need to be expanded with option_expand().
2290 * "opt_flags":
2291 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002292 * OPT_GLOBAL for ":setglobal"
2293 * OPT_LOCAL for ":setlocal" and a modeline
2294 * OPT_MODELINE for a modeline
2295 * OPT_WINONLY to only set window-local options
2296 * OPT_NOWIN to skip setting window-local options
2297 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002299 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 */
2301 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002302do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002303 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002304 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002306 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002308 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
2310 if (*arg == NUL)
2311 {
2312 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002313 did_show = TRUE;
2314 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 }
2316
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002317 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002319 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002320 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
2322 /*
2323 * ":set all" show all options.
2324 * ":set all&" set all options to their default value.
2325 */
2326 arg += 3;
2327 if (*arg == '&')
2328 {
2329 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002330 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002332 didset_options();
2333 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002334 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 }
2336 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002339 did_show = TRUE;
2340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002342 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002345 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002346 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 arg += 7;
2348 }
2349 else
2350 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002351 int stopopteval = FALSE;
2352 char *errmsg = NULL;
2353 char errbuf[80];
2354 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002356 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2357 &did_show, &stopopteval, errbuf,
2358 sizeof(errbuf));
2359 if (stopopteval)
2360 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 /*
2363 * Advance to next argument.
2364 * - skip until a blank found, taking care of backslashes
2365 * - skip blanks
2366 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2367 */
2368 for (i = 0; i < 2 ; ++i)
2369 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002370 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 if (*arg++ == '\\' && *arg != NUL)
2372 ++arg;
2373 arg = skipwhite(arg);
2374 if (*arg != '=')
2375 break;
2376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002378 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002380 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2381 i = (int)STRLEN(IObuff) + 2;
2382 if (i + (arg - startarg) < IOSIZE)
2383 {
2384 // append the argument with the error
2385 STRCAT(IObuff, ": ");
2386 mch_memmove(IObuff + i, startarg, (arg - startarg));
2387 IObuff[i + (arg - startarg)] = NUL;
2388 }
2389 // make sure all characters are printable
2390 trans_characters(IObuff, IOSIZE);
2391
2392 ++no_wait_return; // wait_return() done later
2393 emsg((char *)IObuff); // show error highlighted
2394 --no_wait_return;
2395
2396 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 }
2399
2400 arg = skipwhite(arg);
2401 }
2402
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002403theend:
2404 if (silent_mode && did_show)
2405 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002406 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002407 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002408 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002409 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002410 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002411 out_flush();
2412 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002413 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002414 }
2415
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 return OK;
2417}
2418
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002419/*
2420 * Call this when an option has been given a new value through a user command.
2421 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2422 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002423 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002424did_set_option(
2425 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002426 int opt_flags, // possibly with OPT_MODELINE
2427 int new_value, // value was replaced completely
2428 int value_checked) // value was checked to be safe, no need to set the
2429 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002430{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002431 long_u *p;
2432
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002433 options[opt_idx].flags |= P_WAS_SET;
2434
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002435 // When an option is set in the sandbox, from a modeline or in secure mode
2436 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2437 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002438 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002439 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002440#ifdef HAVE_SANDBOX
2441 || sandbox != 0
2442#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002443 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002444 *p = *p | P_INSECURE;
2445 else if (new_value)
2446 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002447}
2448
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449/*
2450 * Convert a key name or string into a key value.
2451 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002452 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002454 int
2455string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002458 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 if (*arg == '^')
2460 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002461 if (multi_byte)
2462 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 return *arg;
2464}
2465
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466/*
2467 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2468 * maketitle() to create and display it.
2469 * When switching the title or icon off, call mch_restore_title() to get
2470 * the old value back.
2471 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002472 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002473did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
2475 if (starting != NO_SCREEN
2476#ifdef FEAT_GUI
2477 && !gui.starting
2478#endif
2479 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
2483/*
2484 * set_options_bin - called when 'bin' changes value.
2485 */
2486 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002487set_options_bin(
2488 int oldval,
2489 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002490 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
2492 /*
2493 * The option values that are changed when 'bin' changes are
2494 * copied when 'bin is set and restored when 'bin' is reset.
2495 */
2496 if (newval)
2497 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002498 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 {
2500 if (!(opt_flags & OPT_GLOBAL))
2501 {
2502 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2503 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2504 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2505 curbuf->b_p_et_nobin = curbuf->b_p_et;
2506 }
2507 if (!(opt_flags & OPT_LOCAL))
2508 {
2509 p_tw_nobin = p_tw;
2510 p_wm_nobin = p_wm;
2511 p_ml_nobin = p_ml;
2512 p_et_nobin = p_et;
2513 }
2514 }
2515
2516 if (!(opt_flags & OPT_GLOBAL))
2517 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002518 curbuf->b_p_tw = 0; // no automatic line wrap
2519 curbuf->b_p_wm = 0; // no automatic line wrap
2520 curbuf->b_p_ml = 0; // no modelines
2521 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 }
2523 if (!(opt_flags & OPT_LOCAL))
2524 {
2525 p_tw = 0;
2526 p_wm = 0;
2527 p_ml = FALSE;
2528 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002529 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 }
2531 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002532 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 {
2534 if (!(opt_flags & OPT_GLOBAL))
2535 {
2536 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2537 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2538 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2539 curbuf->b_p_et = curbuf->b_p_et_nobin;
2540 }
2541 if (!(opt_flags & OPT_LOCAL))
2542 {
2543 p_tw = p_tw_nobin;
2544 p_wm = p_wm_nobin;
2545 p_ml = p_ml_nobin;
2546 p_et = p_et_nobin;
2547 }
2548 }
2549}
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551/*
2552 * Expand environment variables for some string options.
2553 * These string options cannot be indirect!
2554 * If "val" is NULL expand the current value of the option.
2555 * Return pointer to NameBuff, or NULL when not expanded.
2556 */
2557 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002558option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002560 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2562 return NULL;
2563
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002564 // If val is longer than MAXPATHL no meaningful expansion can be done,
2565 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (val != NULL && STRLEN(val) > MAXPATHL)
2567 return NULL;
2568
2569 if (val == NULL)
2570 val = *(char_u **)options[opt_idx].var;
2571
2572 /*
2573 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2574 * Escape spaces when expanding 'tags', they are used to separate file
2575 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002576 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 */
2578 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002579 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002580#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002581 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2582#endif
2583 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002584 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 return NULL;
2586
2587 return NameBuff;
2588}
2589
2590/*
2591 * After setting various option values: recompute variables that depend on
2592 * option values.
2593 */
2594 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002595didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002597 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 (void)init_chartab();
2599
Bram Moolenaardac13472019-09-16 21:06:21 +02002600 didset_string_options();
2601
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002602#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002603 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002604 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002605 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002606 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002607#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002608 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 (void)check_cedit();
Bram Moolenaar597a4222014-06-25 14:39:50 +02002610#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002611 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002612 fill_breakat_flags();
2613#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002614 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002615}
2616
2617/*
2618 * More side effects of setting options.
2619 */
2620 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002621didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002622{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002623 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002624 (void)highlight_changed();
2625
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002626 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002627 check_opt_wim();
2628
Bram Moolenaareed9d462021-02-15 20:38:25 +01002629 // Parse default for 'listchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002630 (void)set_chars_option(curwin, &curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002631
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002632 // Parse default for 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002633 (void)set_chars_option(curwin, &curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002634
2635#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002636 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002637 (void)check_clipboard_option();
2638#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002639#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002640 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002641 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002642 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002643 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645}
2646
2647/*
2648 * Check for string options that are NULL (normally only termcap options).
2649 */
2650 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002651check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652{
2653 int opt_idx;
2654
2655 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2656 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2657 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2658}
2659
2660/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002661 * Return the option index found by a pointer into term_strings[].
2662 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002664 int
2665get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002667 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668
2669 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2670 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002671 return opt_idx;
2672 return -1; // cannot happen: didn't find it!
2673}
2674
2675/*
2676 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2677 * Return the option index or -1 if not found.
2678 */
2679 int
2680set_term_option_alloced(char_u **p)
2681{
2682 int opt_idx = get_term_opt_idx(p);
2683
2684 if (opt_idx >= 0)
2685 options[opt_idx].flags |= P_ALLOCED;
2686 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687}
2688
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002689#if defined(FEAT_EVAL) || defined(PROTO)
2690/*
2691 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2692 * Return FALSE when it wasn't.
2693 * Return -1 for an unknown option.
2694 */
2695 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002696was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002697{
2698 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002699 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002700
2701 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002702 {
2703 flagp = insecure_flag(idx, opt_flags);
2704 return (*flagp & P_INSECURE) != 0;
2705 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002706 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002707 return -1;
2708}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002709
2710/*
2711 * Get a pointer to the flags used for the P_INSECURE flag of option
2712 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002713 * NOTE: Caller must make sure that "curwin" is set to the window from which
2714 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002715 */
2716 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002717insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002718{
2719 if (opt_flags & OPT_LOCAL)
2720 switch ((int)options[opt_idx].indir)
2721 {
2722#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002723 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002724#endif
2725#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002726# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002727 case PV_FDE: return &curwin->w_p_fde_flags;
2728 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002729# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002730# ifdef FEAT_BEVAL
2731 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2732# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002733 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002734 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002735# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002736 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002737# endif
2738#endif
2739 }
2740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002741 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002742 return &options[opt_idx].flags;
2743}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002744#endif
2745
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002746/*
2747 * Redraw the window title and/or tab page text later.
2748 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002749void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002750{
2751 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002752 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002753}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002754
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002756 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2757 * characters or characters in "allowed".
2758 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002759 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002760valid_name(char_u *val, char *allowed)
2761{
2762 char_u *s;
2763
2764 for (s = val; *s != NUL; ++s)
2765 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2766 return FALSE;
2767 return TRUE;
2768}
2769
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002770#if defined(FEAT_EVAL) || defined(PROTO)
2771/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002772 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002773 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002774 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002775 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002776set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002777{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002778 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2779 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002780 sctx_T new_script_ctx = script_ctx;
2781
Bram Moolenaar51258742020-05-03 17:19:33 +02002782 // Modeline already has the line number set.
2783 if (!(opt_flags & OPT_MODELINE))
2784 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002785
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002786 // Remember where the option was set. For local options need to do that
2787 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002788 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002789 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002790 if (both || (opt_flags & OPT_LOCAL))
2791 {
2792 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002793 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002794 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002795 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002796 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002797 if (both)
2798 // also setting the "all buffers" value
2799 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2800 new_script_ctx;
2801 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002802 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002803}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002804
2805/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002806 * Get the script context of global option "name".
2807 *
2808 */
2809 sctx_T *
2810get_option_sctx(char *name)
2811{
2812 int idx = findoption((char_u *)name);
2813
2814 if (idx >= 0)
2815 return &options[idx].script_ctx;
2816 siemsg("no such option: %s", name);
2817 return NULL;
2818}
2819
2820/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002821 * Set the script_ctx for a termcap option.
2822 * "name" must be the two character code, e.g. "RV".
2823 * When "name" is NULL use "opt_idx".
2824 */
2825 void
2826set_term_option_sctx_idx(char *name, int opt_idx)
2827{
2828 char_u buf[5];
2829 int idx;
2830
2831 if (name == NULL)
2832 idx = opt_idx;
2833 else
2834 {
2835 buf[0] = 't';
2836 buf[1] = '_';
2837 buf[2] = name[0];
2838 buf[3] = name[1];
2839 buf[4] = 0;
2840 idx = findoption(buf);
2841 }
2842 if (idx >= 0)
2843 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2844}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002845#endif
2846
Bram Moolenaarf4140482020-02-15 23:06:45 +01002847#if defined(FEAT_EVAL)
2848/*
2849 * Apply the OptionSet autocommand.
2850 */
2851 static void
2852apply_optionset_autocmd(
2853 int opt_idx,
2854 long opt_flags,
2855 long oldval,
2856 long oldval_g,
2857 long newval,
2858 char *errmsg)
2859{
2860 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2861
2862 // Don't do this while starting up, failure or recursively.
2863 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2864 return;
2865
2866 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2867 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2868 oldval_g);
2869 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2870 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2871 (opt_flags & OPT_LOCAL) ? "local" : "global");
2872 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2873 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2874 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2875 if (opt_flags & OPT_LOCAL)
2876 {
2877 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2878 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2879 }
2880 if (opt_flags & OPT_GLOBAL)
2881 {
2882 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2883 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2884 }
2885 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2886 {
2887 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2888 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2889 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2890 }
2891 if (opt_flags & OPT_MODELINE)
2892 {
2893 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2894 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2895 }
2896 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2897 NULL, FALSE, NULL);
2898 reset_v_option_vars();
2899}
2900#endif
2901
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902/*
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00002903 * Process the updated 'compatible' option value.
2904 */
2905 static void
2906did_set_compatible(void)
2907{
2908 compatible_set();
2909}
2910
2911#ifdef FEAT_LANGMAP
2912/*
2913 * Process the updated 'langremap' option value.
2914 */
2915 static void
2916did_set_langremap(void)
2917{
2918 // 'langremap' -> !'langnoremap'
2919 p_lnr = !p_lrm;
2920}
2921
2922/*
2923 * Process the updated 'langnoremap' option value.
2924 */
2925 static void
2926did_set_langnoremap(void)
2927{
2928 // 'langnoremap' -> !'langremap'
2929 p_lrm = !p_lnr;
2930}
2931#endif
2932
2933#ifdef FEAT_PERSISTENT_UNDO
2934/*
2935 * Process the updated 'undofile' option value.
2936 */
2937 static void
2938did_set_undofile(int opt_flags)
2939{
2940 // Only take action when the option was set. When reset we do not
2941 // delete the undo file, the option may be set again without making
2942 // any changes in between.
2943 if (curbuf->b_p_udf || p_udf)
2944 {
2945 char_u hash[UNDO_HASH_SIZE];
2946 buf_T *save_curbuf = curbuf;
2947
2948 FOR_ALL_BUFFERS(curbuf)
2949 {
2950 // When 'undofile' is set globally: for every buffer, otherwise
2951 // only for the current buffer: Try to read in the undofile,
2952 // if one exists, the buffer wasn't changed and the buffer was
2953 // loaded
2954 if ((curbuf == save_curbuf
2955 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2956 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2957 {
2958#ifdef FEAT_CRYPT
2959 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2960 continue;
2961#endif
2962 u_compute_hash(hash);
2963 u_read_undo(NULL, hash, curbuf->b_fname);
2964 }
2965 }
2966 curbuf = save_curbuf;
2967 }
2968
2969}
2970#endif
2971
2972/*
2973 * Process the updated 'readonly' option value.
2974 */
2975 static void
2976did_set_readonly(int opt_flags)
2977{
2978 // when 'readonly' is reset globally, also reset readonlymode
2979 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2980 readonlymode = FALSE;
2981
2982 // when 'readonly' is set may give W10 again
2983 if (curbuf->b_p_ro)
2984 curbuf->b_did_warn = FALSE;
2985
2986 redraw_titles();
2987}
2988
2989#ifdef FEAT_GUI
2990/*
2991 * Process the updated 'mousehide' option value.
2992 */
2993 static void
2994did_set_mousehide(void)
2995{
2996 if (!p_mh)
2997 gui_mch_mousehide(FALSE);
2998}
2999#endif
3000
3001/*
3002 * Process the updated 'modifiable' option value.
3003 */
3004 static char *
3005did_set_modifiable(int *doskip UNUSED)
3006{
3007 // when 'modifiable' is changed, redraw the window title
3008
3009# ifdef FEAT_TERMINAL
3010 // Cannot set 'modifiable' when in Terminal mode.
3011 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3012 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3013 {
3014 curbuf->b_p_ma = FALSE;
3015 *doskip = TRUE;
3016 return e_cannot_make_terminal_with_running_job_modifiable;
3017 }
3018# endif
3019 redraw_titles();
3020
3021 return NULL;
3022}
3023
3024/*
3025 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3026 * option value.
3027 */
3028 static void
3029did_set_eof_eol_fixeol_bomb(void)
3030{
3031 // redraw the window title and tab page text
3032 redraw_titles();
3033}
3034
3035/*
3036 * Process the updated 'binary' option value.
3037 */
3038 static void
3039did_set_binary(int opt_flags, long old_value)
3040{
3041 // when 'bin' is set also set some other options
3042 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
3043 redraw_titles();
3044}
3045
3046/*
3047 * Process the updated 'buflisted' option value.
3048 */
3049 static void
3050did_set_buflisted(long old_value)
3051{
3052 // when 'buflisted' changes, trigger autocommands
3053 if (old_value != curbuf->b_p_bl)
3054 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3055 NULL, NULL, TRUE, curbuf);
3056}
3057
3058/*
3059 * Process the updated 'swapfile' option value.
3060 */
3061 static void
3062did_set_swapfile(void)
3063{
3064 // when 'swf' is set, create swapfile, when reset remove swapfile
3065 if (curbuf->b_p_swf && p_uc)
3066 ml_open_file(curbuf); // create the swap file
3067 else
3068 // no need to reset curbuf->b_may_swap, ml_open_file() will check
3069 // buf->b_p_swf
3070 mf_close_file(curbuf, TRUE); // remove the swap file
3071}
3072
3073/*
3074 * Process the updated 'terse' option value.
3075 */
3076 static void
3077did_set_terse(void)
3078{
3079 char_u *p;
3080
3081 // when 'terse' is set change 'shortmess'
3082 p = vim_strchr(p_shm, SHM_SEARCH);
3083
3084 // insert 's' in p_shm
3085 if (p_terse && p == NULL)
3086 {
3087 STRCPY(IObuff, p_shm);
3088 STRCAT(IObuff, "s");
3089 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
3090 }
3091 // remove 's' from p_shm
3092 else if (!p_terse && p != NULL)
3093 STRMOVE(p, p + 1);
3094}
3095
3096/*
3097 * Process the updated 'paste' option value.
3098 */
3099 static void
3100did_set_paste(void)
3101{
3102 // when 'paste' is set or reset also change other options
3103 paste_option_changed();
3104}
3105
3106/*
3107 * Process the updated 'insertmode' option value.
3108 */
3109 static void
3110did_set_insertmode(long old_value)
3111{
3112 // when 'insertmode' is set from an autocommand need to do work here
3113 if (p_im)
3114 {
3115 if ((State & MODE_INSERT) == 0)
3116 need_start_insertmode = TRUE;
3117 stop_insert_mode = FALSE;
3118 }
3119 // only reset if it was set previously
3120 else if (old_value)
3121 {
3122 need_start_insertmode = FALSE;
3123 stop_insert_mode = TRUE;
3124 if (restart_edit != 0 && mode_displayed)
3125 clear_cmdline = TRUE; // remove "(insert)"
3126 restart_edit = 0;
3127 }
3128}
3129
3130/*
3131 * Process the updated 'ignorecase' option value.
3132 */
3133 static void
3134did_set_ignorecase(void)
3135{
3136 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3137 if (p_hls)
3138 redraw_all_later(UPD_SOME_VALID);
3139}
3140
3141#ifdef FEAT_SEARCH_EXTRA
3142/*
3143 * Process the updated 'hlsearch' option value.
3144 */
3145 static void
3146did_set_hlsearch(void)
3147{
3148 // when 'hlsearch' is set or reset: reset no_hlsearch
3149 set_no_hlsearch(FALSE);
3150}
3151#endif
3152
3153/*
3154 * Process the updated 'scrollbind' option value.
3155 */
3156 static void
3157did_set_scrollbind(void)
3158{
3159 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3160 // at the end of normal_cmd()
3161 if (curwin->w_p_scb)
3162 {
3163 do_check_scrollbind(FALSE);
3164 curwin->w_scbind_pos = curwin->w_topline;
3165 }
3166}
3167
3168#ifdef FEAT_QUICKFIX
3169/*
3170 * Process the updated 'previewwindow' option value.
3171 */
3172 static char *
3173did_set_previewwindow(int *doskip)
3174{
3175 if (!curwin->w_p_pvw)
3176 return NULL;
3177
3178 // There can be only one window with 'previewwindow' set.
3179 win_T *win;
3180
3181 FOR_ALL_WINDOWS(win)
3182 if (win->w_p_pvw && win != curwin)
3183 {
3184 curwin->w_p_pvw = FALSE;
3185 *doskip = TRUE;
3186 return e_preview_window_already_exists;
3187 }
3188
3189 return NULL;
3190}
3191#endif
3192
3193/*
3194 * Process the updated 'smoothscroll' option value.
3195 */
3196 static void
3197did_set_smoothscroll(void)
3198{
3199 if (!curwin->w_p_sms)
3200 {
3201 curwin->w_skipcol = 0;
3202 changed_line_abv_curs();
3203 }
3204}
3205
3206/*
3207 * Process the updated 'textmode' option value.
3208 */
3209 static void
3210did_set_textmode(int opt_flags)
3211{
3212 // when 'textmode' is set or reset also change 'fileformat'
3213 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
3214}
3215
3216/*
3217 * Process the updated 'textauto' option value.
3218 */
3219 static void
3220did_set_textauto(int opt_flags)
3221{
3222 // when 'textauto' is set or reset also change 'fileformats'
3223 set_string_option_direct((char_u *)"ffs", -1,
3224 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
3225 OPT_FREE | opt_flags, 0);
3226}
3227
3228/*
3229 * Process the updated 'lisp' option value.
3230 */
3231 static void
3232did_set_lisp(void)
3233{
3234 // When 'lisp' option changes include/exclude '-' in keyword characters.
3235 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3236}
3237
3238/*
3239 * Process the updated 'title' or the 'icon' option value.
3240 */
3241 static void
3242did_set_title_icon(void)
3243{
3244 // when 'title' changed, may need to change the title; same for 'icon'
3245 did_set_title();
3246}
3247
3248/*
3249 * Process the updated 'modified' option value.
3250 */
3251 static void
3252did_set_modified(long value)
3253{
3254 if (!value)
3255 save_file_ff(curbuf); // Buffer is unchanged
3256 redraw_titles();
3257 modified_was_set = value;
3258}
3259
3260#ifdef BACKSLASH_IN_FILENAME
3261/*
3262 * Process the updated 'shellslash' option value.
3263 */
3264 static void
3265did_set_shellslash(void)
3266{
3267 if (p_ssl)
3268 {
3269 psepc = '/';
3270 psepcN = '\\';
3271 pseps[0] = '/';
3272 }
3273 else
3274 {
3275 psepc = '\\';
3276 psepcN = '/';
3277 pseps[0] = '\\';
3278 }
3279
3280 // need to adjust the file name arguments and buffer names.
3281 buflist_slash_adjust();
3282 alist_slash_adjust();
3283# ifdef FEAT_EVAL
3284 scriptnames_slash_adjust();
3285# endif
3286}
3287#endif
3288
3289/*
3290 * Process the updated 'wrap' option value.
3291 */
3292 static void
3293did_set_wrap(void)
3294{
3295 // If 'wrap' is set, set w_leftcol to zero.
3296 if (curwin->w_p_wrap)
3297 curwin->w_leftcol = 0;
3298}
3299
3300/*
3301 * Process the updated 'equalalways' option value.
3302 */
3303 static void
3304did_set_equalalways(long old_value)
3305{
3306 if (p_ea && !old_value)
3307 win_equal(curwin, FALSE, 0);
3308}
3309
3310/*
3311 * Process the updated 'weirdinvert' option value.
3312 */
3313 static void
3314did_set_weirdinvert(long old_value)
3315{
3316 // When 'weirdinvert' changed, set/reset 't_xs'.
3317 // Then set 'weirdinvert' according to value of 't_xs'.
3318 if (p_wiv && !old_value)
3319 T_XS = (char_u *)"y";
3320 else if (!p_wiv && old_value)
3321 T_XS = empty_option;
3322 p_wiv = (*T_XS != NUL);
3323}
3324
3325#ifdef FEAT_BEVAL_GUI
3326/*
3327 * Process the updated 'ballooneval' option value.
3328 */
3329 static void
3330did_set_ballooneval(long old_value)
3331{
3332 if (!balloonEvalForTerm)
3333 {
3334 if (p_beval && !old_value)
3335 gui_mch_enable_beval_area(balloonEval);
3336 else if (!p_beval && old_value)
3337 gui_mch_disable_beval_area(balloonEval);
3338 }
3339
3340}
3341#endif
3342
3343#ifdef FEAT_BEVAL_TERM
3344/*
3345 * Process the updated 'balloonevalterm' option value.
3346 */
3347 static void
3348did_set_balloonevalterm(void)
3349{
3350 mch_bevalterm_changed();
3351}
3352#endif
3353
3354#ifdef FEAT_AUTOCHDIR
3355/*
3356 * Process the updated 'autochdir' option value.
3357 */
3358 static void
3359did_set_autochdir(void)
3360{
3361 // Change directories when the 'acd' option is set now.
3362 DO_AUTOCHDIR;
3363}
3364#endif
3365
3366#ifdef FEAT_DIFF
3367/*
3368 * Process the updated 'diff' option value.
3369 */
3370 static void
3371did_set_diff(void)
3372{
3373 // May add or remove the buffer from the list of diff buffers.
3374 diff_buf_adjust(curwin);
3375# ifdef FEAT_FOLDING
3376 if (foldmethodIsDiff(curwin))
3377 foldUpdateAll(curwin);
3378# endif
3379}
3380#endif
3381
3382#ifdef HAVE_INPUT_METHOD
3383/*
3384 * Process the updated 'imdisable' option value.
3385 */
3386 static void
3387did_set_imdisable(void)
3388{
3389 // Only de-activate it here, it will be enabled when changing mode.
3390 if (p_imdisable)
3391 im_set_active(FALSE);
3392 else if (State & MODE_INSERT)
3393 // When the option is set from an autocommand, it may need to take
3394 // effect right away.
3395 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3396}
3397#endif
3398
3399#ifdef FEAT_SPELL
3400/*
3401 * Process the updated 'spell' option value.
3402 */
3403 static char *
3404did_set_spell(void)
3405{
3406 if (curwin->w_p_spell)
3407 return did_set_spelllang(curwin);
3408
3409 return NULL;
3410}
3411#endif
3412
3413#ifdef FEAT_ARABIC
3414/*
3415 * Process the updated 'arabic' option value.
3416 */
3417 static char *
3418did_set_arabic(void)
3419{
3420 char *errmsg = NULL;
3421
3422 if (curwin->w_p_arab)
3423 {
3424 /*
3425 * 'arabic' is set, handle various sub-settings.
3426 */
3427 if (!p_tbidi)
3428 {
3429 // set rightleft mode
3430 if (!curwin->w_p_rl)
3431 {
3432 curwin->w_p_rl = TRUE;
3433 changed_window_setting();
3434 }
3435
3436 // Enable Arabic shaping (major part of what Arabic requires)
3437 if (!p_arshape)
3438 {
3439 p_arshape = TRUE;
3440 redraw_later_clear();
3441 }
3442 }
3443
3444 // Arabic requires a utf-8 encoding, inform the user if it's not
3445 // set.
3446 if (STRCMP(p_enc, "utf-8") != 0)
3447 {
3448 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3449
3450 msg_source(HL_ATTR(HLF_W));
3451 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3452#ifdef FEAT_EVAL
3453 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3454#endif
3455 }
3456
3457 // set 'delcombine'
3458 p_deco = TRUE;
3459
3460# ifdef FEAT_KEYMAP
3461 // Force-set the necessary keymap for arabic
3462 errmsg = set_option_value((char_u *)"keymap",
3463 0L, (char_u *)"arabic", OPT_LOCAL);
3464# endif
3465 }
3466 else
3467 {
3468 /*
3469 * 'arabic' is reset, handle various sub-settings.
3470 */
3471 if (!p_tbidi)
3472 {
3473 // reset rightleft mode
3474 if (curwin->w_p_rl)
3475 {
3476 curwin->w_p_rl = FALSE;
3477 changed_window_setting();
3478 }
3479
3480 // 'arabicshape' isn't reset, it is a global option and
3481 // another window may still need it "on".
3482 }
3483
3484 // 'delcombine' isn't reset, it is a global option and another
3485 // window may still want it "on".
3486
3487# ifdef FEAT_KEYMAP
3488 // Revert to the default keymap
3489 curbuf->b_p_iminsert = B_IMODE_NONE;
3490 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3491# endif
3492 }
3493
3494 return errmsg;
3495}
3496#endif
3497
3498#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3499/*
3500 * Process the updated 'number' or 'relativenumber' option value.
3501 */
3502 static void
3503did_set_number_relativenumber(char_u *varp)
3504{
3505 if (gui.in_use
3506 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3507 && curbuf->b_signlist != NULL)
3508 {
3509 // If the 'number' or 'relativenumber' options are modified and
3510 // 'signcolumn' is set to 'number', then clear the screen for a full
3511 // refresh. Otherwise the sign icons are not displayed properly in the
3512 // number column. If the 'number' option is set and only the
3513 // 'relativenumber' option is toggled, then don't refresh the screen
3514 // (optimization).
3515 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3516 redraw_all_later(UPD_CLEAR);
3517 }
3518}
3519#endif
3520
3521#ifdef FEAT_TERMGUICOLORS
3522 static char *
3523did_set_termguicolors(int *doskip UNUSED)
3524{
3525# ifdef FEAT_VTP
3526 // Do not turn on 'tgc' when 24-bit colors are not supported.
3527 if (
3528# ifdef VIMDLL
3529 !gui.in_use && !gui.starting &&
3530# endif
3531 !has_vtp_working())
3532 {
3533 p_tgc = 0;
3534 *doskip = TRUE;
3535 return e_24_bit_colors_are_not_supported_on_this_environment;
3536 }
3537 if (is_term_win32())
3538 swap_tcap();
3539# endif
3540# ifdef FEAT_GUI
3541 if (!gui.in_use && !gui.starting)
3542# endif
3543 highlight_gui_started();
3544# ifdef FEAT_VTP
3545 // reset t_Co
3546 if (is_term_win32())
3547 {
3548 control_console_color_rgb();
3549 set_termname(T_NAME);
3550 init_highlight(TRUE, FALSE);
3551 }
3552# endif
3553# ifdef FEAT_TERMINAL
3554 term_update_colors_all();
3555 term_update_palette_all();
3556 term_update_wincolor_all();
3557# endif
3558
3559 return NULL;
3560}
3561#endif
3562
3563/*
3564 * When some boolean options are changed, need to take some action.
3565 */
3566 static char *
3567did_set_bool_option(
3568 char_u *varp,
3569 int opt_flags,
3570 long value,
3571 long old_value,
3572 int *doskip)
3573{
3574 char *errmsg = NULL;
3575
3576 if ((int *)varp == &p_cp) // 'compatible'
3577 did_set_compatible();
3578#ifdef FEAT_LANGMAP
3579 else if ((int *)varp == &p_lrm) // 'langremap'
3580 did_set_langremap();
3581 else if ((int *)varp == &p_lnr) // 'langnoremap'
3582 did_set_langnoremap();
3583#endif
3584#ifdef FEAT_PERSISTENT_UNDO
3585 else if ((int *)varp == &curbuf->b_p_udf // buffer local 'undofile'
3586 || (int *)varp == &p_udf) // 'undofile'
3587 did_set_undofile(opt_flags);
3588#endif
3589 else if ((int *)varp == &curbuf->b_p_ro) // 'readonly'
3590 did_set_readonly(opt_flags);
3591#ifdef FEAT_GUI
3592 else if ((int *)varp == &p_mh) // 'mousehide'
3593 did_set_mousehide();
3594#endif
3595 else if ((int *)varp == &curbuf->b_p_ma)
3596 errmsg = did_set_modifiable(doskip); // 'modifiable'
3597 else if ((int *)varp == &curbuf->b_p_eof // 'endoffile'
3598 || (int *)varp == &curbuf->b_p_eol // 'endofline'
3599 || (int *)varp == &curbuf->b_p_fixeol // 'fixendofline'
3600 || (int *)varp == &curbuf->b_p_bomb) // 'bomb'
3601 did_set_eof_eol_fixeol_bomb();
3602 else if ((int *)varp == &curbuf->b_p_bin) // 'binary'
3603 did_set_binary(opt_flags, old_value);
3604 else if ((int *)varp == &curbuf->b_p_bl) // 'buflisted'
3605 did_set_buflisted(old_value);
3606 else if ((int *)varp == &curbuf->b_p_swf) // 'swapfile'
3607 did_set_swapfile();
3608 else if ((int *)varp == &p_terse) // 'terse'
3609 did_set_terse();
3610 else if ((int *)varp == &p_paste) // 'paste'
3611 did_set_paste();
3612 else if ((int *)varp == &p_im) // 'insertmode'
3613 did_set_insertmode(old_value);
3614 else if ((int *)varp == &p_ic) // 'ignorecase'
3615 did_set_ignorecase();
3616#ifdef FEAT_SEARCH_EXTRA
3617 else if ((int *)varp == &p_hls) // 'hlsearch'
3618 did_set_hlsearch();
3619#endif
3620 else if ((int *)varp == &curwin->w_p_scb) // 'scrollbind'
3621 did_set_scrollbind();
3622#ifdef FEAT_QUICKFIX
3623 else if ((int *)varp == &curwin->w_p_pvw) // 'previewwindow'
3624 errmsg = did_set_previewwindow(doskip);
3625#endif
3626 else if ((int *)varp == &curwin->w_p_sms) // 'smoothscroll'
3627 did_set_smoothscroll();
3628 else if ((int *)varp == &curbuf->b_p_tx) // 'textmode'
3629 did_set_textmode(opt_flags);
3630 else if ((int *)varp == &p_ta) // 'textauto'
3631 did_set_textauto(opt_flags);
3632 else if (varp == (char_u *)&(curbuf->b_p_lisp)) // 'lisp'
3633 did_set_lisp();
3634 else if ( (int *)varp == &p_title // 'title'
3635 || (int *)varp == &p_icon) // 'icon'
3636 did_set_title_icon();
3637 else if ((int *)varp == &curbuf->b_changed) // 'modified'
3638 did_set_modified(value);
3639#ifdef BACKSLASH_IN_FILENAME
3640 else if ((int *)varp == &p_ssl) // 'shellslash'
3641 did_set_shellslash();
3642#endif
3643 else if ((int *)varp == &curwin->w_p_wrap) // 'wrap'
3644 did_set_wrap();
3645 else if ((int *)varp == &p_ea) // 'equalalways'
3646 did_set_equalalways(old_value);
3647 else if ((int *)varp == &p_wiv) // weirdinvert'
3648 did_set_weirdinvert(old_value);
3649#ifdef FEAT_BEVAL_GUI
3650 else if ((int *)varp == &p_beval) // 'ballooneval'
3651 did_set_ballooneval(old_value);
3652#endif
3653#ifdef FEAT_BEVAL_TERM
3654 else if ((int *)varp == &p_bevalterm) // 'balloonevalterm'
3655 did_set_balloonevalterm();
3656#endif
3657#ifdef FEAT_AUTOCHDIR
3658 else if ((int *)varp == &p_acd) // 'autochdir'
3659 did_set_autochdir();
3660#endif
3661#ifdef FEAT_DIFF
3662 else if ((int *)varp == &curwin->w_p_diff) // 'diff'
3663 did_set_diff();
3664#endif
3665#ifdef HAVE_INPUT_METHOD
3666 else if ((int *)varp == &p_imdisable) // 'imdisable'
3667 did_set_imdisable();
3668#endif
3669#ifdef FEAT_SPELL
3670 else if ((int *)varp == &curwin->w_p_spell) // 'spell'
3671 errmsg = did_set_spell();
3672#endif
3673#ifdef FEAT_ARABIC
3674 else if ((int *)varp == &curwin->w_p_arab) // 'arabic'
3675 errmsg = did_set_arabic();
3676#endif
3677#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3678 else if ( (int *)varp == &curwin->w_p_nu // 'number'
3679 || (int *)varp == &curwin->w_p_rnu) // 'relativenumber'
3680 did_set_number_relativenumber(varp);
3681#endif
3682#ifdef FEAT_TERMGUICOLORS
3683 else if ((int *)varp == &p_tgc) // 'termguicolors'
3684 errmsg = did_set_termguicolors(doskip);
3685#endif
3686
3687 return errmsg;
3688}
3689
3690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 * Set the value of a boolean option, and take care of side effects.
3692 * Returns NULL for success, or an error message for an error.
3693 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003694 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003695set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003696 int opt_idx, // index in options[] table
3697 char_u *varp, // pointer to the option variable
3698 int value, // new value
3699 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700{
3701 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003702#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003703 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003704#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003705 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003707 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 if ((secure
3709#ifdef HAVE_SANDBOX
3710 || sandbox != 0
3711#endif
3712 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003713 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003715#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003716 // Save the global value before changing anything. This is needed as for
3717 // a global-only option setting the "local value" in fact sets the global
3718 // value (since there is only one value).
3719 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3720 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
3721 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003722#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003723
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003724 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003726 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003727 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728#endif
3729
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003730#ifdef FEAT_GUI
3731 need_mouse_correct = TRUE;
3732#endif
3733
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003734 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3736 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
3737
3738 /*
3739 * Handle side effects of changing a bool option.
3740 */
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003741 int doskip = FALSE;
3742 errmsg = did_set_bool_option(varp, opt_flags, value, old_value, &doskip);
3743 if (doskip)
3744 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003746 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003747
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 options[opt_idx].flags |= P_WAS_SET;
3749
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003750#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003751 apply_optionset_autocmd(opt_idx, opt_flags,
3752 (long)(old_value ? TRUE : FALSE),
3753 (long)(old_global_value ? TRUE : FALSE),
3754 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003755#endif
3756
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003757 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003758 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003759 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003760 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003761
3762 if ((opt_flags & OPT_NO_REDRAW) == 0)
3763 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003765 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766}
3767
3768/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003769 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003771 static char *
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003772did_set_winheight_helpheight(long *pp, char *errmsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773{
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003774 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003776 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003777 p_wh = 1;
3778 }
3779 if (p_wmh > p_wh)
3780 {
3781 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
3782 p_wh = p_wmh;
3783 }
3784 if (p_hh < 0)
3785 {
3786 errmsg = e_argument_must_be_positive;
3787 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003790 // Change window height NOW
3791 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003793 if (pp == &p_wh && curwin->w_height < p_wh)
3794 win_setheight((int)p_wh);
3795 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3796 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003799 return errmsg;
3800}
3801
3802/*
3803 * Process the new 'winminheight' option value.
3804 */
3805 static char *
3806did_set_winminheight(char *errmsg)
3807{
3808 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003810 errmsg = e_argument_must_be_positive;
3811 p_wmh = 0;
3812 }
3813 if (p_wmh > p_wh)
3814 {
3815 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
3816 p_wmh = p_wh;
3817 }
3818 win_setminheight();
3819
3820 return errmsg;
3821}
3822
3823/*
3824 * Process the new 'winwidth' option value.
3825 */
3826 static char *
3827did_set_winwidth(char *errmsg)
3828{
3829 if (p_wiw < 1)
3830 {
3831 errmsg = e_argument_must_be_positive;
3832 p_wiw = 1;
3833 }
3834 if (p_wmw > p_wiw)
3835 {
3836 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
3837 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003839
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003840 // Change window width NOW
3841 if (!ONE_WINDOW && curwin->w_width < p_wiw)
3842 win_setwidth((int)p_wiw);
3843
3844 return errmsg;
3845}
3846
3847/*
3848 * Process the new 'winminwidth' option value.
3849 */
3850 static char *
3851did_set_winminwidth(char *errmsg)
3852{
3853 if (p_wmw < 0)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003854 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003855 errmsg = e_argument_must_be_positive;
3856 p_wmw = 0;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003857 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003858 if (p_wmw > p_wiw)
3859 {
3860 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
3861 p_wmw = p_wiw;
3862 }
3863 win_setminwidth();
3864
3865 return errmsg;
3866}
3867
3868/*
3869 * Process the new 'laststatus' option value.
3870 */
3871 static void
3872did_set_laststatus(void)
3873{
3874 last_status(FALSE); // (re)set last window status line
3875}
3876
3877/*
3878 * Process the new 'showtabline' option value.
3879 */
3880 static void
3881did_set_showtabline(void)
3882{
3883 shell_new_rows(); // recompute window positions and heights
3884}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885
3886#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003887/*
3888 * Process the new 'linespace' option value.
3889 */
3890 static void
3891did_set_linespace(void)
3892{
3893 // Recompute gui.char_height and resize the Vim window to keep the
3894 // same number of lines.
3895 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3896 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3897}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898#endif
3899
3900#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003901/*
3902 * Process the new 'foldlevel' option value.
3903 */
3904 static void
3905did_set_foldlevel(void)
3906{
3907 if (curwin->w_p_fdl < 0)
3908 curwin->w_p_fdl = 0;
3909 newFoldLevel();
3910}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003912/*
3913 * Process the new 'foldminlines' option value.
3914 */
3915 static void
3916did_set_foldminlines(void)
3917{
3918 foldUpdateAll(curwin);
3919}
3920
3921/*
3922 * Process the new 'foldnestmax' option value.
3923 */
3924 static void
3925did_set_foldnestmax(void)
3926{
3927 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 foldUpdateAll(curwin);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003929}
3930
3931/*
3932 * Process the new 'foldcolumn' option value.
3933 */
3934 static char *
3935did_set_foldcolumn(char *errmsg)
3936{
3937 if (curwin->w_p_fdc < 0)
3938 {
3939 errmsg = e_argument_must_be_positive;
3940 curwin->w_p_fdc = 0;
3941 }
3942 else if (curwin->w_p_fdc > 12)
3943 {
3944 errmsg = e_invalid_argument;
3945 curwin->w_p_fdc = 12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003948 return errmsg;
3949}
3950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003952/*
3953 * Process the new 'shiftwidth' or the 'tabstop' option value.
3954 */
3955 static void
3956did_set_shiftwidth_tabstop(long *pp)
3957{
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003958#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003959 if (foldmethodIsIndent(curwin))
3960 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003961#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003962 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3963 // parse 'cinoptions'.
3964 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3965 parse_cino(curbuf);
3966}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003968/*
3969 * Process the new 'maxcombine' option value.
3970 */
3971 static void
3972did_set_maxcombine(void)
3973{
3974 if (p_mco > MAX_MCO)
3975 p_mco = MAX_MCO;
3976 else if (p_mco < 0)
3977 p_mco = 0;
3978 screenclear(); // will re-allocate the screen
3979}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003980
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003981/*
3982 * Process the new 'iminsert' option value.
3983 */
3984 static char *
3985did_set_iminsert(char *errmsg)
3986{
3987 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003989 errmsg = e_invalid_argument;
3990 curbuf->b_p_iminsert = B_IMODE_NONE;
3991 }
3992 p_iminsert = curbuf->b_p_iminsert;
3993 if (termcap_active) // don't do this in the alternate screen
3994 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003995#if defined(FEAT_KEYMAP)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003996 // Show/unshow value of 'keymap' in status lines.
3997 status_redraw_curbuf();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003999
4000 return errmsg;
4001}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004003#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004004/*
4005 * Process the new 'imstyle' option value.
4006 */
4007 static char *
4008did_set_imstyle(char *errmsg)
4009{
4010 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
4011 errmsg = e_invalid_argument;
4012
4013 return errmsg;
4014}
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004015#endif
4016
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004017/*
4018 * Process the new 'window' option value.
4019 */
4020 static void
4021did_set_window(void)
4022{
4023 if (p_window < 1)
4024 p_window = 1;
4025 else if (p_window >= Rows)
4026 p_window = Rows - 1;
4027}
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004028
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004029/*
4030 * Process the new 'imsearch' option value.
4031 */
4032 static char *
4033did_set_imsearch(char *errmsg)
4034{
4035 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004037 errmsg = e_invalid_argument;
4038 curbuf->b_p_imsearch = B_IMODE_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004040 p_imsearch = curbuf->b_p_imsearch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004042 return errmsg;
4043}
4044
4045/*
4046 * Process the new 'titlelen' option value.
4047 */
4048 static char *
4049did_set_titlelen(long old_value, char *errmsg)
4050{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004051 // if 'titlelen' has changed, redraw the title
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004052 if (p_titlelen < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004054 errmsg = e_argument_must_be_positive;
4055 p_titlelen = 85;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004057 if (starting != NO_SCREEN && old_value != p_titlelen)
4058 need_maketitle = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004060 return errmsg;
4061}
4062
4063/*
4064 * Process the new 'cmdheight' option value.
4065 */
4066 static char *
4067did_set_cmdheight(long old_value, char *errmsg)
4068{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004069 // if p_ch changed value, change the command line height
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004070 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004072 errmsg = e_argument_must_be_positive;
4073 p_ch = 1;
4074 }
4075 if (p_ch > Rows - min_rows() + 1)
4076 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004078 // Only compute the new window layout when startup has been
4079 // completed. Otherwise the frame sizes may be wrong.
4080 if ((p_ch != old_value
4081 || tabline_height() + topframe->fr_height != Rows - p_ch)
4082 && full_screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004084 && !gui.starting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004086 )
4087 command_height();
4088
4089 return errmsg;
4090}
4091
4092/*
4093 * Process the new 'updatecount' option value.
4094 */
4095 static char *
4096did_set_updatecount(long old_value, char *errmsg)
4097{
4098 // when 'updatecount' changes from zero to non-zero, open swap files
4099 if (p_uc < 0)
4100 {
4101 errmsg = e_argument_must_be_positive;
4102 p_uc = 100;
4103 }
4104 if (p_uc && !old_value)
4105 ml_open_files();
4106
4107 return errmsg;
4108}
4109
4110#ifdef FEAT_CONCEAL
4111/*
4112 * Process the new 'conceallevel' option value.
4113 */
4114 static char *
4115did_set_conceallevel(char *errmsg)
4116{
4117 if (curwin->w_p_cole < 0)
4118 {
4119 errmsg = e_argument_must_be_positive;
4120 curwin->w_p_cole = 0;
4121 }
4122 else if (curwin->w_p_cole > 3)
4123 {
4124 errmsg = e_invalid_argument;
4125 curwin->w_p_cole = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
4127
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004128 return errmsg;
4129}
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004132#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004133/*
4134 * Process the new 'pyxversion' option value.
4135 */
4136 static char *
4137did_set_pyxversion(char *errmsg)
4138{
4139 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4140 errmsg = e_invalid_argument;
4141
4142 return errmsg;
4143}
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004144#endif
4145
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004146/*
4147 * Process the new global 'undolevels' option value.
4148 */
4149 static void
4150did_set_global_undolevels(long value, long old_value)
4151{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004152 // sync undo before 'undolevels' changes
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004153
4154 // use the old value, otherwise u_sync() may not work properly
4155 p_ul = old_value;
4156 u_sync(TRUE);
4157 p_ul = value;
4158}
4159
4160/*
4161 * Process the new buffer local 'undolevels' option value.
4162 */
4163 static void
4164did_set_buflocal_undolevels(long value, long old_value)
4165{
4166 // use the old value, otherwise u_sync() may not work properly
4167 curbuf->b_p_ul = old_value;
4168 u_sync(TRUE);
4169 curbuf->b_p_ul = value;
4170}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004172#ifdef FEAT_LINEBREAK
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004173/*
4174 * Process the new 'numberwidth' option value.
4175 */
4176 static char *
4177did_set_numberwidth(char *errmsg)
4178{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004179 // 'numberwidth' must be positive
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004180 if (curwin->w_p_nuw < 1)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004181 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004182 errmsg = e_argument_must_be_positive;
4183 curwin->w_p_nuw = 1;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004184 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004185 if (curwin->w_p_nuw > 20)
4186 {
4187 errmsg = e_invalid_argument;
4188 curwin->w_p_nuw = 20;
4189 }
4190 curwin->w_nrwidth_line_count = 0; // trigger a redraw
4191
4192 return errmsg;
4193}
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004194#endif
4195
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004196/*
4197 * Process the new 'textwidth' option value.
4198 */
4199 static char *
4200did_set_textwidth(char *errmsg)
4201{
4202 if (curbuf->b_p_tw < 0)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004203 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004204 errmsg = e_argument_must_be_positive;
4205 curbuf->b_p_tw = 0;
4206 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02004207#ifdef FEAT_SYN_HL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004208 {
4209 win_T *wp;
4210 tabpage_T *tp;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004211
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004212 FOR_ALL_TAB_WINDOWS(tp, wp)
4213 check_colorcolumn(wp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004214 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004215#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004216
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004217 return errmsg;
4218}
4219
4220/*
4221 * When some number options are changed, need to take some action.
4222 */
4223 static char *
4224did_set_num_option(long *pp, long value, long old_value, char *errmsg)
4225{
4226 if (pp == &p_wh // 'winheight'
4227 || pp == &p_hh) // 'helpheight'
4228 errmsg = did_set_winheight_helpheight(pp, errmsg);
4229 else if (pp == &p_wmh) // 'winminheight'
4230 errmsg = did_set_winminheight(errmsg);
4231 else if (pp == &p_wiw) // 'winwidth'
4232 errmsg = did_set_winwidth(errmsg);
4233 else if (pp == &p_wmw) // 'winminwidth'
4234 errmsg = did_set_winminwidth(errmsg);
4235 else if (pp == &p_ls)
4236 did_set_laststatus(); // 'laststatus'
4237 else if (pp == &p_stal)
4238 did_set_showtabline(); // 'showtabline'
4239#ifdef FEAT_GUI
4240 else if (pp == &p_linespace) // 'linespace'
4241 did_set_linespace();
4242#endif
4243#ifdef FEAT_FOLDING
4244 else if (pp == &curwin->w_p_fdl) // 'foldlevel'
4245 did_set_foldlevel();
4246 else if (pp == &curwin->w_p_fml) // 'foldminlines'
4247 did_set_foldminlines();
4248 else if (pp == &curwin->w_p_fdn) // 'foldnestmax'
4249 did_set_foldnestmax();
4250 else if (pp == &curwin->w_p_fdc) // 'foldcolumn'
4251 errmsg = did_set_foldcolumn(errmsg);
4252#endif // FEAT_FOLDING
4253 else if ( pp == &curbuf->b_p_sw // 'shiftwidth'
4254 || pp == &curbuf->b_p_ts) // 'tabstop'
4255 did_set_shiftwidth_tabstop(pp);
4256 else if (pp == &p_mco) // 'maxcombine'
4257 did_set_maxcombine();
4258 else if (pp == &curbuf->b_p_iminsert) // 'iminsert'
4259 errmsg = did_set_iminsert(errmsg);
4260#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4261 else if (pp == &p_imst) // 'imstyle'
4262 errmsg = did_set_imstyle(errmsg);
4263#endif
4264 else if (pp == &p_window) // 'window'
4265 did_set_window();
4266 else if (pp == &curbuf->b_p_imsearch) // 'imsearch'
4267 errmsg = did_set_imsearch(errmsg);
4268 else if (pp == &p_titlelen) // 'titlelen'
4269 errmsg = did_set_titlelen(old_value, errmsg);
4270 else if (pp == &p_ch) // 'cmdheight'
4271 errmsg = did_set_cmdheight(old_value, errmsg);
4272 else if (pp == &p_uc) // 'updatecount'
4273 errmsg = did_set_updatecount(old_value, errmsg);
4274#ifdef FEAT_CONCEAL
4275 else if (pp == &curwin->w_p_cole) // 'conceallevel'
4276 errmsg = did_set_conceallevel(errmsg);
4277#endif
4278#ifdef MZSCHEME_GUI_THREADS
4279 else if (pp == &p_mzq) // 'mzquantum'
4280 mzvim_reset_timer();
4281#endif
4282#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
4283 else if (pp == &p_pyx) // 'pyxversion'
4284 errmsg = did_set_pyxversion(errmsg);
4285#endif
4286 else if (pp == &p_ul) // global 'undolevels'
4287 did_set_global_undolevels(value, old_value);
4288 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4289 did_set_buflocal_undolevels(value, old_value);
4290#ifdef FEAT_LINEBREAK
4291 else if (pp == &curwin->w_p_nuw) // 'numberwidth'
4292 errmsg = did_set_numberwidth(errmsg);
4293#endif
4294 else if (pp == &curbuf->b_p_tw) // 'textwidth'
4295 errmsg = did_set_textwidth(errmsg);
4296
4297 return errmsg;
4298}
4299
4300/*
4301 * Check the bounds of numeric options.
4302 */
4303 static char *
4304check_num_option_bounds(
4305 long *pp,
4306 long old_value,
4307 long old_Rows,
4308 long old_Columns,
4309 char *errbuf,
4310 size_t errbuflen,
4311 char *errmsg)
4312{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 if (Rows < min_rows() && full_screen)
4314 {
4315 if (errbuf != NULL)
4316 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004317 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004318 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 errmsg = errbuf;
4320 }
4321 Rows = min_rows();
4322 }
4323 if (Columns < MIN_COLUMNS && full_screen)
4324 {
4325 if (errbuf != NULL)
4326 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004327 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004328 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 errmsg = errbuf;
4330 }
4331 Columns = MIN_COLUMNS;
4332 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004333 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 /*
4336 * If the screen (shell) height has been changed, assume it is the
4337 * physical screenheight.
4338 */
4339 if (old_Rows != Rows || old_Columns != Columns)
4340 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004341 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 if (updating_screen)
4343 *pp = old_value;
4344 else if (full_screen
4345#ifdef FEAT_GUI
4346 && !gui.starting
4347#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004348 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 set_shellsize((int)Columns, (int)Rows, TRUE);
4350 else
4351 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004352 // Postpone the resizing; check the size and cmdline position for
4353 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 check_shellsize();
4355 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4356 cmdline_row = Rows - p_ch;
4357 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004358 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004359 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 if (curbuf->b_p_ts <= 0)
4363 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004364 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 curbuf->b_p_ts = 8;
4366 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004367 else if (curbuf->b_p_ts > TABSTOP_MAX)
4368 {
4369 errmsg = e_invalid_argument;
4370 curbuf->b_p_ts = 8;
4371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 if (p_tm < 0)
4373 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004374 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 p_tm = 0;
4376 }
4377 if ((curwin->w_p_scr <= 0
4378 || (curwin->w_p_scr > curwin->w_height
4379 && curwin->w_height > 0))
4380 && full_screen)
4381 {
4382 if (pp == &(curwin->w_p_scr))
4383 {
4384 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004385 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 win_comp_scroll(curwin);
4387 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004388 // If 'scroll' became invalid because of a side effect silently adjust
4389 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 else if (curwin->w_p_scr <= 0)
4391 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004392 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 curwin->w_p_scr = curwin->w_height;
4394 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004395 if (p_hi < 0)
4396 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004397 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004398 p_hi = 0;
4399 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004400 else if (p_hi > 10000)
4401 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004402 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004403 p_hi = 10000;
4404 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004405 if (p_re < 0 || p_re > 2)
4406 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004407 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004408 p_re = 0;
4409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 if (p_report < 0)
4411 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004412 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 p_report = 1;
4414 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004415 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004417 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 p_sj = Rows / 2;
4419 else
4420 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004421 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 p_sj = 1;
4423 }
4424 }
4425 if (p_so < 0 && full_screen)
4426 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004427 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 p_so = 0;
4429 }
4430 if (p_siso < 0 && full_screen)
4431 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004432 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 p_siso = 0;
4434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 if (p_cwh < 1)
4436 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004437 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 p_cwh = 1;
4439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 if (p_ut < 0)
4441 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004442 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 p_ut = 2000;
4444 }
4445 if (p_ss < 0)
4446 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004447 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 p_ss = 0;
4449 }
4450
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004451 return errmsg;
4452}
4453
4454/*
4455 * Set the value of a number option, and take care of side effects.
4456 * Returns NULL for success, or an error message for an error.
4457 */
4458 static char *
4459set_num_option(
4460 int opt_idx, // index in options[] table
4461 char_u *varp, // pointer to the option variable
4462 long value, // new value
4463 char *errbuf, // buffer for error messages
4464 size_t errbuflen, // length of "errbuf"
4465 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4466 // OPT_MODELINE, etc.
4467{
4468 char *errmsg = NULL;
4469 long old_value = *(long *)varp;
4470#if defined(FEAT_EVAL)
4471 long old_global_value = 0; // only used when setting a local and
4472 // global option
4473#endif
4474 long old_Rows = Rows; // remember old Rows
4475 long old_Columns = Columns; // remember old Columns
4476 long *pp = (long *)varp;
4477
4478 // Disallow changing some options from secure mode.
4479 if ((secure
4480#ifdef HAVE_SANDBOX
4481 || sandbox != 0
4482#endif
4483 ) && (options[opt_idx].flags & P_SECURE))
4484 return e_not_allowed_here;
4485
4486#if defined(FEAT_EVAL)
4487 // Save the global value before changing anything. This is needed as for
4488 // a global-only option setting the "local value" in fact sets the global
4489 // value (since there is only one value).
4490 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4491 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4492 OPT_GLOBAL);
4493#endif
4494
4495 *pp = value;
4496#ifdef FEAT_EVAL
4497 // Remember where the option was set.
4498 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4499#endif
4500#ifdef FEAT_GUI
4501 need_mouse_correct = TRUE;
4502#endif
4503
4504 if (curbuf->b_p_sw < 0)
4505 {
4506 errmsg = e_argument_must_be_positive;
4507#ifdef FEAT_VARTABS
4508 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4509 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4510 ? tabstop_first(curbuf->b_p_vts_array)
4511 : curbuf->b_p_ts;
4512#else
4513 curbuf->b_p_sw = curbuf->b_p_ts;
4514#endif
4515 }
4516
4517 /*
4518 * Number options that need some action when changed
4519 */
4520 errmsg = did_set_num_option(pp, value, old_value, errmsg);
4521
4522 /*
4523 * Check the bounds for numeric options here
4524 */
4525 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4526 errbuf, errbuflen, errmsg);
4527
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004528 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4530 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4531
4532 options[opt_idx].flags |= P_WAS_SET;
4533
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004534#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004535 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4536 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004537#endif
4538
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004539 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004540 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004541 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004542 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004543 if ((opt_flags & OPT_NO_REDRAW) == 0)
4544 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545
4546 return errmsg;
4547}
4548
4549/*
4550 * Called after an option changed: check if something needs to be redrawn.
4551 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004552 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004553check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004555 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004556 int doclear = (flags & P_RCLR) == P_RCLR;
4557 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004559 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
4562 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4563 changed_window_setting();
4564 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004565 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004566 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004567 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004568 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004569 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004571 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572}
4573
4574/*
4575 * Find index for option 'arg'.
4576 * Return -1 if not found.
4577 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004578 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004579findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580{
4581 int opt_idx;
4582 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004583 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 int is_term_opt;
4585
4586 /*
4587 * For first call: Initialize the quick-access table.
4588 * It contains the index for the first option that starts with a certain
4589 * letter. There are 26 letters, plus the first "t_" option.
4590 */
4591 if (quick_tab[1] == 0)
4592 {
4593 p = options[0].fullname;
4594 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4595 {
4596 if (s[0] != p[0])
4597 {
4598 if (s[0] == 't' && s[1] == '_')
4599 quick_tab[26] = opt_idx;
4600 else
4601 quick_tab[CharOrdLow(s[0])] = opt_idx;
4602 }
4603 p = s;
4604 }
4605 }
4606
4607 /*
4608 * Check for name starting with an illegal character.
4609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 return -1;
4612
4613 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4614 if (is_term_opt)
4615 opt_idx = quick_tab[26];
4616 else
4617 opt_idx = quick_tab[CharOrdLow(arg[0])];
4618 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4619 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004620 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 break;
4622 }
4623 if (s == NULL && !is_term_opt)
4624 {
4625 opt_idx = quick_tab[CharOrdLow(arg[0])];
4626 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4627 {
4628 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004629 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 break;
4631 s = NULL;
4632 }
4633 }
4634 if (s == NULL)
4635 opt_idx = -1;
4636 return opt_idx;
4637}
4638
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004639#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4640 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641/*
4642 * Get the value for an option.
4643 *
4644 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004645 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004646 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004647 * String option: gov_string, *stringval gets allocated string.
4648 * Hidden Number option: gov_hidden_number.
4649 * Hidden Toggle option: gov_hidden_bool.
4650 * Hidden String option: gov_hidden_string.
4651 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004652 *
4653 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004655 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004656get_option_value(
4657 char_u *name,
4658 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004659 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004660 int *flagsp,
4661 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662{
4663 int opt_idx;
4664 char_u *varp;
4665
4666 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004667 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004668 {
4669 int key;
4670
4671 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004672 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004673 {
4674 char_u key_name[2];
4675 char_u *p;
4676
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004677 if (flagsp != NULL)
4678 *flagsp = 0; // terminal option has no flags
4679
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004680 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004681 if (key < 0)
4682 {
4683 key_name[0] = KEY2TERMCAP0(key);
4684 key_name[1] = KEY2TERMCAP1(key);
4685 }
4686 else
4687 {
4688 key_name[0] = KS_KEY;
4689 key_name[1] = (key & 0xff);
4690 }
4691 p = find_termcode(key_name);
4692 if (p != NULL)
4693 {
4694 if (stringval != NULL)
4695 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004696 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004697 }
4698 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004699 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004702 varp = get_varp_scope(&(options[opt_idx]), scope);
4703
4704 if (flagsp != NULL)
4705 // Return the P_xxxx option flags.
4706 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
4708 if (options[opt_idx].flags & P_STRING)
4709 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004710 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004711 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 if (stringval != NULL)
4713 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004714 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004715 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4716 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004718 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004719 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004720 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 *stringval = vim_strsave(*(char_u **)(varp));
4725 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004726 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
4728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004729 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004730 return (options[opt_idx].flags & P_NUM)
4731 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 if (options[opt_idx].flags & P_NUM)
4733 *numval = *(long *)varp;
4734 else
4735 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004736 // Special case: 'modified' is b_changed, but we also want to consider
4737 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 if ((int *)varp == &curbuf->b_changed)
4739 *numval = curbufIsChanged();
4740 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004741 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004743 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744}
4745#endif
4746
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004747#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004748/*
4749 * Returns the option attributes and its value. Unlike the above function it
4750 * will return either global value or local value of the option depending on
4751 * what was requested, but it will never return global value if it was
4752 * requested to return local one and vice versa. Neither it will return
4753 * buffer-local value if it was requested to return window-local one.
4754 *
4755 * Pretends that option is absent if it is not present in the requested scope
4756 * (i.e. has no global, window-local or buffer-local value depending on
4757 * opt_type). Uses
4758 *
4759 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004760 * 0 hidden or unknown option, also option that does not have requested
4761 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004762 * see SOPT_* in vim.h for other flags
4763 *
4764 * Possible opt_type values: see SREQ_* in vim.h
4765 */
4766 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004767get_option_value_strict(
4768 char_u *name,
4769 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004770 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004771 int opt_type,
4772 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004773{
4774 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004775 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004776 struct vimoption *p;
4777 int r = 0;
4778
4779 opt_idx = findoption(name);
4780 if (opt_idx < 0)
4781 return 0;
4782
4783 p = &(options[opt_idx]);
4784
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004785 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004786 if (p->var == NULL)
4787 return 0;
4788
4789 if (p->flags & P_BOOL)
4790 r |= SOPT_BOOL;
4791 else if (p->flags & P_NUM)
4792 r |= SOPT_NUM;
4793 else if (p->flags & P_STRING)
4794 r |= SOPT_STRING;
4795
4796 if (p->indir == PV_NONE)
4797 {
4798 if (opt_type == SREQ_GLOBAL)
4799 r |= SOPT_GLOBAL;
4800 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004801 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004802 }
4803 else
4804 {
4805 if (p->indir & PV_BOTH)
4806 r |= SOPT_GLOBAL;
4807 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004808 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004809
4810 if (p->indir & PV_WIN)
4811 {
4812 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004813 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004814 else
4815 r |= SOPT_WIN;
4816 }
4817 else if (p->indir & PV_BUF)
4818 {
4819 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004820 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004821 else
4822 r |= SOPT_BUF;
4823 }
4824 }
4825
4826 if (stringval == NULL)
4827 return r;
4828
4829 if (opt_type == SREQ_GLOBAL)
4830 varp = p->var;
4831 else
4832 {
4833 if (opt_type == SREQ_BUF)
4834 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004835 // Special case: 'modified' is b_changed, but we also want to
4836 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004837 if (p->indir == PV_MOD)
4838 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004839 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004840 varp = NULL;
4841 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004842# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004843 else if (p->indir == PV_KEY)
4844 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004845 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004846 *stringval = NULL;
4847 varp = NULL;
4848 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004849# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004850 else
4851 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004852 buf_T *save_curbuf = curbuf;
4853
4854 // only getting a pointer, no need to use aucmd_prepbuf()
4855 curbuf = (buf_T *)from;
4856 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004857 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004858 curbuf = save_curbuf;
4859 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004860 }
4861 }
4862 else if (opt_type == SREQ_WIN)
4863 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004864 win_T *save_curwin = curwin;
4865
4866 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004867 curbuf = curwin->w_buffer;
4868 varp = get_varp(p);
4869 curwin = save_curwin;
4870 curbuf = curwin->w_buffer;
4871 }
4872 if (varp == p->var)
4873 return (r | SOPT_UNSET);
4874 }
4875
4876 if (varp != NULL)
4877 {
4878 if (p->flags & P_STRING)
4879 *stringval = vim_strsave(*(char_u **)(varp));
4880 else if (p->flags & P_NUM)
4881 *numval = *(long *) varp;
4882 else
4883 *numval = *(int *)varp;
4884 }
4885
4886 return r;
4887}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004888
4889/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004890 * Iterate over options. First argument is a pointer to a pointer to a
4891 * structure inside options[] array, second is option type like in the above
4892 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004893 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004894 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004895 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004896 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004897 * first argument to NULL.
4898 *
4899 * Returns full option name for current option on each call.
4900 */
4901 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004902option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004903{
4904 struct vimoption *ret = NULL;
4905 do
4906 {
4907 if (*option == NULL)
4908 *option = (void *) options;
4909 else if (((struct vimoption *) (*option))->fullname == NULL)
4910 {
4911 *option = NULL;
4912 return NULL;
4913 }
4914 else
4915 *option = (void *) (((struct vimoption *) (*option)) + 1);
4916
4917 ret = ((struct vimoption *) (*option));
4918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004919 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004920 if (ret->var == NULL)
4921 {
4922 ret = NULL;
4923 continue;
4924 }
4925
4926 switch (opt_type)
4927 {
4928 case SREQ_GLOBAL:
4929 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4930 ret = NULL;
4931 break;
4932 case SREQ_BUF:
4933 if (!(ret->indir & PV_BUF))
4934 ret = NULL;
4935 break;
4936 case SREQ_WIN:
4937 if (!(ret->indir & PV_WIN))
4938 ret = NULL;
4939 break;
4940 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004941 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004942 return NULL;
4943 }
4944 }
4945 while (ret == NULL);
4946
4947 return (char_u *)ret->fullname;
4948}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004949#endif
4950
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004952 * Return the flags for the option at 'opt_idx'.
4953 */
4954 long_u
4955get_option_flags(int opt_idx)
4956{
4957 return options[opt_idx].flags;
4958}
4959
4960/*
4961 * Set a flag for the option at 'opt_idx'.
4962 */
4963 void
4964set_option_flag(int opt_idx, long_u flag)
4965{
4966 options[opt_idx].flags |= flag;
4967}
4968
4969/*
4970 * Clear a flag for the option at 'opt_idx'.
4971 */
4972 void
4973clear_option_flag(int opt_idx, long_u flag)
4974{
4975 options[opt_idx].flags &= ~flag;
4976}
4977
4978/*
4979 * Returns TRUE if the option at 'opt_idx' is a global option
4980 */
4981 int
4982is_global_option(int opt_idx)
4983{
4984 return options[opt_idx].indir == PV_NONE;
4985}
4986
4987/*
4988 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4989 * local value.
4990 */
4991 int
4992is_global_local_option(int opt_idx)
4993{
4994 return options[opt_idx].indir & PV_BOTH;
4995}
4996
4997/*
4998 * Returns TRUE if the option at 'opt_idx' is a window-local option
4999 */
5000 int
5001is_window_local_option(int opt_idx)
5002{
5003 return options[opt_idx].var == VAR_WIN;
5004}
5005
5006/*
5007 * Returns TRUE if the option at 'opt_idx' is a hidden option
5008 */
5009 int
5010is_hidden_option(int opt_idx)
5011{
5012 return options[opt_idx].var == NULL;
5013}
5014
5015#if defined(FEAT_CRYPT) || defined(PROTO)
5016/*
5017 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5018 */
5019 int
5020is_crypt_key_option(int opt_idx)
5021{
5022 return options[opt_idx].indir == PV_KEY;
5023}
5024#endif
5025
5026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 * Set the value of option "name".
5028 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005029 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005030 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005032 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005033set_option_value(
5034 char_u *name,
5035 long number,
5036 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005037 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038{
5039 int opt_idx;
5040 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005041 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
5043 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005044 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005045 {
5046 int key;
5047
5048 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005049 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005050 {
5051 char_u key_name[2];
5052
5053 if (key < 0)
5054 {
5055 key_name[0] = KEY2TERMCAP0(key);
5056 key_name[1] = KEY2TERMCAP1(key);
5057 }
5058 else
5059 {
5060 key_name[0] = KS_KEY;
5061 key_name[1] = (key & 0xff);
5062 }
5063 add_termcode(key_name, string, FALSE);
5064 if (full_screen)
5065 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005066 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005067 return NULL;
5068 }
5069
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005070 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 else
5073 {
5074 flags = options[opt_idx].flags;
5075#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005076 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005078 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005079 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005080 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005083 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005084 return set_string_option(opt_idx, string, opt_flags);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005085
5086 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5087 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005089 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005091 int idx;
5092
5093 // Either we are given a string or we are setting option
5094 // to zero.
5095 for (idx = 0; string[idx] == '0'; ++idx)
5096 ;
5097 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005098 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005099 // There's another character after zeros or the string
5100 // is empty. In both cases, we are trying to set a
5101 // num option using a string.
5102 semsg(_(e_number_required_after_str_equal_str),
5103 name, string);
5104 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005105
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005108 if (flags & P_NUM)
5109 return set_num_option(opt_idx, varp, number,
5110 NULL, 0, opt_flags);
5111 else
5112 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005114
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005116 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117}
5118
5119/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005120 * Call set_option_value() and when an error is returned report it.
5121 */
5122 void
5123set_option_value_give_err(
5124 char_u *name,
5125 long number,
5126 char_u *string,
5127 int opt_flags) // OPT_LOCAL or 0 (both)
5128{
5129 char *errmsg = set_option_value(name, number, string, opt_flags);
5130
5131 if (errmsg != NULL)
5132 emsg(_(errmsg));
5133}
5134
5135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 * Get the terminal code for a terminal option.
5137 * Returns NULL when not found.
5138 */
5139 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005140get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
5142 int opt_idx;
5143 char_u *varp;
5144
5145 if (tname[0] != 't' || tname[1] != '_' ||
5146 tname[2] == NUL || tname[3] == NUL)
5147 return NULL;
5148 if ((opt_idx = findoption(tname)) >= 0)
5149 {
5150 varp = get_varp(&(options[opt_idx]));
5151 if (varp != NULL)
5152 varp = *(char_u **)(varp);
5153 return varp;
5154 }
5155 return find_termcode(tname + 2);
5156}
5157
5158 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005159get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160{
5161 int i;
5162
5163 i = findoption((char_u *)"hl");
5164 if (i >= 0)
5165 return options[i].def_val[VI_DEFAULT];
5166 return (char_u *)NULL;
5167}
5168
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005169 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005170get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005171{
5172 int i;
5173
5174 i = findoption((char_u *)"enc");
5175 if (i >= 0)
5176 return options[i].def_val[VI_DEFAULT];
5177 return (char_u *)NULL;
5178}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005179
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005180#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005181 int
5182is_option_allocated(char *name)
5183{
5184 int idx = findoption((char_u *)name);
5185
5186 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5187}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005188#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005189
Bram Moolenaar28f84e12022-07-27 12:30:13 +01005190#if defined(FEAT_EVAL) || defined(PROTO)
5191/*
5192 * Return TRUE if "name" is a string option.
5193 * Returns FALSE if option "name" does not exist.
5194 */
5195 int
5196is_string_option(char_u *name)
5197{
5198 int idx = findoption(name);
5199
5200 return idx >= 0 && (options[idx].flags & P_STRING);
5201}
5202#endif
5203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204/*
5205 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005206 * When "has_lt" is true there is a '<' before "*arg_arg".
5207 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 */
5209 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005210find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005212 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005214 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215
5216 /*
5217 * Don't use get_special_key_code() for t_xx, we don't want it to call
5218 * add_termcap_entry().
5219 */
5220 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5221 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005222 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005224 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005226 key = find_special_key(&arg, &modifiers,
5227 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005228 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 key = 0;
5230 }
5231 return key;
5232}
5233
5234/*
5235 * if 'all' == 0: show changed options
5236 * if 'all' == 1: show all normal options
5237 * if 'all' == 2: show all terminal options
5238 */
5239 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005240showoptions(
5241 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005242 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243{
5244 struct vimoption *p;
5245 int col;
5246 int isterm;
5247 char_u *varp;
5248 struct vimoption **items;
5249 int item_count;
5250 int run;
5251 int row, rows;
5252 int cols;
5253 int i;
5254 int len;
5255
5256#define INC 20
5257#define GAP 3
5258
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005259 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 if (items == NULL)
5261 return;
5262
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005263 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005265 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005267 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005269 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005271 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005274 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 * 1. display the short items
5276 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005277 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 */
5279 for (run = 1; run <= 2 && !got_int; ++run)
5280 {
5281 /*
5282 * collect the items in items[]
5283 */
5284 item_count = 0;
5285 for (p = &options[0]; p->fullname != NULL; p++)
5286 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005287 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005288 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005289 continue;
5290
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 varp = NULL;
5292 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005293 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
5295 if (p->indir != PV_NONE && !isterm)
5296 varp = get_varp_scope(p, opt_flags);
5297 }
5298 else
5299 varp = get_varp(p);
5300 if (varp != NULL
5301 && ((all == 2 && isterm)
5302 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005303 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005305 if (opt_flags & OPT_ONECOLUMN)
5306 len = Columns;
5307 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005308 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 else
5310 {
5311 option_value2string(p, opt_flags);
5312 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5313 }
5314 if ((len <= INC - GAP && run == 1) ||
5315 (len > INC - GAP && run == 2))
5316 items[item_count++] = p;
5317 }
5318 }
5319
5320 /*
5321 * display the items
5322 */
5323 if (run == 1)
5324 {
5325 cols = (Columns + GAP - 3) / INC;
5326 if (cols == 0)
5327 cols = 1;
5328 rows = (item_count + cols - 1) / cols;
5329 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005330 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 rows = item_count;
5332 for (row = 0; row < rows && !got_int; ++row)
5333 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005334 msg_putchar('\n'); // go to next line
5335 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 break;
5337 col = 0;
5338 for (i = row; i < item_count; i += rows)
5339 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005340 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 showoneopt(items[i], opt_flags);
5342 col += INC;
5343 }
5344 out_flush();
5345 ui_breakcheck();
5346 }
5347 }
5348 vim_free(items);
5349}
5350
5351/*
5352 * Return TRUE if option "p" has its default value.
5353 */
5354 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005355optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
5357 int dvi;
5358
5359 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005360 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005361 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005363 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005365 // the cast to long is required for Manx C, long_i is
5366 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005367 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005368 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5370}
5371
5372/*
5373 * showoneopt: show the value of one option
5374 * must not be called with a hidden option!
5375 */
5376 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005377showoneopt(
5378 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005379 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005381 char_u *varp;
5382 int save_silent = silent_mode;
5383
5384 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005385 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386
5387 varp = get_varp_scope(p, opt_flags);
5388
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005389 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5391 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005392 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005394 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005396 msg_puts(" ");
5397 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 if (!(p->flags & P_BOOL))
5399 {
5400 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005401 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 option_value2string(p, opt_flags);
5403 msg_outtrans(NameBuff);
5404 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005405
5406 silent_mode = save_silent;
5407 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408}
5409
5410/*
5411 * Write modified options as ":set" commands to a file.
5412 *
5413 * There are three values for "opt_flags":
5414 * OPT_GLOBAL: Write global option values and fresh values of
5415 * buffer-local options (used for start of a session
5416 * file).
5417 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5418 * curwin (used for a vimrc file).
5419 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5420 * and local values for window-local options of
5421 * curwin. Local values are also written when at the
5422 * default value, because a modeline or autocommand
5423 * may have set them when doing ":edit file" and the
5424 * user has set them back at the default or fresh
5425 * value.
5426 * When "local_only" is TRUE, don't write fresh
5427 * values, only local values (for ":mkview").
5428 * (fresh value = value used for a new buffer or window for a local option).
5429 *
5430 * Return FAIL on error, OK otherwise.
5431 */
5432 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005433makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434{
5435 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005436 char_u *varp; // currently used value
5437 char_u *varp_fresh; // local value
5438 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 char *cmd;
5440 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005441 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442
5443 /*
5444 * The options that don't have a default (terminal name, columns, lines)
5445 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005446 * Do the loop over "options[]" twice: once for options with the
5447 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005449 for (pri = 1; pri >= 0; --pri)
5450 {
5451 for (p = &options[0]; !istermoption(p); p++)
5452 if (!(p->flags & P_NO_MKRC)
5453 && !istermoption(p)
5454 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005456 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5458 continue;
5459
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005460 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5461 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5463 continue;
5464
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005465 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005467 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 continue;
5469
Bram Moolenaard23b7142021-04-17 21:04:34 +02005470 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5471 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005472 continue;
5473
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 round = 2;
5475 if (p->indir != PV_NONE)
5476 {
5477 if (p->var == VAR_WIN)
5478 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005479 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 if (!(opt_flags & OPT_LOCAL))
5481 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005482 // When fresh value of window-local option is not at the
5483 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5485 {
5486 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005487 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {
5489 round = 1;
5490 varp_local = varp;
5491 varp = varp_fresh;
5492 }
5493 }
5494 }
5495 }
5496
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005497 // Round 1: fresh value for window-local options.
5498 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 for ( ; round <= 2; varp = varp_local, ++round)
5500 {
5501 if (round == 1 || (opt_flags & OPT_GLOBAL))
5502 cmd = "set";
5503 else
5504 cmd = "setlocal";
5505
5506 if (p->flags & P_BOOL)
5507 {
5508 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5509 return FAIL;
5510 }
5511 else if (p->flags & P_NUM)
5512 {
5513 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5514 return FAIL;
5515 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005516 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005518 int do_endif = FALSE;
5519
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005520 // Don't set 'syntax' and 'filetype' again if the value is
5521 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005522 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005523#if defined(FEAT_SYN_HL)
5524 p->indir == PV_SYN ||
5525#endif
5526 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 {
5528 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5529 *(char_u **)(varp)) < 0
5530 || put_eol(fd) < 0)
5531 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005532 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
5534 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005535 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005537 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 {
5539 if (put_line(fd, "endif") == FAIL)
5540 return FAIL;
5541 }
5542 }
5543 }
5544 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 return OK;
5547}
5548
5549#if defined(FEAT_FOLDING) || defined(PROTO)
5550/*
5551 * Generate set commands for the local fold options only. Used when
5552 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5553 */
5554 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005555makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005557 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005559 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 == FAIL
5561# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005562 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005564 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 == FAIL
5566 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5567 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5568 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5569 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5570 )
5571 return FAIL;
5572
5573 return OK;
5574}
5575#endif
5576
5577 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005578put_setstring(
5579 FILE *fd,
5580 char *cmd,
5581 char *name,
5582 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005583 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584{
5585 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005586 char_u *buf = NULL;
5587 char_u *part = NULL;
5588 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
5590 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5591 return FAIL;
5592 if (*valuep != NULL)
5593 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005594 // Output 'pastetoggle' as key names. For other
5595 // options some characters have to be escaped with
5596 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if (valuep == &p_pt)
5598 {
5599 s = *valuep;
5600 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005601 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 return FAIL;
5603 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005604 // expand the option value, replace $HOME by ~
5605 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005607 int size = (int)STRLEN(*valuep) + 1;
5608
5609 // replace home directory in the whole option value into "buf"
5610 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005611 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005612 goto fail;
5613 home_replace(NULL, *valuep, buf, size, FALSE);
5614
5615 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005616 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005617 // can be expanded when read back.
5618 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5619 && vim_strchr(*valuep, ',') != NULL)
5620 {
5621 part = alloc(size);
5622 if (part == NULL)
5623 goto fail;
5624
5625 // write line break to clear the option, e.g. ':set rtp='
5626 if (put_eol(fd) == FAIL)
5627 goto fail;
5628
5629 p = buf;
5630 while (*p != NUL)
5631 {
5632 // for each comma separated option part, append value to
5633 // the option, :set rtp+=value
5634 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5635 goto fail;
5636 (void)copy_option_part(&p, part, size, ",");
5637 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5638 goto fail;
5639 }
5640 vim_free(buf);
5641 vim_free(part);
5642 return OK;
5643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005645 {
5646 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005648 }
5649 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 }
5651 else if (put_escstr(fd, *valuep, 2) == FAIL)
5652 return FAIL;
5653 }
5654 if (put_eol(fd) < 0)
5655 return FAIL;
5656 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005657fail:
5658 vim_free(buf);
5659 vim_free(part);
5660 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661}
5662
5663 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005664put_setnum(
5665 FILE *fd,
5666 char *cmd,
5667 char *name,
5668 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669{
5670 long wc;
5671
5672 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5673 return FAIL;
5674 if (wc_use_keyname((char_u *)valuep, &wc))
5675 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005676 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5678 return FAIL;
5679 }
5680 else if (fprintf(fd, "%ld", *valuep) < 0)
5681 return FAIL;
5682 if (put_eol(fd) < 0)
5683 return FAIL;
5684 return OK;
5685}
5686
5687 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005688put_setbool(
5689 FILE *fd,
5690 char *cmd,
5691 char *name,
5692 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005694 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005695 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5697 || put_eol(fd) < 0)
5698 return FAIL;
5699 return OK;
5700}
5701
5702/*
5703 * Clear all the terminal options.
5704 * If the option has been allocated, free the memory.
5705 * Terminal options are never hidden or indirect.
5706 */
5707 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005708clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 /*
5711 * Reset a few things before clearing the old options. This may cause
5712 * outputting a few things that the terminal doesn't understand, but the
5713 * screen will be cleared later, so this is OK.
5714 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005715 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005716 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005718 // When starting the GUI close the display opened for the clipboard.
5719 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 if (gui.starting)
5721 clear_xterm_clip();
5722#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005723 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005725 free_termoptions();
5726}
5727
5728 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005729free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005730{
5731 struct vimoption *p;
5732
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005733 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 if (istermoption(p))
5735 {
5736 if (p->flags & P_ALLOCED)
5737 free_string_option(*(char_u **)(p->var));
5738 if (p->flags & P_DEF_ALLOCED)
5739 free_string_option(p->def_val[VI_DEFAULT]);
5740 *(char_u **)(p->var) = empty_option;
5741 p->def_val[VI_DEFAULT] = empty_option;
5742 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005743#ifdef FEAT_EVAL
5744 // remember where the option was cleared
5745 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748 clear_termcodes();
5749}
5750
5751/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005752 * Free the string for one term option, if it was allocated.
5753 * Set the string to empty_option and clear allocated flag.
5754 * "var" points to the option value.
5755 */
5756 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005757free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005758{
5759 struct vimoption *p;
5760
5761 for (p = &options[0]; p->fullname != NULL; p++)
5762 if (p->var == var)
5763 {
5764 if (p->flags & P_ALLOCED)
5765 free_string_option(*(char_u **)(p->var));
5766 *(char_u **)(p->var) = empty_option;
5767 p->flags &= ~P_ALLOCED;
5768 break;
5769 }
5770}
5771
5772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 * Set the terminal option defaults to the current value.
5774 * Used after setting the terminal name.
5775 */
5776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005777set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
5779 struct vimoption *p;
5780
5781 for (p = &options[0]; p->fullname != NULL; p++)
5782 {
5783 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5784 {
5785 if (p->flags & P_DEF_ALLOCED)
5786 {
5787 free_string_option(p->def_val[VI_DEFAULT]);
5788 p->flags &= ~P_DEF_ALLOCED;
5789 }
5790 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5791 if (p->flags & P_ALLOCED)
5792 {
5793 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005794 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 }
5796 }
5797 }
5798}
5799
5800/*
5801 * return TRUE if 'p' starts with 't_'
5802 */
5803 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005804istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
5806 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5807}
5808
Bram Moolenaardac13472019-09-16 21:06:21 +02005809/*
5810 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5811 */
5812 int
5813istermoption_idx(int opt_idx)
5814{
5815 return istermoption(&options[opt_idx]);
5816}
5817
Bram Moolenaar113e1072019-01-20 15:30:40 +01005818#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005820 * Unset local option value, similar to ":set opt<".
5821 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005822 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005823unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005824{
5825 struct vimoption *p;
5826 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005827 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005828
5829 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005830 if (opt_idx < 0)
5831 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005832 p = &(options[opt_idx]);
5833
5834 switch ((int)p->indir)
5835 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005836 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005837 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005838 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005839 break;
5840 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005841 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005842 break;
5843 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005844 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005845 break;
5846 case PV_AR:
5847 buf->b_p_ar = -1;
5848 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005849 case PV_BKC:
5850 clear_string_option(&buf->b_p_bkc);
5851 buf->b_bkc_flags = 0;
5852 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005853 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005854 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005855 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005856 case PV_TC:
5857 clear_string_option(&buf->b_p_tc);
5858 buf->b_tc_flags = 0;
5859 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005860 case PV_SISO:
5861 curwin->w_p_siso = -1;
5862 break;
5863 case PV_SO:
5864 curwin->w_p_so = -1;
5865 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005866# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005867 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005868 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005869 break;
5870 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005871 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005872 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005873# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005874 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005875 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005876 break;
5877 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005878 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005879 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005880# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005881 case PV_TSRFU:
5882 clear_string_option(&buf->b_p_tsrfu);
5883 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005884# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005885 case PV_FP:
5886 clear_string_option(&buf->b_p_fp);
5887 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005888# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005889 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005890 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005891 break;
5892 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005893 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005894 break;
5895 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005896 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005897 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005898# endif
5899# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005900 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005901 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005902 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005903# endif
5904# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005905 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005906 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005907 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005908# endif
5909# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01005910 case PV_SBR:
5911 clear_string_option(&((win_T *)from)->w_p_sbr);
5912 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005913# endif
5914# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005915 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005916 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005917 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005918# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005919 case PV_UL:
5920 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5921 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005922 case PV_LW:
5923 clear_string_option(&buf->b_p_lw);
5924 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005925 case PV_MENC:
5926 clear_string_option(&buf->b_p_menc);
5927 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005928 case PV_LCS:
5929 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005930 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005931 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005932 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005933 case PV_FCS:
5934 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005935 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005936 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005937 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005938 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005939 clear_string_option(&((win_T *)from)->w_p_ve);
5940 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005941 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005942 }
5943}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005944#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005945
5946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005948 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 */
5950 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005951get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005953 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 {
5955 if (p->var == VAR_WIN)
5956 return (char_u *)GLOBAL_WO(get_varp(p));
5957 return p->var;
5958 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005959 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 {
5961 switch ((int)p->indir)
5962 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005963 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005965 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5966 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5967 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005969 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5970 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5971 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005972 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005973 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005974 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005975 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5976 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005978 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5979 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005981 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5982 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005983#ifdef FEAT_COMPL_FUNC
5984 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5985#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005986#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5987 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5988#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005989#if defined(FEAT_CRYPT)
5990 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5991#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005992#ifdef FEAT_LINEBREAK
5993 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5994#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005995#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005996 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005997#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005998 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005999 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006000 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006001 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006002 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006003 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006004 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006005
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006007 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 }
6009 return get_varp(p);
6010}
6011
6012/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006013 * Get pointer to option variable at 'opt_idx', depending on local or global
6014 * scope.
6015 */
6016 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006017get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006018{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006019 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006020}
6021
6022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 * Get pointer to option variable.
6024 */
6025 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006026get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006028 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 if (p->var == NULL)
6030 return NULL;
6031
6032 switch ((int)p->indir)
6033 {
6034 case PV_NONE: return p->var;
6035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006036 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006037 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006039 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006041 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006043 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006045 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006047 case PV_TC: return *curbuf->b_p_tc != NUL
6048 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006049 case PV_BKC: return *curbuf->b_p_bkc != NUL
6050 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006051 case PV_SISO: return curwin->w_p_siso >= 0
6052 ? (char_u *)&(curwin->w_p_siso) : p->var;
6053 case PV_SO: return curwin->w_p_so >= 0
6054 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006056 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006058 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6060#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006061 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006063 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006065#ifdef FEAT_COMPL_FUNC
6066 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6067 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6068#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006069 case PV_FP: return *curbuf->b_p_fp != NUL
6070 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006072 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006074 case PV_GP: return *curbuf->b_p_gp != NUL
6075 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6076 case PV_MP: return *curbuf->b_p_mp != NUL
6077 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006079#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6080 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6081 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6082#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006083#if defined(FEAT_CRYPT)
6084 case PV_CM: return *curbuf->b_p_cm != NUL
6085 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6086#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006087#ifdef FEAT_LINEBREAK
6088 case PV_SBR: return *curwin->w_p_sbr != NUL
6089 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6090#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006091#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006092 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006093 ? (char_u *)&(curwin->w_p_stl) : p->var;
6094#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006095 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6096 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006097 case PV_LW: return *curbuf->b_p_lw != NUL
6098 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006099 case PV_MENC: return *curbuf->b_p_menc != NUL
6100 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101#ifdef FEAT_ARABIC
6102 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6103#endif
6104 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006105 case PV_LCS: return *curwin->w_p_lcs != NUL
6106 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006107 case PV_FCS: return *curwin->w_p_fcs != NUL
6108 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006109 case PV_VE: return *curwin->w_p_ve != NUL
6110 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006111#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006112 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6113#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006114#ifdef FEAT_SYN_HL
6115 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6116 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006117 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006118 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120#ifdef FEAT_DIFF
6121 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6122#endif
6123#ifdef FEAT_FOLDING
6124 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6125 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6126 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6127 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6128 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6129 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6130 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6131# ifdef FEAT_EVAL
6132 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6133 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6134# endif
6135 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6136#endif
6137 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006138 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006139#ifdef FEAT_LINEBREAK
6140 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006143 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006144#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6146#endif
6147#ifdef FEAT_RIGHTLEFT
6148 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6149 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6150#endif
6151 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006152 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6154#ifdef FEAT_LINEBREAK
6155 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006156 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6157 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006159 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006161 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006162#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006163 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6164 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6165#endif
6166#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006167 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6168 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6169 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171
6172 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6173 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6176 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6178 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6180 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6181 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006182 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185#ifdef FEAT_FOLDING
6186 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006189#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006190 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006192#ifdef FEAT_COMPL_FUNC
6193 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006194 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006195#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006196#ifdef FEAT_EVAL
6197 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6198#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006199 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006201 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006207 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6209 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6210 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6211 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6212#ifdef FEAT_FIND_ID
6213# ifdef FEAT_EVAL
6214 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6215# endif
6216#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006217#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6219 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6220#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006221#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006222 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224#ifdef FEAT_CRYPT
6225 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006228 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6230 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6231 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6232 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6233 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006235 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6242#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006243 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006244 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6245#endif
6246#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006247 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6248 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6249 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006250 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251#endif
6252 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6253 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6254 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6255 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006256#ifdef FEAT_PERSISTENT_UNDO
6257 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6260#ifdef FEAT_KEYMAP
6261 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6262#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006263#ifdef FEAT_SIGNS
6264 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6265#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006266#ifdef FEAT_VARTABS
6267 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6268 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6269#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006270 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006272 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 return (char_u *)&(curbuf->b_p_wm);
6274}
6275
6276/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006277 * Return a pointer to the variable for option at 'opt_idx'
6278 */
6279 char_u *
6280get_option_var(int opt_idx)
6281{
6282 return options[opt_idx].var;
6283}
6284
Dominique Pelle748b3082022-01-08 12:41:16 +00006285#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006286/*
6287 * Return the full name of the option at 'opt_idx'
6288 */
6289 char_u *
6290get_option_fullname(int opt_idx)
6291{
6292 return (char_u *)options[opt_idx].fullname;
6293}
Dominique Pelle748b3082022-01-08 12:41:16 +00006294#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006295
6296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 * Get the value of 'equalprg', either the buffer-local one or the global one.
6298 */
6299 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006300get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 if (*curbuf->b_p_ep == NUL)
6303 return p_ep;
6304 return curbuf->b_p_ep;
6305}
6306
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307/*
6308 * Copy options from one window to another.
6309 * Used when splitting a window.
6310 */
6311 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006312win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313{
6314 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6315 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006316 after_copy_winopt(wp_to);
6317}
6318
6319/*
6320 * After copying window options: update variables depending on options.
6321 */
6322 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006323after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006324{
6325#ifdef FEAT_LINEBREAK
6326 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006327#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006328#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006329 fill_culopt_flags(NULL, wp);
6330 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006331#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006332 set_chars_option(wp, &wp->w_p_lcs, TRUE);
6333 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006334}
6335
6336 static char_u *
6337copy_option_val(char_u *val)
6338{
6339 if (val == empty_option)
6340 return empty_option; // no need to allocate memory
6341 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343
6344/*
6345 * Copy the options from one winopt_T to another.
6346 * Doesn't free the old option values in "to", use clear_winopt() for that.
6347 * The 'scroll' option is not copied, because it depends on the window height.
6348 * The 'previewwindow' option is reset, there can be only one preview window.
6349 */
6350 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006351copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
6353#ifdef FEAT_ARABIC
6354 to->wo_arab = from->wo_arab;
6355#endif
6356 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006357 to->wo_lcs = copy_option_val(from->wo_lcs);
6358 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006360 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006361 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006362 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006363#ifdef FEAT_LINEBREAK
6364 to->wo_nuw = from->wo_nuw;
6365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366#ifdef FEAT_RIGHTLEFT
6367 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006368 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006370#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006371 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006372#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006373#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006374 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006377#ifdef FEAT_DIFF
6378 to->wo_wrap_save = from->wo_wrap_save;
6379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380#ifdef FEAT_LINEBREAK
6381 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006382 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006383 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006385 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006387 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006388 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006389 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006390 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006391#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006392 to->wo_spell = from->wo_spell;
6393#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006394#ifdef FEAT_SYN_HL
6395 to->wo_cuc = from->wo_cuc;
6396 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006397 to->wo_culopt = copy_option_val(from->wo_culopt);
6398 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400#ifdef FEAT_DIFF
6401 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006402 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006404#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006405 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006406 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006407#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006408#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006409 to->wo_twk = copy_option_val(from->wo_twk);
6410 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412#ifdef FEAT_FOLDING
6413 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006414 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006416 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006417 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 to->wo_fml = from->wo_fml;
6419 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006420 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006421 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006422 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006423 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 to->wo_fdn = from->wo_fdn;
6425# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006426 to->wo_fde = copy_option_val(from->wo_fde);
6427 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006429 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006431#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006432 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006433#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006434
6435#ifdef FEAT_EVAL
6436 // Copy the script context so that we know where the value was last set.
6437 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6438 sizeof(to->wo_script_ctx));
6439#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006440 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441}
6442
6443/*
6444 * Check string options in a window for a NULL value.
6445 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006446 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006447check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449 check_winopt(&win->w_onebuf_opt);
6450 check_winopt(&win->w_allbuf_opt);
6451}
6452
6453/*
6454 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6455 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006456 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006457check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
6459#ifdef FEAT_FOLDING
6460 check_string_option(&wop->wo_fdi);
6461 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006462 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463# ifdef FEAT_EVAL
6464 check_string_option(&wop->wo_fde);
6465 check_string_option(&wop->wo_fdt);
6466# endif
6467 check_string_option(&wop->wo_fmr);
6468#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006469#ifdef FEAT_SIGNS
6470 check_string_option(&wop->wo_scl);
6471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472#ifdef FEAT_RIGHTLEFT
6473 check_string_option(&wop->wo_rlc);
6474#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006475#ifdef FEAT_LINEBREAK
6476 check_string_option(&wop->wo_sbr);
6477#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006478#ifdef FEAT_STL_OPT
6479 check_string_option(&wop->wo_stl);
6480#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006481#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006482 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006483 check_string_option(&wop->wo_cc);
6484#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006485#ifdef FEAT_CONCEAL
6486 check_string_option(&wop->wo_cocu);
6487#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006488#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006489 check_string_option(&wop->wo_twk);
6490 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006491#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006492#ifdef FEAT_LINEBREAK
6493 check_string_option(&wop->wo_briopt);
6494#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006495 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006496 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006497 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006498 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499}
6500
6501/*
6502 * Free the allocated memory inside a winopt_T.
6503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006505clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
6507#ifdef FEAT_FOLDING
6508 clear_string_option(&wop->wo_fdi);
6509 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006510 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511# ifdef FEAT_EVAL
6512 clear_string_option(&wop->wo_fde);
6513 clear_string_option(&wop->wo_fdt);
6514# endif
6515 clear_string_option(&wop->wo_fmr);
6516#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006517#ifdef FEAT_SIGNS
6518 clear_string_option(&wop->wo_scl);
6519#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006520#ifdef FEAT_LINEBREAK
6521 clear_string_option(&wop->wo_briopt);
6522#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006523 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524#ifdef FEAT_RIGHTLEFT
6525 clear_string_option(&wop->wo_rlc);
6526#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006527#ifdef FEAT_LINEBREAK
6528 clear_string_option(&wop->wo_sbr);
6529#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006530#ifdef FEAT_STL_OPT
6531 clear_string_option(&wop->wo_stl);
6532#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006533#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006534 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006535 clear_string_option(&wop->wo_cc);
6536#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006537#ifdef FEAT_CONCEAL
6538 clear_string_option(&wop->wo_cocu);
6539#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006540#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006541 clear_string_option(&wop->wo_twk);
6542 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006543#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006544 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006545 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006546 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547}
6548
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006549#ifdef FEAT_EVAL
6550// Index into the options table for a buffer-local option enum.
6551static int buf_opt_idx[BV_COUNT];
6552# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6553
6554/*
6555 * Initialize buf_opt_idx[] if not done already.
6556 */
6557 static void
6558init_buf_opt_idx(void)
6559{
6560 static int did_init_buf_opt_idx = FALSE;
6561 int i;
6562
6563 if (did_init_buf_opt_idx)
6564 return;
6565 did_init_buf_opt_idx = TRUE;
6566 for (i = 0; !istermoption_idx(i); i++)
6567 if (options[i].indir & PV_BUF)
6568 buf_opt_idx[options[i].indir & PV_MASK] = i;
6569}
6570#else
6571# define COPY_OPT_SCTX(buf, bv)
6572#endif
6573
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574/*
6575 * Copy global option values to local options for one buffer.
6576 * Used when creating a new buffer and sometimes when entering a buffer.
6577 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006578 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6580 * appropriate.
6581 * BCO_NOHELP Don't copy the values to a help buffer.
6582 */
6583 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006584buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
6586 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006587 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 int dont_do_help;
6589 int did_isk = FALSE;
6590
6591 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 * Skip this when the option defaults have not been set yet. Happens when
6593 * main() allocates the first buffer.
6594 */
6595 if (p_cpo != NULL)
6596 {
6597 /*
6598 * Always copy when entering and 'cpo' contains 'S'.
6599 * Don't copy when already initialized.
6600 * Don't copy when 'cpo' contains 's' and not entering.
6601 * 'S' BCO_ENTER initialized 's' should_copy
6602 * yes yes X X TRUE
6603 * yes no yes X FALSE
6604 * no X yes X FALSE
6605 * X no no yes FALSE
6606 * X no no no TRUE
6607 * no yes no X TRUE
6608 */
6609 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6610 && (buf->b_p_initialized
6611 || (!(flags & BCO_ENTER)
6612 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6613 should_copy = FALSE;
6614
6615 if (should_copy || (flags & BCO_ALWAYS))
6616 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006617#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006618 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006619 init_buf_opt_idx();
6620#endif
6621 // Don't copy the options specific to a help buffer when
6622 // BCO_NOHELP is given or the options were initialized already
6623 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6625 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006626 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 {
6628 save_p_isk = buf->b_p_isk;
6629 buf->b_p_isk = NULL;
6630 }
6631 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02006632 * Always free the allocated strings. If not already initialized,
6633 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 */
6635 if (!buf->b_p_initialized)
6636 {
6637 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006638 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006641 switch (*p_ffs)
6642 {
6643 case 'm':
6644 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6645 case 'd':
6646 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6647 case 'u':
6648 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6649 default:
6650 buf->b_p_ff = vim_strsave(p_ff);
6651 }
6652 if (buf->b_p_ff != NULL)
6653 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 buf->b_p_bh = empty_option;
6655 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 }
6657 else
6658 free_buf_options(buf, FALSE);
6659
6660 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006661 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 buf->b_p_ai_nopaste = p_ai_nopaste;
6663 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006664 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006666 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 buf->b_p_tw_nopaste = p_tw_nopaste;
6668 buf->b_p_tw_nobin = p_tw_nobin;
6669 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006670 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 buf->b_p_wm_nopaste = p_wm_nopaste;
6672 buf->b_p_wm_nobin = p_wm_nobin;
6673 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006674 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006675 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006676 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006677 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006678 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006680 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006682 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006684 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 buf->b_p_ml_nobin = p_ml_nobin;
6686 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006687 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006688 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006689 buf->b_p_swf = FALSE;
6690 else
6691 {
6692 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006693 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006696 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006697#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006698 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006699 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006701#ifdef FEAT_COMPL_FUNC
6702 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006703 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006704 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006705 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006706 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006707 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006708#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006709#ifdef FEAT_EVAL
6710 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006711 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006712 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006715 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006717#ifdef FEAT_VARTABS
6718 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006719 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006720 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006721 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006722 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006723 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006724 buf->b_p_vsts_nopaste = p_vsts_nopaste
6725 ? vim_strsave(p_vsts_nopaste) : NULL;
6726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006728 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006730 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731#ifdef FEAT_FOLDING
6732 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006733 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734#endif
6735 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006736 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006737 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006738 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006739 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6740 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006742 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006744 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006746 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006748 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006749
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006751 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006753 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006755 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006756 buf->b_p_cinsd = vim_strsave(p_cinsd);
6757 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006758 buf->b_p_lop = vim_strsave(p_lop);
6759 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006760
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006761 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006764 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006766 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006768 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006770 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006772 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006773 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006774 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006775#endif
6776#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006777 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006778 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006779 (void)compile_cap_prog(&buf->b_s);
6780 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006781 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006782 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006783 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006784 buf->b_s.b_p_spo = vim_strsave(p_spo);
6785 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006787#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006789 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006791 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006793 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006794#if defined(FEAT_EVAL)
6795 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006796 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798#ifdef FEAT_CRYPT
6799 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006800 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006803 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804#ifdef FEAT_KEYMAP
6805 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006806 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 buf->b_kmap_state |= KEYMAP_INIT;
6808#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006809#ifdef FEAT_TERMINAL
6810 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006811 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006812#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006813 // This isn't really an option, but copying the langmap and IME
6814 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006816 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006818 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006820 // options that are normally global but also have a local value
6821 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006823 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006824 buf->b_p_bkc = empty_option;
6825 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826#ifdef FEAT_QUICKFIX
6827 buf->b_p_gp = empty_option;
6828 buf->b_p_mp = empty_option;
6829 buf->b_p_efm = empty_option;
6830#endif
6831 buf->b_p_ep = empty_option;
6832 buf->b_p_kp = empty_option;
6833 buf->b_p_path = empty_option;
6834 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006835 buf->b_p_tc = empty_option;
6836 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837#ifdef FEAT_FIND_ID
6838 buf->b_p_def = empty_option;
6839 buf->b_p_inc = empty_option;
6840# ifdef FEAT_EVAL
6841 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006842 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843# endif
6844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 buf->b_p_dict = empty_option;
6846 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006847#ifdef FEAT_COMPL_FUNC
6848 buf->b_p_tsrfu = empty_option;
6849#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006850 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006851 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006852#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6853 buf->b_p_bexpr = empty_option;
6854#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006855#if defined(FEAT_CRYPT)
6856 buf->b_p_cm = empty_option;
6857#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006858#ifdef FEAT_PERSISTENT_UNDO
6859 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006860 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006861#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006862 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006863 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864
6865 /*
6866 * Don't copy the options set by ex_help(), use the saved values,
6867 * when going from a help buffer to a non-help buffer.
6868 * Don't touch these at all when BCO_NOHELP is used and going from
6869 * or to a help buffer.
6870 */
6871 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006874#ifdef FEAT_VARTABS
6875 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006876 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006877 else
6878 buf->b_p_vts_array = NULL;
6879#endif
6880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 else
6882 {
6883 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006884 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 did_isk = TRUE;
6886 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006887 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006888#ifdef FEAT_VARTABS
6889 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006890 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006891 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006892 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006893 else
6894 buf->b_p_vts_array = NULL;
6895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 if (buf->b_p_bt[0] == 'h')
6898 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006900 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 }
6902 }
6903
6904 /*
6905 * When the options should be copied (ignoring BCO_ALWAYS), set the
6906 * flag that indicates that the options have been initialized.
6907 */
6908 if (should_copy)
6909 buf->b_p_initialized = TRUE;
6910 }
6911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006912 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 if (did_isk)
6914 (void)buf_init_chartab(buf, FALSE);
6915}
6916
6917/*
6918 * Reset the 'modifiable' option and its default value.
6919 */
6920 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006921reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922{
6923 int opt_idx;
6924
6925 curbuf->b_p_ma = FALSE;
6926 p_ma = FALSE;
6927 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006928 if (opt_idx >= 0)
6929 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930}
6931
6932/*
6933 * Set the global value for 'iminsert' to the local value.
6934 */
6935 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006936set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
6938 p_iminsert = curbuf->b_p_iminsert;
6939}
6940
6941/*
6942 * Set the global value for 'imsearch' to the local value.
6943 */
6944 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006945set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946{
6947 p_imsearch = curbuf->b_p_imsearch;
6948}
6949
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950static int expand_option_idx = -1;
6951static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6952static int expand_option_flags = 0;
6953
6954 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006955set_context_in_set_cmd(
6956 expand_T *xp,
6957 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006958 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959{
6960 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006961 long_u flags = 0; // init for GCC
6962 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 char_u *p;
6964 char_u *s;
6965 int is_term_option = FALSE;
6966 int key;
6967
6968 expand_option_flags = opt_flags;
6969
6970 xp->xp_context = EXPAND_SETTINGS;
6971 if (*arg == NUL)
6972 {
6973 xp->xp_pattern = arg;
6974 return;
6975 }
6976 p = arg + STRLEN(arg) - 1;
6977 if (*p == ' ' && *(p - 1) != '\\')
6978 {
6979 xp->xp_pattern = p + 1;
6980 return;
6981 }
6982 while (p > arg)
6983 {
6984 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006985 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 if (*p == ' ' || *p == ',')
6987 {
6988 while (s > arg && *(s - 1) == '\\')
6989 --s;
6990 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006991 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (*p == ' ' && ((p - s) & 1) == 0)
6993 {
6994 ++p;
6995 break;
6996 }
6997 --p;
6998 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006999 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {
7001 xp->xp_context = EXPAND_BOOL_SETTINGS;
7002 p += 2;
7003 }
7004 if (STRNCMP(p, "inv", 3) == 0)
7005 {
7006 xp->xp_context = EXPAND_BOOL_SETTINGS;
7007 p += 3;
7008 }
7009 xp->xp_pattern = arg = p;
7010 if (*arg == '<')
7011 {
7012 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007013 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 return;
7015 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007016 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 {
7018 xp->xp_context = EXPAND_NOTHING;
7019 return;
7020 }
7021 nextchar = *++p;
7022 is_term_option = TRUE;
7023 expand_option_name[2] = KEY2TERMCAP0(key);
7024 expand_option_name[3] = KEY2TERMCAP1(key);
7025 }
7026 else
7027 {
7028 if (p[0] == 't' && p[1] == '_')
7029 {
7030 p += 2;
7031 if (*p != NUL)
7032 ++p;
7033 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007034 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 nextchar = *++p;
7036 is_term_option = TRUE;
7037 expand_option_name[2] = p[-2];
7038 expand_option_name[3] = p[-1];
7039 }
7040 else
7041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007042 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7044 p++;
7045 if (*p == NUL)
7046 return;
7047 nextchar = *p;
7048 *p = NUL;
7049 opt_idx = findoption(arg);
7050 *p = nextchar;
7051 if (opt_idx == -1 || options[opt_idx].var == NULL)
7052 {
7053 xp->xp_context = EXPAND_NOTHING;
7054 return;
7055 }
7056 flags = options[opt_idx].flags;
7057 if (flags & P_BOOL)
7058 {
7059 xp->xp_context = EXPAND_NOTHING;
7060 return;
7061 }
7062 }
7063 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007064 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7066 {
7067 ++p;
7068 nextchar = '=';
7069 }
7070 if ((nextchar != '=' && nextchar != ':')
7071 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7072 {
7073 xp->xp_context = EXPAND_UNSUCCESSFUL;
7074 return;
7075 }
7076 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7077 {
7078 xp->xp_context = EXPAND_OLD_SETTING;
7079 if (is_term_option)
7080 expand_option_idx = -1;
7081 else
7082 expand_option_idx = opt_idx;
7083 xp->xp_pattern = p + 1;
7084 return;
7085 }
7086 xp->xp_context = EXPAND_NOTHING;
7087 if (is_term_option || (flags & P_NUM))
7088 return;
7089
7090 xp->xp_pattern = p + 1;
7091
7092 if (flags & P_EXPAND)
7093 {
7094 p = options[opt_idx].var;
7095 if (p == (char_u *)&p_bdir
7096 || p == (char_u *)&p_dir
7097 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007098 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101#ifdef FEAT_SESSION
7102 || p == (char_u *)&p_vdir
7103#endif
7104 )
7105 {
7106 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007107 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 xp->xp_backslash = XP_BS_THREE;
7109 else
7110 xp->xp_backslash = XP_BS_ONE;
7111 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007112 else if (p == (char_u *)&p_ft)
7113 {
7114 xp->xp_context = EXPAND_FILETYPE;
7115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 else
7117 {
7118 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007119 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 if (p == (char_u *)&p_tags)
7121 xp->xp_backslash = XP_BS_THREE;
7122 else
7123 xp->xp_backslash = XP_BS_ONE;
7124 }
7125 }
7126
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007127 // For an option that is a list of file names, find the start of the
7128 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7130 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007131 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 if (*p == ' ' || *p == ',')
7133 {
7134 s = p;
7135 while (s > xp->xp_pattern && *(s - 1) == '\\')
7136 --s;
7137 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7138 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7139 {
7140 xp->xp_pattern = p + 1;
7141 break;
7142 }
7143 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007144
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007145#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007146 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007147 if (options[opt_idx].var == (char_u *)&p_sps
7148 && STRNCMP(p, "file:", 5) == 0)
7149 {
7150 xp->xp_pattern = p + 5;
7151 break;
7152 }
7153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155}
7156
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007157/*
7158 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7159 *
7160 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7161 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7162 *
7163 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7164 * regular expression 'regmatch', then stores the match in matches[idx] and
7165 * returns TRUE.
7166 *
7167 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7168 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7169 *
7170 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7171 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7172 */
7173 static int
7174match_str(
7175 char_u *str,
7176 regmatch_T *regmatch,
7177 char_u **matches,
7178 int idx,
7179 int test_only,
7180 int fuzzy,
7181 char_u *fuzzystr,
7182 fuzmatch_str_T *fuzmatch)
7183{
7184 if (!fuzzy)
7185 {
7186 if (vim_regexec(regmatch, str, (colnr_T)0))
7187 {
7188 if (!test_only)
7189 matches[idx] = vim_strsave(str);
7190 return TRUE;
7191 }
7192 }
7193 else
7194 {
7195 int score;
7196
7197 score = fuzzy_match_str(str, fuzzystr);
7198 if (score != 0)
7199 {
7200 if (!test_only)
7201 {
7202 fuzmatch[idx].idx = idx;
7203 fuzmatch[idx].str = vim_strsave(str);
7204 fuzmatch[idx].score = score;
7205 }
7206
7207 return TRUE;
7208 }
7209 }
7210
7211 return FALSE;
7212}
7213
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007215ExpandSettings(
7216 expand_T *xp,
7217 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007218 char_u *fuzzystr,
7219 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007220 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007221 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007223 int num_normal = 0; // Nr of matching non-term-code settings
7224 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 int opt_idx;
7226 int match;
7227 int count = 0;
7228 char_u *str;
7229 int loop;
7230 int is_term_opt;
7231 char_u name_buf[MAX_KEY_NAME_LEN];
7232 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007233 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007234 int fuzzy;
7235 fuzmatch_str_T *fuzmatch = NULL;
7236
Christian Brabandtcb747892022-05-08 21:10:56 +01007237 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007239 // do this loop twice:
7240 // loop == 0: count the number of matching options
7241 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 for (loop = 0; loop <= 1; ++loop)
7243 {
7244 regmatch->rm_ic = ic;
7245 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7246 {
K.Takataeeec2542021-06-02 13:28:16 +02007247 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007248 {
7249 if (match_str((char_u *)names[match], regmatch, *matches,
7250 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 {
7252 if (loop == 0)
7253 num_normal++;
7254 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007255 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 }
7259 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7260 opt_idx++)
7261 {
7262 if (options[opt_idx].var == NULL)
7263 continue;
7264 if (xp->xp_context == EXPAND_BOOL_SETTINGS
7265 && !(options[opt_idx].flags & P_BOOL))
7266 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007267 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 if (is_term_opt && num_normal > 0)
7269 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007270
7271 if (match_str(str, regmatch, *matches, count, (loop == 0),
7272 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {
7274 if (loop == 0)
7275 {
7276 if (is_term_opt)
7277 num_term++;
7278 else
7279 num_normal++;
7280 }
7281 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007282 count++;
7283 }
7284 else if (!fuzzy && options[opt_idx].shortname != NULL
7285 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007286 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007287 {
7288 // Compare against the abbreviated option name (for regular
7289 // expression match). Fuzzy matching (previous if) already
7290 // matches against both the expanded and abbreviated names.
7291 if (loop == 0)
7292 {
7293 if (is_term_opt)
7294 num_term++;
7295 else
7296 num_normal++;
7297 }
7298 else
7299 (*matches)[count++] = vim_strsave(str);
7300 }
7301 else if (is_term_opt)
7302 {
7303 name_buf[0] = '<';
7304 name_buf[1] = 't';
7305 name_buf[2] = '_';
7306 name_buf[3] = str[2];
7307 name_buf[4] = str[3];
7308 name_buf[5] = '>';
7309 name_buf[6] = NUL;
7310
7311 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7312 fuzzy, fuzzystr, fuzmatch))
7313 {
7314 if (loop == 0)
7315 num_term++;
7316 else
7317 count++;
7318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 }
7320 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007321
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 /*
7323 * Check terminal key codes, these are not in the option table
7324 */
7325 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7326 {
7327 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7328 {
7329 if (!isprint(str[0]) || !isprint(str[1]))
7330 continue;
7331
7332 name_buf[0] = 't';
7333 name_buf[1] = '_';
7334 name_buf[2] = str[0];
7335 name_buf[3] = str[1];
7336 name_buf[4] = NUL;
7337
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007338 if (match_str(name_buf, regmatch, *matches, count,
7339 (loop == 0), fuzzy, fuzzystr, fuzmatch))
7340 {
7341 if (loop == 0)
7342 num_term++;
7343 else
7344 count++;
7345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 else
7347 {
7348 name_buf[0] = '<';
7349 name_buf[1] = 't';
7350 name_buf[2] = '_';
7351 name_buf[3] = str[0];
7352 name_buf[4] = str[1];
7353 name_buf[5] = '>';
7354 name_buf[6] = NUL;
7355
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007356 if (match_str(name_buf, regmatch, *matches, count,
7357 (loop == 0), fuzzy, fuzzystr,
7358 fuzmatch))
7359 {
7360 if (loop == 0)
7361 num_term++;
7362 else
7363 count++;
7364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 }
7366 }
7367
7368 /*
7369 * Check special key names.
7370 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007371 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7373 {
7374 name_buf[0] = '<';
7375 STRCPY(name_buf + 1, str);
7376 STRCAT(name_buf, ">");
7377
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007378 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7379 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
7381 if (loop == 0)
7382 num_term++;
7383 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007384 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 }
7386 }
7387 }
7388 if (loop == 0)
7389 {
7390 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007391 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007393 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 else
7395 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007396 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007398 *matches = ALLOC_MULT(char_u *, *numMatches);
7399 if (*matches == NULL)
7400 {
7401 *matches = (char_u **)"";
7402 return FAIL;
7403 }
7404 }
7405 else
7406 {
7407 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7408 if (fuzmatch == NULL)
7409 {
7410 *matches = (char_u **)"";
7411 return FAIL;
7412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 }
7414 }
7415 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007416
7417 if (fuzzy &&
7418 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7419 return FAIL;
7420
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 return OK;
7422}
7423
7424 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007425ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007427 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 char_u *buf;
7429
zeertzjqb0d45ec2023-01-25 15:04:22 +00007430 *numMatches = 0;
7431 *matches = ALLOC_ONE(char_u *);
7432 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 return FAIL;
7434
7435 /*
7436 * For a terminal key code expand_option_idx is < 0.
7437 */
7438 if (expand_option_idx < 0)
7439 {
7440 var = find_termcode(expand_option_name + 2);
7441 if (var == NULL)
7442 expand_option_idx = findoption(expand_option_name);
7443 }
7444
7445 if (expand_option_idx >= 0)
7446 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007447 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 option_value2string(&options[expand_option_idx], expand_option_flags);
7449 var = NameBuff;
7450 }
7451 else if (var == NULL)
7452 var = (char_u *)"";
7453
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007454 // A backslash is required before some characters. This is the reverse of
7455 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 buf = vim_strsave_escaped(var, escape_chars);
7457
7458 if (buf == NULL)
7459 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007460 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 return FAIL;
7462 }
7463
7464#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007465 // For MS-Windows et al. we don't double backslashes at the start and
7466 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007467 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 if (var[0] == '\\' && var[1] == '\\'
7469 && expand_option_idx >= 0
7470 && (options[expand_option_idx].flags & P_EXPAND)
7471 && vim_isfilec(var[2])
7472 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007473 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474#endif
7475
zeertzjqb0d45ec2023-01-25 15:04:22 +00007476 *matches[0] = buf;
7477 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 return OK;
7479}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480
7481/*
7482 * Get the value for the numeric or string option *opp in a nice format into
7483 * NameBuff[]. Must not be called with a hidden option!
7484 */
7485 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007486option_value2string(
7487 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007488 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
7490 char_u *varp;
7491
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007492 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493
7494 if (opp->flags & P_NUM)
7495 {
7496 long wc = 0;
7497
7498 if (wc_use_keyname(varp, &wc))
7499 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7500 else if (wc != 0)
7501 STRCPY(NameBuff, transchar((int)wc));
7502 else
7503 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7504 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007505 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 {
7507 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007508 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 NameBuff[0] = NUL;
7510#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007511 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007512 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 STRCPY(NameBuff, "*****");
7514#endif
7515 else if (opp->flags & P_EXPAND)
7516 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007517 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 else if ((char_u **)opp->var == &p_pt)
7519 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7520 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007521 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 }
7523}
7524
7525/*
7526 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7527 * printed as a keyname.
7528 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7529 */
7530 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007531wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532{
7533 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7534 {
7535 *wcp = *(long *)varp;
7536 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7537 return TRUE;
7538 }
7539 return FALSE;
7540}
7541
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 * Return TRUE if "x" is present in 'shortmess' option, or
7544 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7545 */
7546 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007547shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007549 return p_shm != NULL &&
7550 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 || (vim_strchr(p_shm, 'a') != NULL
7552 && vim_strchr((char_u *)SHM_A, x) != NULL));
7553}
7554
7555/*
7556 * paste_option_changed() - Called after p_paste was set or reset.
7557 */
7558 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007559paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560{
7561 static int old_p_paste = FALSE;
7562 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007563 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565#ifdef FEAT_RIGHTLEFT
7566 static int save_ri = 0;
7567 static int save_hkmap = 0;
7568#endif
7569 buf_T *buf;
7570
7571 if (p_paste)
7572 {
7573 /*
7574 * Paste switched from off to on.
7575 * Save the current values, so they can be restored later.
7576 */
7577 if (!old_p_paste)
7578 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007579 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007580 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 {
7582 buf->b_p_tw_nopaste = buf->b_p_tw;
7583 buf->b_p_wm_nopaste = buf->b_p_wm;
7584 buf->b_p_sts_nopaste = buf->b_p_sts;
7585 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007586 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007587#ifdef FEAT_VARTABS
7588 if (buf->b_p_vsts_nopaste)
7589 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007590 buf->b_p_vsts_nopaste =
7591 buf->b_p_vsts && buf->b_p_vsts != empty_option
7592 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 }
7595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007596 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007598 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600#ifdef FEAT_RIGHTLEFT
7601 save_ri = p_ri;
7602 save_hkmap = p_hkmap;
7603#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007604 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007605 p_ai_nopaste = p_ai;
7606 p_et_nopaste = p_et;
7607 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 p_tw_nopaste = p_tw;
7609 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007610#ifdef FEAT_VARTABS
7611 if (p_vsts_nopaste)
7612 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007613 p_vsts_nopaste = p_vsts && p_vsts != empty_option
7614 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 }
7617
7618 /*
7619 * Always set the option values, also when 'paste' is set when it is
7620 * already on.
7621 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007622 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007623 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007625 buf->b_p_tw = 0; // textwidth is 0
7626 buf->b_p_wm = 0; // wrapmargin is 0
7627 buf->b_p_sts = 0; // softtabstop is 0
7628 buf->b_p_ai = 0; // no auto-indent
7629 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007630#ifdef FEAT_VARTABS
7631 if (buf->b_p_vsts)
7632 free_string_option(buf->b_p_vsts);
7633 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007634 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 }
7637
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007638 // set global options
7639 p_sm = 0; // no showmatch
7640 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007642 status_redraw_all(); // redraw to remove the ruler
7643 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007645 p_ri = 0; // no reverse insert
7646 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007648 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 p_tw = 0;
7650 p_wm = 0;
7651 p_sts = 0;
7652 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007653#ifdef FEAT_VARTABS
7654 if (p_vsts)
7655 free_string_option(p_vsts);
7656 p_vsts = empty_option;
7657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 }
7659
7660 /*
7661 * Paste switched from on to off: Restore saved values.
7662 */
7663 else if (old_p_paste)
7664 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007665 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007666 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {
7668 buf->b_p_tw = buf->b_p_tw_nopaste;
7669 buf->b_p_wm = buf->b_p_wm_nopaste;
7670 buf->b_p_sts = buf->b_p_sts_nopaste;
7671 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007672 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007673#ifdef FEAT_VARTABS
7674 if (buf->b_p_vsts)
7675 free_string_option(buf->b_p_vsts);
7676 buf->b_p_vsts = buf->b_p_vsts_nopaste
7677 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007678 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007679 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007680 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007681 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007682 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 }
7685
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007686 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007688 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007690 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692#ifdef FEAT_RIGHTLEFT
7693 p_ri = save_ri;
7694 p_hkmap = save_hkmap;
7695#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007696 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007697 p_ai = p_ai_nopaste;
7698 p_et = p_et_nopaste;
7699 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 p_tw = p_tw_nopaste;
7701 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007702#ifdef FEAT_VARTABS
7703 if (p_vsts)
7704 free_string_option(p_vsts);
7705 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 }
7708
7709 old_p_paste = p_paste;
7710}
7711
7712/*
7713 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7714 *
7715 * Reset 'compatible' and set the values for options that didn't get set yet
7716 * to the Vim defaults.
7717 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007718 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 */
7720 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007721vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007723 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007724 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007725 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726
7727 if (!option_was_set((char_u *)"cp"))
7728 {
7729 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007730 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7732 set_option_default(opt_idx, OPT_FREE, FALSE);
7733 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007734 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007736
7737 if (fname != NULL)
7738 {
7739 p = vim_getenv(envname, &dofree);
7740 if (p == NULL)
7741 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007742 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007743 p = FullName_save(fname, FALSE);
7744 if (p != NULL)
7745 {
7746 vim_setenv(envname, p);
7747 vim_free(p);
7748 }
7749 }
7750 else if (dofree)
7751 vim_free(p);
7752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753}
7754
7755/*
7756 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7757 */
7758 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007759change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007761 int opt_idx;
7762
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if (p_cp != on)
7764 {
7765 p_cp = on;
7766 compatible_set();
7767 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007768 opt_idx = findoption((char_u *)"cp");
7769 if (opt_idx >= 0)
7770 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771}
7772
7773/*
7774 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007775 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 */
7777 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007778option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780 int idx;
7781
7782 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007783 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 return FALSE;
7785 if (options[idx].flags & P_WAS_SET)
7786 return TRUE;
7787 return FALSE;
7788}
7789
7790/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007791 * Reset the flag indicating option "name" was set.
7792 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007793 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007794reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007795{
7796 int idx = findoption(name);
7797
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007798 if (idx < 0)
7799 return FAIL;
7800
7801 options[idx].flags &= ~P_WAS_SET;
7802 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007803}
7804
7805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 * compatible_set() - Called when 'compatible' has been set or unset.
7807 *
7808 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7809 * flag) to a Vi compatible value.
7810 * When 'compatible' is unset: Set all options that have a different default
7811 * for Vim (without the P_VI_DEF flag) to that default.
7812 */
7813 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007814compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815{
7816 int opt_idx;
7817
Bram Moolenaardac13472019-09-16 21:06:21 +02007818 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7820 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7821 set_option_default(opt_idx, OPT_FREE, p_cp);
7822 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007823 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824}
7825
Bram Moolenaardac13472019-09-16 21:06:21 +02007826#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828/*
7829 * fill_breakat_flags() -- called when 'breakat' changes value.
7830 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007831 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007832fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007834 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 int i;
7836
7837 for (i = 0; i < 256; i++)
7838 breakat_flags[i] = FALSE;
7839
7840 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007841 for (p = p_breakat; *p; p++)
7842 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844#endif
7845
7846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 * Check if backspacing over something is allowed.
7848 */
7849 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007850can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007851 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007853#ifdef FEAT_JOB_CHANNEL
7854 if (what == BS_START && bt_prompt(curbuf))
7855 return FALSE;
7856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 switch (*p_bs)
7858 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007859 case '3': return TRUE;
7860 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 case '1': return (what != BS_START);
7862 case '0': return FALSE;
7863 }
7864 return vim_strchr(p_bs, what) != NULL;
7865}
7866
7867/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007868 * Return the effective 'scrolloff' value for the current window, using the
7869 * global value when appropriate.
7870 */
7871 long
7872get_scrolloff_value(void)
7873{
7874 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7875}
7876
7877/*
7878 * Return the effective 'sidescrolloff' value for the current window, using the
7879 * global value when appropriate.
7880 */
7881 long
7882get_sidescrolloff_value(void)
7883{
7884 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7885}
7886
7887/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007888 * Get the local or global value of 'backupcopy'.
7889 */
7890 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007891get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007892{
7893 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7894}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007895
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007896#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007897/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007898 * Get the local or global value of 'formatlistpat'.
7899 */
7900 char_u *
7901get_flp_value(buf_T *buf)
7902{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007903 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7904 return p_flp;
7905 return buf->b_p_flp;
7906}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007907#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007908
7909/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007910 * Get the local or global value of the 'virtualedit' flags.
7911 */
7912 unsigned int
7913get_ve_flags(void)
7914{
Gary Johnson51ad8502021-08-03 18:33:08 +02007915 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7916 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007917}
7918
Bram Moolenaaree857022019-11-09 23:26:40 +01007919#if defined(FEAT_LINEBREAK) || defined(PROTO)
7920/*
7921 * Get the local or global value of 'showbreak'.
7922 */
7923 char_u *
7924get_showbreak_value(win_T *win)
7925{
7926 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7927 return p_sbr;
7928 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7929 return empty_option;
7930 return win->w_p_sbr;
7931}
7932#endif
7933
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007934#if defined(FEAT_EVAL) || defined(PROTO)
7935/*
7936 * Get window or buffer local options.
7937 */
7938 dict_T *
7939get_winbuf_options(int bufopt)
7940{
7941 dict_T *d;
7942 int opt_idx;
7943
7944 d = dict_alloc();
7945 if (d == NULL)
7946 return NULL;
7947
Bram Moolenaardac13472019-09-16 21:06:21 +02007948 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007949 {
7950 struct vimoption *opt = &options[opt_idx];
7951
7952 if ((bufopt && (opt->indir & PV_BUF))
7953 || (!bufopt && (opt->indir & PV_WIN)))
7954 {
7955 char_u *varp = get_varp(opt);
7956
7957 if (varp != NULL)
7958 {
7959 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007960 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007961 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007962 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007963 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007964 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007965 }
7966 }
7967 }
7968
7969 return d;
7970}
7971#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007972
Bram Moolenaardac13472019-09-16 21:06:21 +02007973#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007974/*
7975 * This is called when 'culopt' is changed
7976 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007977 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007978fill_culopt_flags(char_u *val, win_T *wp)
7979{
7980 char_u *p;
7981 char_u culopt_flags_new = 0;
7982
7983 if (val == NULL)
7984 p = wp->w_p_culopt;
7985 else
7986 p = val;
7987 while (*p != NUL)
7988 {
7989 if (STRNCMP(p, "line", 4) == 0)
7990 {
7991 p += 4;
7992 culopt_flags_new |= CULOPT_LINE;
7993 }
7994 else if (STRNCMP(p, "both", 4) == 0)
7995 {
7996 p += 4;
7997 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7998 }
7999 else if (STRNCMP(p, "number", 6) == 0)
8000 {
8001 p += 6;
8002 culopt_flags_new |= CULOPT_NBR;
8003 }
8004 else if (STRNCMP(p, "screenline", 10) == 0)
8005 {
8006 p += 10;
8007 culopt_flags_new |= CULOPT_SCRLINE;
8008 }
8009
8010 if (*p != ',' && *p != NUL)
8011 return FAIL;
8012 if (*p == ',')
8013 ++p;
8014 }
8015
8016 // Can't have both "line" and "screenline".
8017 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8018 return FAIL;
8019 wp->w_p_culopt_flags = culopt_flags_new;
8020
8021 return OK;
8022}
8023#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008024
8025/*
8026 * Get the value of 'magic' adjusted for Vim9 script.
8027 */
8028 int
8029magic_isset(void)
8030{
8031 switch (magic_overruled)
8032 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008033 case OPTION_MAGIC_ON: return TRUE;
8034 case OPTION_MAGIC_OFF: return FALSE;
8035 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008036 }
8037#ifdef FEAT_EVAL
8038 if (in_vim9script())
8039 return TRUE;
8040#endif
8041 return p_magic;
8042}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008043
8044/*
8045 * Set the callback function value for an option that accepts a function name,
8046 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8047 * Returns OK if the option is successfully set to a function, otherwise
8048 * returns FAIL.
8049 */
8050 int
8051option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8052{
8053#ifdef FEAT_EVAL
8054 typval_T *tv;
8055 callback_T cb;
8056
8057 if (optval == NULL || *optval == NUL)
8058 {
8059 free_callback(optcb);
8060 return OK;
8061 }
8062
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008063 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008064 || (STRNCMP(optval, "function(", 9) == 0)
8065 || (STRNCMP(optval, "funcref(", 8) == 0))
8066 // Lambda expression or a funcref
8067 tv = eval_expr(optval, NULL);
8068 else
8069 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008070 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008071 if (tv == NULL)
8072 return FAIL;
8073
8074 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008075 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008076 {
8077 free_tv(tv);
8078 return FAIL;
8079 }
8080
8081 free_callback(optcb);
8082 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008083 if (cb.cb_free_name)
8084 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008085 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008086
8087 // when using Vim9 style "import.funcname" it needs to be expanded to
8088 // "import#funcname".
8089 expand_autload_callback(optcb);
8090
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008091 return OK;
8092#else
8093 return FAIL;
8094#endif
8095}