blob: 6d860511560c6e64dd83b55fde2dc637d1287847 [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/*
2903 * Set the value of a boolean option, and take care of side effects.
2904 * Returns NULL for success, or an error message for an error.
2905 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002906 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002907set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 int opt_idx, // index in options[] table
2909 char_u *varp, // pointer to the option variable
2910 int value, // new value
2911 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912{
2913 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002914#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002915 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002916#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002917 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002919 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 if ((secure
2921#ifdef HAVE_SANDBOX
2922 || sandbox != 0
2923#endif
2924 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002925 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002927#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002928 // Save the global value before changing anything. This is needed as for
2929 // a global-only option setting the "local value" in fact sets the global
2930 // value (since there is only one value).
2931 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2932 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2933 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002934#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002936 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002938 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002939 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940#endif
2941
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002942#ifdef FEAT_GUI
2943 need_mouse_correct = TRUE;
2944#endif
2945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002946 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2948 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2949
2950 /*
2951 * Handle side effects of changing a bool option.
2952 */
2953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002954 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
Bram Moolenaar920694c2016-08-21 17:45:02 +02002958#ifdef FEAT_LANGMAP
2959 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002960 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002961 p_lnr = !p_lrm;
2962 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002963 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002964 p_lrm = !p_lnr;
2965#endif
2966
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002967#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002968 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002969 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2970 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002971 // Only take action when the option was set. When reset we do not
2972 // delete the undo file, the option may be set again without making
2973 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002974 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002975 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002976 char_u hash[UNDO_HASH_SIZE];
2977 buf_T *save_curbuf = curbuf;
2978
Bram Moolenaar29323592016-07-24 22:04:11 +02002979 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002980 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002981 // When 'undofile' is set globally: for every buffer, otherwise
2982 // only for the current buffer: Try to read in the undofile,
2983 // if one exists, the buffer wasn't changed and the buffer was
2984 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002985 if ((curbuf == save_curbuf
2986 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2987 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2988 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002989#ifdef FEAT_CRYPT
2990 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2991 continue;
2992#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002993 u_compute_hash(hash);
2994 u_read_undo(NULL, hash, curbuf->b_fname);
2995 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002996 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002997 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002998 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002999 }
3000#endif
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 else if ((int *)varp == &curbuf->b_p_ro)
3003 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003004 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
3006 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003008 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003009 if (curbuf->b_p_ro)
3010 curbuf->b_did_warn = FALSE;
3011
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003012 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
3014
Bram Moolenaar9c449722010-07-20 18:44:27 +02003015#ifdef FEAT_GUI
3016 else if ((int *)varp == &p_mh)
3017 {
3018 if (!p_mh)
3019 gui_mch_mousehide(FALSE);
3020 }
3021#endif
3022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003023 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003025 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02003026# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003027 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01003028 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3029 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02003030 {
3031 curbuf->b_p_ma = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003032 return e_cannot_make_terminal_with_running_job_modifiable;
Bram Moolenaar423802d2017-07-30 16:52:24 +02003033 }
3034# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003035 redraw_titles();
3036 }
K.Takata845bbb72022-11-05 18:31:19 +00003037 // redraw the window title and tab page text when 'endoffile', 'endofline',
3038 // 'fixeol' or 'bomb' is changed
3039 else if ((int *)varp == &curbuf->b_p_eof
3040 || (int *)varp == &curbuf->b_p_eol
3041 || (int *)varp == &curbuf->b_p_fixeol
3042 || (int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003043 {
3044 redraw_titles();
3045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003047 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 else if ((int *)varp == &curbuf->b_p_bin)
3049 {
3050 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003051 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 }
3053
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003054 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
3056 {
3057 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3058 NULL, NULL, TRUE, curbuf);
3059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003061 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 else if ((int *)varp == &curbuf->b_p_swf)
3063 {
3064 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003065 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003067 // no need to reset curbuf->b_may_swap, ml_open_file() will check
3068 // buf->b_p_swf
3069 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 }
3071
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003072 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 else if ((int *)varp == &p_terse)
3074 {
3075 char_u *p;
3076
3077 p = vim_strchr(p_shm, SHM_SEARCH);
3078
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003079 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 if (p_terse && p == NULL)
3081 {
3082 STRCPY(IObuff, p_shm);
3083 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003084 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003086 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003088 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 }
3090
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003091 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 else if ((int *)varp == &p_paste)
3093 {
3094 paste_option_changed();
3095 }
3096
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003097 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 else if ((int *)varp == &p_im)
3099 {
3100 if (p_im)
3101 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003102 if ((State & MODE_INSERT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 need_start_insertmode = TRUE;
3104 stop_insert_mode = FALSE;
3105 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003106 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02003107 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 {
3109 need_start_insertmode = FALSE;
3110 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003111 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003112 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 restart_edit = 0;
3114 }
3115 }
3116
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003117 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 else if ((int *)varp == &p_ic && p_hls)
3119 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003120 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 }
3122
3123#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003124 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 else if ((int *)varp == &p_hls)
3126 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02003127 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 }
3129#endif
3130
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003131 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3132 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 else if ((int *)varp == &curwin->w_p_scb)
3134 {
3135 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02003136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02003138 curwin->w_scbind_pos = curwin->w_topline;
3139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
Bram Moolenaar4033c552017-09-16 20:54:51 +02003142#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003143 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 else if ((int *)varp == &curwin->w_p_pvw)
3145 {
3146 if (curwin->w_p_pvw)
3147 {
3148 win_T *win;
3149
Bram Moolenaar29323592016-07-24 22:04:11 +02003150 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 if (win->w_p_pvw && win != curwin)
3152 {
3153 curwin->w_p_pvw = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003154 return e_preview_window_already_exists;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 }
3156 }
3157 }
3158#endif
3159
Bram Moolenaarf6196f42022-10-02 21:29:55 +01003160 else if ((int *)varp == &curwin->w_p_sms)
3161 {
3162 if (!curwin->w_p_sms)
3163 {
3164 curwin->w_skipcol = 0;
3165 changed_line_abv_curs();
3166 }
3167 }
3168
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003169 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 else if ((int *)varp == &curbuf->b_p_tx)
3171 {
3172 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
3173 }
3174
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003175 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 set_string_option_direct((char_u *)"ffs", -1,
3179 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003180 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
3183 /*
3184 * When 'lisp' option changes include/exclude '-' in
3185 * keyword characters.
3186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 else if (varp == (char_u *)&(curbuf->b_p_lisp))
3188 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003189 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003192 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02003193 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003195 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
3198 else if ((int *)varp == &curbuf->b_changed)
3199 {
3200 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003201 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003202 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
3205
3206#ifdef BACKSLASH_IN_FILENAME
3207 else if ((int *)varp == &p_ssl)
3208 {
3209 if (p_ssl)
3210 {
3211 psepc = '/';
3212 psepcN = '\\';
3213 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 }
3215 else
3216 {
3217 psepc = '\\';
3218 psepcN = '/';
3219 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 }
3221
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003222 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 buflist_slash_adjust();
3224 alist_slash_adjust();
3225# ifdef FEAT_EVAL
3226 scriptnames_slash_adjust();
3227# endif
3228 }
3229#endif
3230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003231 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 else if ((int *)varp == &curwin->w_p_wrap)
3233 {
3234 if (curwin->w_p_wrap)
3235 curwin->w_leftcol = 0;
3236 }
3237
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 else if ((int *)varp == &p_ea)
3239 {
3240 if (p_ea && !old_value)
3241 win_equal(curwin, FALSE, 0);
3242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
3244 else if ((int *)varp == &p_wiv)
3245 {
3246 /*
3247 * When 'weirdinvert' changed, set/reset 't_xs'.
3248 * Then set 'weirdinvert' according to value of 't_xs'.
3249 */
3250 if (p_wiv && !old_value)
3251 T_XS = (char_u *)"y";
3252 else if (!p_wiv && old_value)
3253 T_XS = empty_option;
3254 p_wiv = (*T_XS != NUL);
3255 }
3256
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003257#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 else if ((int *)varp == &p_beval)
3259 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003260 if (!balloonEvalForTerm)
3261 {
3262 if (p_beval && !old_value)
3263 gui_mch_enable_beval_area(balloonEval);
3264 else if (!p_beval && old_value)
3265 gui_mch_disable_beval_area(balloonEval);
3266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003268#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003269#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003270 else if ((int *)varp == &p_bevalterm)
3271 {
3272 mch_bevalterm_changed();
3273 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003276#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 else if ((int *)varp == &p_acd)
3278 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003279 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003280 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282#endif
3283
3284#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003285 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 else if ((int *)varp == &curwin->w_p_diff)
3287 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003288 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003289 diff_buf_adjust(curwin);
3290# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (foldmethodIsDiff(curwin))
3292 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
3295#endif
3296
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003297#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003298 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 else if ((int *)varp == &p_imdisable)
3300 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003301 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 if (p_imdisable)
3303 im_set_active(FALSE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003304 else if (State & MODE_INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003305 // When the option is set from an autocommand, it may need to take
3306 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003307 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
3309#endif
3310
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003311#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003312 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003313 else if ((int *)varp == &curwin->w_p_spell)
3314 {
3315 if (curwin->w_p_spell)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003316 errmsg = did_set_spelllang(curwin);
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003317 }
3318#endif
3319
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#ifdef FEAT_ARABIC
3321 if ((int *)varp == &curwin->w_p_arab)
3322 {
3323 if (curwin->w_p_arab)
3324 {
3325 /*
3326 * 'arabic' is set, handle various sub-settings.
3327 */
3328 if (!p_tbidi)
3329 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003330 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (!curwin->w_p_rl)
3332 {
3333 curwin->w_p_rl = TRUE;
3334 changed_window_setting();
3335 }
3336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003337 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 if (!p_arshape)
3339 {
3340 p_arshape = TRUE;
3341 redraw_later_clear();
3342 }
3343 }
3344
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003345 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003346 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003348 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003349 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3350
Bram Moolenaar8820b482017-03-16 17:23:31 +01003351 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003352 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003353#ifdef FEAT_EVAL
3354 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3355#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003358 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
3361# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003362 // Force-set the necessary keymap for arabic
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003363 errmsg = set_option_value((char_u *)"keymap",
3364 0L, (char_u *)"arabic", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
3367 else
3368 {
3369 /*
3370 * 'arabic' is reset, handle various sub-settings.
3371 */
3372 if (!p_tbidi)
3373 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003374 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (curwin->w_p_rl)
3376 {
3377 curwin->w_p_rl = FALSE;
3378 changed_window_setting();
3379 }
3380
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003381 // 'arabicshape' isn't reset, it is a global option and
3382 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003385 // 'delcombine' isn't reset, it is a global option and another
3386 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
3388# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003389 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 curbuf->b_p_iminsert = B_IMODE_NONE;
3391 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3392# endif
3393 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003394 }
3395
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003396#endif
3397
Bram Moolenaar44826212019-08-22 21:23:20 +02003398#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3399 else if (((int *)varp == &curwin->w_p_nu
3400 || (int *)varp == &curwin->w_p_rnu)
3401 && gui.in_use
3402 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3403 && curbuf->b_signlist != NULL)
3404 {
3405 // If the 'number' or 'relativenumber' options are modified and
3406 // 'signcolumn' is set to 'number', then clear the screen for a full
3407 // refresh. Otherwise the sign icons are not displayed properly in the
3408 // number column. If the 'number' option is set and only the
3409 // 'relativenumber' option is toggled, then don't refresh the screen
3410 // (optimization).
3411 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003412 redraw_all_later(UPD_CLEAR);
Bram Moolenaar44826212019-08-22 21:23:20 +02003413 }
3414#endif
3415
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003416#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003417 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003418 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003419 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003420# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003421 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003422 if (
3423# ifdef VIMDLL
3424 !gui.in_use && !gui.starting &&
3425# endif
3426 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003427 {
3428 p_tgc = 0;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003429 return e_24_bit_colors_are_not_supported_on_this_environment;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003430 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003431 if (is_term_win32())
3432 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003433# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003434# ifdef FEAT_GUI
3435 if (!gui.in_use && !gui.starting)
3436# endif
3437 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003438# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003439 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003440 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003441 {
3442 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003443 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003444 init_highlight(TRUE, FALSE);
3445 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003446# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003447# ifdef FEAT_TERMINAL
3448 term_update_colors_all();
LemonBoyb2b3acb2022-05-20 10:10:34 +01003449 term_update_palette_all();
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003450 term_update_wincolor_all();
3451# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003452 }
3453#endif
3454
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 /*
3456 * End of handling side effects for bool options.
3457 */
3458
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003459 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003460
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 options[opt_idx].flags |= P_WAS_SET;
3462
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003463#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003464 apply_optionset_autocmd(opt_idx, opt_flags,
3465 (long)(old_value ? TRUE : FALSE),
3466 (long)(old_global_value ? TRUE : FALSE),
3467 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003468#endif
3469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003470 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003471 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003472 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003473 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003474
3475 if ((opt_flags & OPT_NO_REDRAW) == 0)
3476 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003478 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479}
3480
3481/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003482 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003484 static char *
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003485did_set_winheight_helpheight(long *pp, char *errmsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003487 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003489 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003490 p_wh = 1;
3491 }
3492 if (p_wmh > p_wh)
3493 {
3494 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
3495 p_wh = p_wmh;
3496 }
3497 if (p_hh < 0)
3498 {
3499 errmsg = e_argument_must_be_positive;
3500 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003503 // Change window height NOW
3504 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003506 if (pp == &p_wh && curwin->w_height < p_wh)
3507 win_setheight((int)p_wh);
3508 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3509 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003512 return errmsg;
3513}
3514
3515/*
3516 * Process the new 'winminheight' option value.
3517 */
3518 static char *
3519did_set_winminheight(char *errmsg)
3520{
3521 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003523 errmsg = e_argument_must_be_positive;
3524 p_wmh = 0;
3525 }
3526 if (p_wmh > p_wh)
3527 {
3528 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
3529 p_wmh = p_wh;
3530 }
3531 win_setminheight();
3532
3533 return errmsg;
3534}
3535
3536/*
3537 * Process the new 'winwidth' option value.
3538 */
3539 static char *
3540did_set_winwidth(char *errmsg)
3541{
3542 if (p_wiw < 1)
3543 {
3544 errmsg = e_argument_must_be_positive;
3545 p_wiw = 1;
3546 }
3547 if (p_wmw > p_wiw)
3548 {
3549 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
3550 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003552
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003553 // Change window width NOW
3554 if (!ONE_WINDOW && curwin->w_width < p_wiw)
3555 win_setwidth((int)p_wiw);
3556
3557 return errmsg;
3558}
3559
3560/*
3561 * Process the new 'winminwidth' option value.
3562 */
3563 static char *
3564did_set_winminwidth(char *errmsg)
3565{
3566 if (p_wmw < 0)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003567 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003568 errmsg = e_argument_must_be_positive;
3569 p_wmw = 0;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003570 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003571 if (p_wmw > p_wiw)
3572 {
3573 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
3574 p_wmw = p_wiw;
3575 }
3576 win_setminwidth();
3577
3578 return errmsg;
3579}
3580
3581/*
3582 * Process the new 'laststatus' option value.
3583 */
3584 static void
3585did_set_laststatus(void)
3586{
3587 last_status(FALSE); // (re)set last window status line
3588}
3589
3590/*
3591 * Process the new 'showtabline' option value.
3592 */
3593 static void
3594did_set_showtabline(void)
3595{
3596 shell_new_rows(); // recompute window positions and heights
3597}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598
3599#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003600/*
3601 * Process the new 'linespace' option value.
3602 */
3603 static void
3604did_set_linespace(void)
3605{
3606 // Recompute gui.char_height and resize the Vim window to keep the
3607 // same number of lines.
3608 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3609 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3610}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611#endif
3612
3613#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003614/*
3615 * Process the new 'foldlevel' option value.
3616 */
3617 static void
3618did_set_foldlevel(void)
3619{
3620 if (curwin->w_p_fdl < 0)
3621 curwin->w_p_fdl = 0;
3622 newFoldLevel();
3623}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003625/*
3626 * Process the new 'foldminlines' option value.
3627 */
3628 static void
3629did_set_foldminlines(void)
3630{
3631 foldUpdateAll(curwin);
3632}
3633
3634/*
3635 * Process the new 'foldnestmax' option value.
3636 */
3637 static void
3638did_set_foldnestmax(void)
3639{
3640 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 foldUpdateAll(curwin);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003642}
3643
3644/*
3645 * Process the new 'foldcolumn' option value.
3646 */
3647 static char *
3648did_set_foldcolumn(char *errmsg)
3649{
3650 if (curwin->w_p_fdc < 0)
3651 {
3652 errmsg = e_argument_must_be_positive;
3653 curwin->w_p_fdc = 0;
3654 }
3655 else if (curwin->w_p_fdc > 12)
3656 {
3657 errmsg = e_invalid_argument;
3658 curwin->w_p_fdc = 12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
3660
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003661 return errmsg;
3662}
3663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003665/*
3666 * Process the new 'shiftwidth' or the 'tabstop' option value.
3667 */
3668 static void
3669did_set_shiftwidth_tabstop(long *pp)
3670{
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003671#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003672 if (foldmethodIsIndent(curwin))
3673 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003674#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003675 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3676 // parse 'cinoptions'.
3677 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3678 parse_cino(curbuf);
3679}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003681/*
3682 * Process the new 'maxcombine' option value.
3683 */
3684 static void
3685did_set_maxcombine(void)
3686{
3687 if (p_mco > MAX_MCO)
3688 p_mco = MAX_MCO;
3689 else if (p_mco < 0)
3690 p_mco = 0;
3691 screenclear(); // will re-allocate the screen
3692}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003693
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003694/*
3695 * Process the new 'iminsert' option value.
3696 */
3697 static char *
3698did_set_iminsert(char *errmsg)
3699{
3700 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003702 errmsg = e_invalid_argument;
3703 curbuf->b_p_iminsert = B_IMODE_NONE;
3704 }
3705 p_iminsert = curbuf->b_p_iminsert;
3706 if (termcap_active) // don't do this in the alternate screen
3707 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003708#if defined(FEAT_KEYMAP)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003709 // Show/unshow value of 'keymap' in status lines.
3710 status_redraw_curbuf();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003712
3713 return errmsg;
3714}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003716#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003717/*
3718 * Process the new 'imstyle' option value.
3719 */
3720 static char *
3721did_set_imstyle(char *errmsg)
3722{
3723 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3724 errmsg = e_invalid_argument;
3725
3726 return errmsg;
3727}
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003728#endif
3729
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003730/*
3731 * Process the new 'window' option value.
3732 */
3733 static void
3734did_set_window(void)
3735{
3736 if (p_window < 1)
3737 p_window = 1;
3738 else if (p_window >= Rows)
3739 p_window = Rows - 1;
3740}
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003741
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003742/*
3743 * Process the new 'imsearch' option value.
3744 */
3745 static char *
3746did_set_imsearch(char *errmsg)
3747{
3748 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003750 errmsg = e_invalid_argument;
3751 curbuf->b_p_imsearch = B_IMODE_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003753 p_imsearch = curbuf->b_p_imsearch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003755 return errmsg;
3756}
3757
3758/*
3759 * Process the new 'titlelen' option value.
3760 */
3761 static char *
3762did_set_titlelen(long old_value, char *errmsg)
3763{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003764 // if 'titlelen' has changed, redraw the title
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003765 if (p_titlelen < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003767 errmsg = e_argument_must_be_positive;
3768 p_titlelen = 85;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003770 if (starting != NO_SCREEN && old_value != p_titlelen)
3771 need_maketitle = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003773 return errmsg;
3774}
3775
3776/*
3777 * Process the new 'cmdheight' option value.
3778 */
3779 static char *
3780did_set_cmdheight(long old_value, char *errmsg)
3781{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003782 // if p_ch changed value, change the command line height
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003783 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003785 errmsg = e_argument_must_be_positive;
3786 p_ch = 1;
3787 }
3788 if (p_ch > Rows - min_rows() + 1)
3789 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003791 // Only compute the new window layout when startup has been
3792 // completed. Otherwise the frame sizes may be wrong.
3793 if ((p_ch != old_value
3794 || tabline_height() + topframe->fr_height != Rows - p_ch)
3795 && full_screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003797 && !gui.starting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003799 )
3800 command_height();
3801
3802 return errmsg;
3803}
3804
3805/*
3806 * Process the new 'updatecount' option value.
3807 */
3808 static char *
3809did_set_updatecount(long old_value, char *errmsg)
3810{
3811 // when 'updatecount' changes from zero to non-zero, open swap files
3812 if (p_uc < 0)
3813 {
3814 errmsg = e_argument_must_be_positive;
3815 p_uc = 100;
3816 }
3817 if (p_uc && !old_value)
3818 ml_open_files();
3819
3820 return errmsg;
3821}
3822
3823#ifdef FEAT_CONCEAL
3824/*
3825 * Process the new 'conceallevel' option value.
3826 */
3827 static char *
3828did_set_conceallevel(char *errmsg)
3829{
3830 if (curwin->w_p_cole < 0)
3831 {
3832 errmsg = e_argument_must_be_positive;
3833 curwin->w_p_cole = 0;
3834 }
3835 else if (curwin->w_p_cole > 3)
3836 {
3837 errmsg = e_invalid_argument;
3838 curwin->w_p_cole = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003841 return errmsg;
3842}
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003845#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003846/*
3847 * Process the new 'pyxversion' option value.
3848 */
3849 static char *
3850did_set_pyxversion(char *errmsg)
3851{
3852 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3853 errmsg = e_invalid_argument;
3854
3855 return errmsg;
3856}
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003857#endif
3858
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003859/*
3860 * Process the new global 'undolevels' option value.
3861 */
3862 static void
3863did_set_global_undolevels(long value, long old_value)
3864{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003865 // sync undo before 'undolevels' changes
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003866
3867 // use the old value, otherwise u_sync() may not work properly
3868 p_ul = old_value;
3869 u_sync(TRUE);
3870 p_ul = value;
3871}
3872
3873/*
3874 * Process the new buffer local 'undolevels' option value.
3875 */
3876 static void
3877did_set_buflocal_undolevels(long value, long old_value)
3878{
3879 // use the old value, otherwise u_sync() may not work properly
3880 curbuf->b_p_ul = old_value;
3881 u_sync(TRUE);
3882 curbuf->b_p_ul = value;
3883}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003885#ifdef FEAT_LINEBREAK
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003886/*
3887 * Process the new 'numberwidth' option value.
3888 */
3889 static char *
3890did_set_numberwidth(char *errmsg)
3891{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003892 // 'numberwidth' must be positive
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003893 if (curwin->w_p_nuw < 1)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003894 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003895 errmsg = e_argument_must_be_positive;
3896 curwin->w_p_nuw = 1;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003897 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003898 if (curwin->w_p_nuw > 20)
3899 {
3900 errmsg = e_invalid_argument;
3901 curwin->w_p_nuw = 20;
3902 }
3903 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3904
3905 return errmsg;
3906}
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003907#endif
3908
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003909/*
3910 * Process the new 'textwidth' option value.
3911 */
3912 static char *
3913did_set_textwidth(char *errmsg)
3914{
3915 if (curbuf->b_p_tw < 0)
Bram Moolenaar1a384422010-07-14 19:53:30 +02003916 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003917 errmsg = e_argument_must_be_positive;
3918 curbuf->b_p_tw = 0;
3919 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003920#ifdef FEAT_SYN_HL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003921 {
3922 win_T *wp;
3923 tabpage_T *tp;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003924
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003925 FOR_ALL_TAB_WINDOWS(tp, wp)
3926 check_colorcolumn(wp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02003927 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003928#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02003929
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003930 return errmsg;
3931}
3932
3933/*
3934 * When some number options are changed, need to take some action.
3935 */
3936 static char *
3937did_set_num_option(long *pp, long value, long old_value, char *errmsg)
3938{
3939 if (pp == &p_wh // 'winheight'
3940 || pp == &p_hh) // 'helpheight'
3941 errmsg = did_set_winheight_helpheight(pp, errmsg);
3942 else if (pp == &p_wmh) // 'winminheight'
3943 errmsg = did_set_winminheight(errmsg);
3944 else if (pp == &p_wiw) // 'winwidth'
3945 errmsg = did_set_winwidth(errmsg);
3946 else if (pp == &p_wmw) // 'winminwidth'
3947 errmsg = did_set_winminwidth(errmsg);
3948 else if (pp == &p_ls)
3949 did_set_laststatus(); // 'laststatus'
3950 else if (pp == &p_stal)
3951 did_set_showtabline(); // 'showtabline'
3952#ifdef FEAT_GUI
3953 else if (pp == &p_linespace) // 'linespace'
3954 did_set_linespace();
3955#endif
3956#ifdef FEAT_FOLDING
3957 else if (pp == &curwin->w_p_fdl) // 'foldlevel'
3958 did_set_foldlevel();
3959 else if (pp == &curwin->w_p_fml) // 'foldminlines'
3960 did_set_foldminlines();
3961 else if (pp == &curwin->w_p_fdn) // 'foldnestmax'
3962 did_set_foldnestmax();
3963 else if (pp == &curwin->w_p_fdc) // 'foldcolumn'
3964 errmsg = did_set_foldcolumn(errmsg);
3965#endif // FEAT_FOLDING
3966 else if ( pp == &curbuf->b_p_sw // 'shiftwidth'
3967 || pp == &curbuf->b_p_ts) // 'tabstop'
3968 did_set_shiftwidth_tabstop(pp);
3969 else if (pp == &p_mco) // 'maxcombine'
3970 did_set_maxcombine();
3971 else if (pp == &curbuf->b_p_iminsert) // 'iminsert'
3972 errmsg = did_set_iminsert(errmsg);
3973#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3974 else if (pp == &p_imst) // 'imstyle'
3975 errmsg = did_set_imstyle(errmsg);
3976#endif
3977 else if (pp == &p_window) // 'window'
3978 did_set_window();
3979 else if (pp == &curbuf->b_p_imsearch) // 'imsearch'
3980 errmsg = did_set_imsearch(errmsg);
3981 else if (pp == &p_titlelen) // 'titlelen'
3982 errmsg = did_set_titlelen(old_value, errmsg);
3983 else if (pp == &p_ch) // 'cmdheight'
3984 errmsg = did_set_cmdheight(old_value, errmsg);
3985 else if (pp == &p_uc) // 'updatecount'
3986 errmsg = did_set_updatecount(old_value, errmsg);
3987#ifdef FEAT_CONCEAL
3988 else if (pp == &curwin->w_p_cole) // 'conceallevel'
3989 errmsg = did_set_conceallevel(errmsg);
3990#endif
3991#ifdef MZSCHEME_GUI_THREADS
3992 else if (pp == &p_mzq) // 'mzquantum'
3993 mzvim_reset_timer();
3994#endif
3995#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
3996 else if (pp == &p_pyx) // 'pyxversion'
3997 errmsg = did_set_pyxversion(errmsg);
3998#endif
3999 else if (pp == &p_ul) // global 'undolevels'
4000 did_set_global_undolevels(value, old_value);
4001 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4002 did_set_buflocal_undolevels(value, old_value);
4003#ifdef FEAT_LINEBREAK
4004 else if (pp == &curwin->w_p_nuw) // 'numberwidth'
4005 errmsg = did_set_numberwidth(errmsg);
4006#endif
4007 else if (pp == &curbuf->b_p_tw) // 'textwidth'
4008 errmsg = did_set_textwidth(errmsg);
4009
4010 return errmsg;
4011}
4012
4013/*
4014 * Check the bounds of numeric options.
4015 */
4016 static char *
4017check_num_option_bounds(
4018 long *pp,
4019 long old_value,
4020 long old_Rows,
4021 long old_Columns,
4022 char *errbuf,
4023 size_t errbuflen,
4024 char *errmsg)
4025{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 if (Rows < min_rows() && full_screen)
4027 {
4028 if (errbuf != NULL)
4029 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004030 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004031 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 errmsg = errbuf;
4033 }
4034 Rows = min_rows();
4035 }
4036 if (Columns < MIN_COLUMNS && full_screen)
4037 {
4038 if (errbuf != NULL)
4039 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004040 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004041 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 errmsg = errbuf;
4043 }
4044 Columns = MIN_COLUMNS;
4045 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004046 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 /*
4049 * If the screen (shell) height has been changed, assume it is the
4050 * physical screenheight.
4051 */
4052 if (old_Rows != Rows || old_Columns != Columns)
4053 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004054 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 if (updating_screen)
4056 *pp = old_value;
4057 else if (full_screen
4058#ifdef FEAT_GUI
4059 && !gui.starting
4060#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004061 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 set_shellsize((int)Columns, (int)Rows, TRUE);
4063 else
4064 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004065 // Postpone the resizing; check the size and cmdline position for
4066 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 check_shellsize();
4068 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4069 cmdline_row = Rows - p_ch;
4070 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004071 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004072 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 if (curbuf->b_p_ts <= 0)
4076 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004077 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 curbuf->b_p_ts = 8;
4079 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004080 else if (curbuf->b_p_ts > TABSTOP_MAX)
4081 {
4082 errmsg = e_invalid_argument;
4083 curbuf->b_p_ts = 8;
4084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 if (p_tm < 0)
4086 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004087 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 p_tm = 0;
4089 }
4090 if ((curwin->w_p_scr <= 0
4091 || (curwin->w_p_scr > curwin->w_height
4092 && curwin->w_height > 0))
4093 && full_screen)
4094 {
4095 if (pp == &(curwin->w_p_scr))
4096 {
4097 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004098 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 win_comp_scroll(curwin);
4100 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004101 // If 'scroll' became invalid because of a side effect silently adjust
4102 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 else if (curwin->w_p_scr <= 0)
4104 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004105 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 curwin->w_p_scr = curwin->w_height;
4107 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004108 if (p_hi < 0)
4109 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004110 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004111 p_hi = 0;
4112 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004113 else if (p_hi > 10000)
4114 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004115 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004116 p_hi = 10000;
4117 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004118 if (p_re < 0 || p_re > 2)
4119 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004120 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004121 p_re = 0;
4122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (p_report < 0)
4124 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004125 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 p_report = 1;
4127 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004128 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004130 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 p_sj = Rows / 2;
4132 else
4133 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004134 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 p_sj = 1;
4136 }
4137 }
4138 if (p_so < 0 && full_screen)
4139 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004140 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 p_so = 0;
4142 }
4143 if (p_siso < 0 && full_screen)
4144 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004145 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 p_siso = 0;
4147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 if (p_cwh < 1)
4149 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004150 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 p_cwh = 1;
4152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 if (p_ut < 0)
4154 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004155 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 p_ut = 2000;
4157 }
4158 if (p_ss < 0)
4159 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004160 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 p_ss = 0;
4162 }
4163
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004164 return errmsg;
4165}
4166
4167/*
4168 * Set the value of a number option, and take care of side effects.
4169 * Returns NULL for success, or an error message for an error.
4170 */
4171 static char *
4172set_num_option(
4173 int opt_idx, // index in options[] table
4174 char_u *varp, // pointer to the option variable
4175 long value, // new value
4176 char *errbuf, // buffer for error messages
4177 size_t errbuflen, // length of "errbuf"
4178 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4179 // OPT_MODELINE, etc.
4180{
4181 char *errmsg = NULL;
4182 long old_value = *(long *)varp;
4183#if defined(FEAT_EVAL)
4184 long old_global_value = 0; // only used when setting a local and
4185 // global option
4186#endif
4187 long old_Rows = Rows; // remember old Rows
4188 long old_Columns = Columns; // remember old Columns
4189 long *pp = (long *)varp;
4190
4191 // Disallow changing some options from secure mode.
4192 if ((secure
4193#ifdef HAVE_SANDBOX
4194 || sandbox != 0
4195#endif
4196 ) && (options[opt_idx].flags & P_SECURE))
4197 return e_not_allowed_here;
4198
4199#if defined(FEAT_EVAL)
4200 // Save the global value before changing anything. This is needed as for
4201 // a global-only option setting the "local value" in fact sets the global
4202 // value (since there is only one value).
4203 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4204 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4205 OPT_GLOBAL);
4206#endif
4207
4208 *pp = value;
4209#ifdef FEAT_EVAL
4210 // Remember where the option was set.
4211 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4212#endif
4213#ifdef FEAT_GUI
4214 need_mouse_correct = TRUE;
4215#endif
4216
4217 if (curbuf->b_p_sw < 0)
4218 {
4219 errmsg = e_argument_must_be_positive;
4220#ifdef FEAT_VARTABS
4221 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4222 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4223 ? tabstop_first(curbuf->b_p_vts_array)
4224 : curbuf->b_p_ts;
4225#else
4226 curbuf->b_p_sw = curbuf->b_p_ts;
4227#endif
4228 }
4229
4230 /*
4231 * Number options that need some action when changed
4232 */
4233 errmsg = did_set_num_option(pp, value, old_value, errmsg);
4234
4235 /*
4236 * Check the bounds for numeric options here
4237 */
4238 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4239 errbuf, errbuflen, errmsg);
4240
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004241 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4243 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4244
4245 options[opt_idx].flags |= P_WAS_SET;
4246
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004247#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004248 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4249 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004250#endif
4251
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004252 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004253 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004254 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004255 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004256 if ((opt_flags & OPT_NO_REDRAW) == 0)
4257 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 return errmsg;
4260}
4261
4262/*
4263 * Called after an option changed: check if something needs to be redrawn.
4264 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004265 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004266check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004268 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004269 int doclear = (flags & P_RCLR) == P_RCLR;
4270 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004272 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
4275 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4276 changed_window_setting();
4277 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004278 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004279 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004280 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004281 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004282 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004284 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285}
4286
4287/*
4288 * Find index for option 'arg'.
4289 * Return -1 if not found.
4290 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004291 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004292findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293{
4294 int opt_idx;
4295 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004296 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 int is_term_opt;
4298
4299 /*
4300 * For first call: Initialize the quick-access table.
4301 * It contains the index for the first option that starts with a certain
4302 * letter. There are 26 letters, plus the first "t_" option.
4303 */
4304 if (quick_tab[1] == 0)
4305 {
4306 p = options[0].fullname;
4307 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4308 {
4309 if (s[0] != p[0])
4310 {
4311 if (s[0] == 't' && s[1] == '_')
4312 quick_tab[26] = opt_idx;
4313 else
4314 quick_tab[CharOrdLow(s[0])] = opt_idx;
4315 }
4316 p = s;
4317 }
4318 }
4319
4320 /*
4321 * Check for name starting with an illegal character.
4322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 return -1;
4325
4326 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4327 if (is_term_opt)
4328 opt_idx = quick_tab[26];
4329 else
4330 opt_idx = quick_tab[CharOrdLow(arg[0])];
4331 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4332 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004333 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 break;
4335 }
4336 if (s == NULL && !is_term_opt)
4337 {
4338 opt_idx = quick_tab[CharOrdLow(arg[0])];
4339 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4340 {
4341 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004342 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 break;
4344 s = NULL;
4345 }
4346 }
4347 if (s == NULL)
4348 opt_idx = -1;
4349 return opt_idx;
4350}
4351
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004352#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4353 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354/*
4355 * Get the value for an option.
4356 *
4357 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004358 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004359 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004360 * String option: gov_string, *stringval gets allocated string.
4361 * Hidden Number option: gov_hidden_number.
4362 * Hidden Toggle option: gov_hidden_bool.
4363 * Hidden String option: gov_hidden_string.
4364 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004365 *
4366 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004368 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004369get_option_value(
4370 char_u *name,
4371 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004372 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004373 int *flagsp,
4374 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375{
4376 int opt_idx;
4377 char_u *varp;
4378
4379 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004380 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004381 {
4382 int key;
4383
4384 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004385 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004386 {
4387 char_u key_name[2];
4388 char_u *p;
4389
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004390 if (flagsp != NULL)
4391 *flagsp = 0; // terminal option has no flags
4392
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004393 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004394 if (key < 0)
4395 {
4396 key_name[0] = KEY2TERMCAP0(key);
4397 key_name[1] = KEY2TERMCAP1(key);
4398 }
4399 else
4400 {
4401 key_name[0] = KS_KEY;
4402 key_name[1] = (key & 0xff);
4403 }
4404 p = find_termcode(key_name);
4405 if (p != NULL)
4406 {
4407 if (stringval != NULL)
4408 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004409 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004410 }
4411 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004412 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004415 varp = get_varp_scope(&(options[opt_idx]), scope);
4416
4417 if (flagsp != NULL)
4418 // Return the P_xxxx option flags.
4419 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
4421 if (options[opt_idx].flags & P_STRING)
4422 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004423 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004424 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 if (stringval != NULL)
4426 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004427 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004428 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4429 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004431 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004432 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004433 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004436 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 *stringval = vim_strsave(*(char_u **)(varp));
4438 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004439 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004442 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004443 return (options[opt_idx].flags & P_NUM)
4444 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 if (options[opt_idx].flags & P_NUM)
4446 *numval = *(long *)varp;
4447 else
4448 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004449 // Special case: 'modified' is b_changed, but we also want to consider
4450 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 if ((int *)varp == &curbuf->b_changed)
4452 *numval = curbufIsChanged();
4453 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004454 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004456 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457}
4458#endif
4459
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004460#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004461/*
4462 * Returns the option attributes and its value. Unlike the above function it
4463 * will return either global value or local value of the option depending on
4464 * what was requested, but it will never return global value if it was
4465 * requested to return local one and vice versa. Neither it will return
4466 * buffer-local value if it was requested to return window-local one.
4467 *
4468 * Pretends that option is absent if it is not present in the requested scope
4469 * (i.e. has no global, window-local or buffer-local value depending on
4470 * opt_type). Uses
4471 *
4472 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004473 * 0 hidden or unknown option, also option that does not have requested
4474 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004475 * see SOPT_* in vim.h for other flags
4476 *
4477 * Possible opt_type values: see SREQ_* in vim.h
4478 */
4479 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004480get_option_value_strict(
4481 char_u *name,
4482 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004483 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004484 int opt_type,
4485 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004486{
4487 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004488 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004489 struct vimoption *p;
4490 int r = 0;
4491
4492 opt_idx = findoption(name);
4493 if (opt_idx < 0)
4494 return 0;
4495
4496 p = &(options[opt_idx]);
4497
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004498 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004499 if (p->var == NULL)
4500 return 0;
4501
4502 if (p->flags & P_BOOL)
4503 r |= SOPT_BOOL;
4504 else if (p->flags & P_NUM)
4505 r |= SOPT_NUM;
4506 else if (p->flags & P_STRING)
4507 r |= SOPT_STRING;
4508
4509 if (p->indir == PV_NONE)
4510 {
4511 if (opt_type == SREQ_GLOBAL)
4512 r |= SOPT_GLOBAL;
4513 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004514 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004515 }
4516 else
4517 {
4518 if (p->indir & PV_BOTH)
4519 r |= SOPT_GLOBAL;
4520 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004521 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004522
4523 if (p->indir & PV_WIN)
4524 {
4525 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004526 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004527 else
4528 r |= SOPT_WIN;
4529 }
4530 else if (p->indir & PV_BUF)
4531 {
4532 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004533 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004534 else
4535 r |= SOPT_BUF;
4536 }
4537 }
4538
4539 if (stringval == NULL)
4540 return r;
4541
4542 if (opt_type == SREQ_GLOBAL)
4543 varp = p->var;
4544 else
4545 {
4546 if (opt_type == SREQ_BUF)
4547 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004548 // Special case: 'modified' is b_changed, but we also want to
4549 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004550 if (p->indir == PV_MOD)
4551 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004552 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004553 varp = NULL;
4554 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004555# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004556 else if (p->indir == PV_KEY)
4557 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004558 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004559 *stringval = NULL;
4560 varp = NULL;
4561 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004562# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004563 else
4564 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004565 buf_T *save_curbuf = curbuf;
4566
4567 // only getting a pointer, no need to use aucmd_prepbuf()
4568 curbuf = (buf_T *)from;
4569 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004570 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004571 curbuf = save_curbuf;
4572 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004573 }
4574 }
4575 else if (opt_type == SREQ_WIN)
4576 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004577 win_T *save_curwin = curwin;
4578
4579 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004580 curbuf = curwin->w_buffer;
4581 varp = get_varp(p);
4582 curwin = save_curwin;
4583 curbuf = curwin->w_buffer;
4584 }
4585 if (varp == p->var)
4586 return (r | SOPT_UNSET);
4587 }
4588
4589 if (varp != NULL)
4590 {
4591 if (p->flags & P_STRING)
4592 *stringval = vim_strsave(*(char_u **)(varp));
4593 else if (p->flags & P_NUM)
4594 *numval = *(long *) varp;
4595 else
4596 *numval = *(int *)varp;
4597 }
4598
4599 return r;
4600}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004601
4602/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004603 * Iterate over options. First argument is a pointer to a pointer to a
4604 * structure inside options[] array, second is option type like in the above
4605 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004606 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004607 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004608 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004609 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004610 * first argument to NULL.
4611 *
4612 * Returns full option name for current option on each call.
4613 */
4614 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004615option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004616{
4617 struct vimoption *ret = NULL;
4618 do
4619 {
4620 if (*option == NULL)
4621 *option = (void *) options;
4622 else if (((struct vimoption *) (*option))->fullname == NULL)
4623 {
4624 *option = NULL;
4625 return NULL;
4626 }
4627 else
4628 *option = (void *) (((struct vimoption *) (*option)) + 1);
4629
4630 ret = ((struct vimoption *) (*option));
4631
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004632 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004633 if (ret->var == NULL)
4634 {
4635 ret = NULL;
4636 continue;
4637 }
4638
4639 switch (opt_type)
4640 {
4641 case SREQ_GLOBAL:
4642 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4643 ret = NULL;
4644 break;
4645 case SREQ_BUF:
4646 if (!(ret->indir & PV_BUF))
4647 ret = NULL;
4648 break;
4649 case SREQ_WIN:
4650 if (!(ret->indir & PV_WIN))
4651 ret = NULL;
4652 break;
4653 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004654 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004655 return NULL;
4656 }
4657 }
4658 while (ret == NULL);
4659
4660 return (char_u *)ret->fullname;
4661}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004662#endif
4663
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004665 * Return the flags for the option at 'opt_idx'.
4666 */
4667 long_u
4668get_option_flags(int opt_idx)
4669{
4670 return options[opt_idx].flags;
4671}
4672
4673/*
4674 * Set a flag for the option at 'opt_idx'.
4675 */
4676 void
4677set_option_flag(int opt_idx, long_u flag)
4678{
4679 options[opt_idx].flags |= flag;
4680}
4681
4682/*
4683 * Clear a flag for the option at 'opt_idx'.
4684 */
4685 void
4686clear_option_flag(int opt_idx, long_u flag)
4687{
4688 options[opt_idx].flags &= ~flag;
4689}
4690
4691/*
4692 * Returns TRUE if the option at 'opt_idx' is a global option
4693 */
4694 int
4695is_global_option(int opt_idx)
4696{
4697 return options[opt_idx].indir == PV_NONE;
4698}
4699
4700/*
4701 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4702 * local value.
4703 */
4704 int
4705is_global_local_option(int opt_idx)
4706{
4707 return options[opt_idx].indir & PV_BOTH;
4708}
4709
4710/*
4711 * Returns TRUE if the option at 'opt_idx' is a window-local option
4712 */
4713 int
4714is_window_local_option(int opt_idx)
4715{
4716 return options[opt_idx].var == VAR_WIN;
4717}
4718
4719/*
4720 * Returns TRUE if the option at 'opt_idx' is a hidden option
4721 */
4722 int
4723is_hidden_option(int opt_idx)
4724{
4725 return options[opt_idx].var == NULL;
4726}
4727
4728#if defined(FEAT_CRYPT) || defined(PROTO)
4729/*
4730 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4731 */
4732 int
4733is_crypt_key_option(int opt_idx)
4734{
4735 return options[opt_idx].indir == PV_KEY;
4736}
4737#endif
4738
4739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 * Set the value of option "name".
4741 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004742 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004743 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004745 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004746set_option_value(
4747 char_u *name,
4748 long number,
4749 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004750 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751{
4752 int opt_idx;
4753 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004754 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004757 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004758 {
4759 int key;
4760
4761 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004762 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004763 {
4764 char_u key_name[2];
4765
4766 if (key < 0)
4767 {
4768 key_name[0] = KEY2TERMCAP0(key);
4769 key_name[1] = KEY2TERMCAP1(key);
4770 }
4771 else
4772 {
4773 key_name[0] = KS_KEY;
4774 key_name[1] = (key & 0xff);
4775 }
4776 add_termcode(key_name, string, FALSE);
4777 if (full_screen)
4778 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004779 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01004780 return NULL;
4781 }
4782
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004783 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 else
4786 {
4787 flags = options[opt_idx].flags;
4788#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004789 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004791 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004792 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004793 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004796 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004797 return set_string_option(opt_idx, string, opt_flags);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004798
4799 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
4800 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004802 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004804 int idx;
4805
4806 // Either we are given a string or we are setting option
4807 // to zero.
4808 for (idx = 0; string[idx] == '0'; ++idx)
4809 ;
4810 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004811 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004812 // There's another character after zeros or the string
4813 // is empty. In both cases, we are trying to set a
4814 // num option using a string.
4815 semsg(_(e_number_required_after_str_equal_str),
4816 name, string);
4817 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004818
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004821 if (flags & P_NUM)
4822 return set_num_option(opt_idx, varp, number,
4823 NULL, 0, opt_flags);
4824 else
4825 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004827
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004829 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830}
4831
4832/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004833 * Call set_option_value() and when an error is returned report it.
4834 */
4835 void
4836set_option_value_give_err(
4837 char_u *name,
4838 long number,
4839 char_u *string,
4840 int opt_flags) // OPT_LOCAL or 0 (both)
4841{
4842 char *errmsg = set_option_value(name, number, string, opt_flags);
4843
4844 if (errmsg != NULL)
4845 emsg(_(errmsg));
4846}
4847
4848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 * Get the terminal code for a terminal option.
4850 * Returns NULL when not found.
4851 */
4852 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004853get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
4855 int opt_idx;
4856 char_u *varp;
4857
4858 if (tname[0] != 't' || tname[1] != '_' ||
4859 tname[2] == NUL || tname[3] == NUL)
4860 return NULL;
4861 if ((opt_idx = findoption(tname)) >= 0)
4862 {
4863 varp = get_varp(&(options[opt_idx]));
4864 if (varp != NULL)
4865 varp = *(char_u **)(varp);
4866 return varp;
4867 }
4868 return find_termcode(tname + 2);
4869}
4870
4871 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004872get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873{
4874 int i;
4875
4876 i = findoption((char_u *)"hl");
4877 if (i >= 0)
4878 return options[i].def_val[VI_DEFAULT];
4879 return (char_u *)NULL;
4880}
4881
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004882 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004883get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004884{
4885 int i;
4886
4887 i = findoption((char_u *)"enc");
4888 if (i >= 0)
4889 return options[i].def_val[VI_DEFAULT];
4890 return (char_u *)NULL;
4891}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004892
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004893#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004894 int
4895is_option_allocated(char *name)
4896{
4897 int idx = findoption((char_u *)name);
4898
4899 return idx >= 0 && (options[idx].flags & P_ALLOCED);
4900}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004901#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004902
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004903#if defined(FEAT_EVAL) || defined(PROTO)
4904/*
4905 * Return TRUE if "name" is a string option.
4906 * Returns FALSE if option "name" does not exist.
4907 */
4908 int
4909is_string_option(char_u *name)
4910{
4911 int idx = findoption(name);
4912
4913 return idx >= 0 && (options[idx].flags & P_STRING);
4914}
4915#endif
4916
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917/*
4918 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004919 * When "has_lt" is true there is a '<' before "*arg_arg".
4920 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 */
4922 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004923find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004925 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004927 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
4929 /*
4930 * Don't use get_special_key_code() for t_xx, we don't want it to call
4931 * add_termcap_entry().
4932 */
4933 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4934 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004935 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004937 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004939 key = find_special_key(&arg, &modifiers,
4940 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004941 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 key = 0;
4943 }
4944 return key;
4945}
4946
4947/*
4948 * if 'all' == 0: show changed options
4949 * if 'all' == 1: show all normal options
4950 * if 'all' == 2: show all terminal options
4951 */
4952 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004953showoptions(
4954 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004955 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956{
4957 struct vimoption *p;
4958 int col;
4959 int isterm;
4960 char_u *varp;
4961 struct vimoption **items;
4962 int item_count;
4963 int run;
4964 int row, rows;
4965 int cols;
4966 int i;
4967 int len;
4968
4969#define INC 20
4970#define GAP 3
4971
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004972 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if (items == NULL)
4974 return;
4975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004976 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004978 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004980 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004982 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004984 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985
4986 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004987 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 * 1. display the short items
4989 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004990 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 */
4992 for (run = 1; run <= 2 && !got_int; ++run)
4993 {
4994 /*
4995 * collect the items in items[]
4996 */
4997 item_count = 0;
4998 for (p = &options[0]; p->fullname != NULL; p++)
4999 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005000 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005001 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005002 continue;
5003
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 varp = NULL;
5005 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005006 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 {
5008 if (p->indir != PV_NONE && !isterm)
5009 varp = get_varp_scope(p, opt_flags);
5010 }
5011 else
5012 varp = get_varp(p);
5013 if (varp != NULL
5014 && ((all == 2 && isterm)
5015 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005016 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005018 if (opt_flags & OPT_ONECOLUMN)
5019 len = Columns;
5020 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005021 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 else
5023 {
5024 option_value2string(p, opt_flags);
5025 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5026 }
5027 if ((len <= INC - GAP && run == 1) ||
5028 (len > INC - GAP && run == 2))
5029 items[item_count++] = p;
5030 }
5031 }
5032
5033 /*
5034 * display the items
5035 */
5036 if (run == 1)
5037 {
5038 cols = (Columns + GAP - 3) / INC;
5039 if (cols == 0)
5040 cols = 1;
5041 rows = (item_count + cols - 1) / cols;
5042 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005043 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 rows = item_count;
5045 for (row = 0; row < rows && !got_int; ++row)
5046 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005047 msg_putchar('\n'); // go to next line
5048 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 break;
5050 col = 0;
5051 for (i = row; i < item_count; i += rows)
5052 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005053 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 showoneopt(items[i], opt_flags);
5055 col += INC;
5056 }
5057 out_flush();
5058 ui_breakcheck();
5059 }
5060 }
5061 vim_free(items);
5062}
5063
5064/*
5065 * Return TRUE if option "p" has its default value.
5066 */
5067 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005068optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069{
5070 int dvi;
5071
5072 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005073 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005074 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005076 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005078 // the cast to long is required for Manx C, long_i is
5079 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005080 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005081 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5083}
5084
5085/*
5086 * showoneopt: show the value of one option
5087 * must not be called with a hidden option!
5088 */
5089 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005090showoneopt(
5091 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005092 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005094 char_u *varp;
5095 int save_silent = silent_mode;
5096
5097 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005098 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
5100 varp = get_varp_scope(p, opt_flags);
5101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005102 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5104 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005105 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005107 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005109 msg_puts(" ");
5110 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 if (!(p->flags & P_BOOL))
5112 {
5113 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005114 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 option_value2string(p, opt_flags);
5116 msg_outtrans(NameBuff);
5117 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005118
5119 silent_mode = save_silent;
5120 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121}
5122
5123/*
5124 * Write modified options as ":set" commands to a file.
5125 *
5126 * There are three values for "opt_flags":
5127 * OPT_GLOBAL: Write global option values and fresh values of
5128 * buffer-local options (used for start of a session
5129 * file).
5130 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5131 * curwin (used for a vimrc file).
5132 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5133 * and local values for window-local options of
5134 * curwin. Local values are also written when at the
5135 * default value, because a modeline or autocommand
5136 * may have set them when doing ":edit file" and the
5137 * user has set them back at the default or fresh
5138 * value.
5139 * When "local_only" is TRUE, don't write fresh
5140 * values, only local values (for ":mkview").
5141 * (fresh value = value used for a new buffer or window for a local option).
5142 *
5143 * Return FAIL on error, OK otherwise.
5144 */
5145 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005146makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147{
5148 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005149 char_u *varp; // currently used value
5150 char_u *varp_fresh; // local value
5151 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 char *cmd;
5153 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005154 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155
5156 /*
5157 * The options that don't have a default (terminal name, columns, lines)
5158 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005159 * Do the loop over "options[]" twice: once for options with the
5160 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005162 for (pri = 1; pri >= 0; --pri)
5163 {
5164 for (p = &options[0]; !istermoption(p); p++)
5165 if (!(p->flags & P_NO_MKRC)
5166 && !istermoption(p)
5167 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005169 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5171 continue;
5172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005173 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5174 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5176 continue;
5177
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005178 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005180 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 continue;
5182
Bram Moolenaard23b7142021-04-17 21:04:34 +02005183 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5184 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005185 continue;
5186
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 round = 2;
5188 if (p->indir != PV_NONE)
5189 {
5190 if (p->var == VAR_WIN)
5191 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005192 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 if (!(opt_flags & OPT_LOCAL))
5194 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005195 // When fresh value of window-local option is not at the
5196 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5198 {
5199 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005200 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
5202 round = 1;
5203 varp_local = varp;
5204 varp = varp_fresh;
5205 }
5206 }
5207 }
5208 }
5209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005210 // Round 1: fresh value for window-local options.
5211 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 for ( ; round <= 2; varp = varp_local, ++round)
5213 {
5214 if (round == 1 || (opt_flags & OPT_GLOBAL))
5215 cmd = "set";
5216 else
5217 cmd = "setlocal";
5218
5219 if (p->flags & P_BOOL)
5220 {
5221 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5222 return FAIL;
5223 }
5224 else if (p->flags & P_NUM)
5225 {
5226 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5227 return FAIL;
5228 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005229 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005231 int do_endif = FALSE;
5232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005233 // Don't set 'syntax' and 'filetype' again if the value is
5234 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005235 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005236#if defined(FEAT_SYN_HL)
5237 p->indir == PV_SYN ||
5238#endif
5239 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
5241 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5242 *(char_u **)(varp)) < 0
5243 || put_eol(fd) < 0)
5244 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005245 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 }
5247 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005248 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005250 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
5252 if (put_line(fd, "endif") == FAIL)
5253 return FAIL;
5254 }
5255 }
5256 }
5257 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 return OK;
5260}
5261
5262#if defined(FEAT_FOLDING) || defined(PROTO)
5263/*
5264 * Generate set commands for the local fold options only. Used when
5265 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5266 */
5267 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005268makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005270 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005272 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 == FAIL
5274# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005275 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005277 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 == FAIL
5279 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5280 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5281 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5282 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5283 )
5284 return FAIL;
5285
5286 return OK;
5287}
5288#endif
5289
5290 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005291put_setstring(
5292 FILE *fd,
5293 char *cmd,
5294 char *name,
5295 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005296 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297{
5298 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005299 char_u *buf = NULL;
5300 char_u *part = NULL;
5301 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
5303 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5304 return FAIL;
5305 if (*valuep != NULL)
5306 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005307 // Output 'pastetoggle' as key names. For other
5308 // options some characters have to be escaped with
5309 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 if (valuep == &p_pt)
5311 {
5312 s = *valuep;
5313 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005314 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 return FAIL;
5316 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005317 // expand the option value, replace $HOME by ~
5318 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005320 int size = (int)STRLEN(*valuep) + 1;
5321
5322 // replace home directory in the whole option value into "buf"
5323 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005324 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005325 goto fail;
5326 home_replace(NULL, *valuep, buf, size, FALSE);
5327
5328 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005329 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005330 // can be expanded when read back.
5331 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5332 && vim_strchr(*valuep, ',') != NULL)
5333 {
5334 part = alloc(size);
5335 if (part == NULL)
5336 goto fail;
5337
5338 // write line break to clear the option, e.g. ':set rtp='
5339 if (put_eol(fd) == FAIL)
5340 goto fail;
5341
5342 p = buf;
5343 while (*p != NUL)
5344 {
5345 // for each comma separated option part, append value to
5346 // the option, :set rtp+=value
5347 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5348 goto fail;
5349 (void)copy_option_part(&p, part, size, ",");
5350 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5351 goto fail;
5352 }
5353 vim_free(buf);
5354 vim_free(part);
5355 return OK;
5356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005358 {
5359 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005361 }
5362 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 }
5364 else if (put_escstr(fd, *valuep, 2) == FAIL)
5365 return FAIL;
5366 }
5367 if (put_eol(fd) < 0)
5368 return FAIL;
5369 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005370fail:
5371 vim_free(buf);
5372 vim_free(part);
5373 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374}
5375
5376 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005377put_setnum(
5378 FILE *fd,
5379 char *cmd,
5380 char *name,
5381 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
5383 long wc;
5384
5385 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5386 return FAIL;
5387 if (wc_use_keyname((char_u *)valuep, &wc))
5388 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005389 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5391 return FAIL;
5392 }
5393 else if (fprintf(fd, "%ld", *valuep) < 0)
5394 return FAIL;
5395 if (put_eol(fd) < 0)
5396 return FAIL;
5397 return OK;
5398}
5399
5400 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005401put_setbool(
5402 FILE *fd,
5403 char *cmd,
5404 char *name,
5405 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005407 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005408 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5410 || put_eol(fd) < 0)
5411 return FAIL;
5412 return OK;
5413}
5414
5415/*
5416 * Clear all the terminal options.
5417 * If the option has been allocated, free the memory.
5418 * Terminal options are never hidden or indirect.
5419 */
5420 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005421clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 /*
5424 * Reset a few things before clearing the old options. This may cause
5425 * outputting a few things that the terminal doesn't understand, but the
5426 * screen will be cleared later, so this is OK.
5427 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005428 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005429 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005431 // When starting the GUI close the display opened for the clipboard.
5432 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 if (gui.starting)
5434 clear_xterm_clip();
5435#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005436 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005438 free_termoptions();
5439}
5440
5441 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005442free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005443{
5444 struct vimoption *p;
5445
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005446 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (istermoption(p))
5448 {
5449 if (p->flags & P_ALLOCED)
5450 free_string_option(*(char_u **)(p->var));
5451 if (p->flags & P_DEF_ALLOCED)
5452 free_string_option(p->def_val[VI_DEFAULT]);
5453 *(char_u **)(p->var) = empty_option;
5454 p->def_val[VI_DEFAULT] = empty_option;
5455 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005456#ifdef FEAT_EVAL
5457 // remember where the option was cleared
5458 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 }
5461 clear_termcodes();
5462}
5463
5464/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005465 * Free the string for one term option, if it was allocated.
5466 * Set the string to empty_option and clear allocated flag.
5467 * "var" points to the option value.
5468 */
5469 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005470free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005471{
5472 struct vimoption *p;
5473
5474 for (p = &options[0]; p->fullname != NULL; p++)
5475 if (p->var == var)
5476 {
5477 if (p->flags & P_ALLOCED)
5478 free_string_option(*(char_u **)(p->var));
5479 *(char_u **)(p->var) = empty_option;
5480 p->flags &= ~P_ALLOCED;
5481 break;
5482 }
5483}
5484
5485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 * Set the terminal option defaults to the current value.
5487 * Used after setting the terminal name.
5488 */
5489 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005490set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
5492 struct vimoption *p;
5493
5494 for (p = &options[0]; p->fullname != NULL; p++)
5495 {
5496 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5497 {
5498 if (p->flags & P_DEF_ALLOCED)
5499 {
5500 free_string_option(p->def_val[VI_DEFAULT]);
5501 p->flags &= ~P_DEF_ALLOCED;
5502 }
5503 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5504 if (p->flags & P_ALLOCED)
5505 {
5506 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005507 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 }
5509 }
5510 }
5511}
5512
5513/*
5514 * return TRUE if 'p' starts with 't_'
5515 */
5516 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005517istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5520}
5521
Bram Moolenaardac13472019-09-16 21:06:21 +02005522/*
5523 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5524 */
5525 int
5526istermoption_idx(int opt_idx)
5527{
5528 return istermoption(&options[opt_idx]);
5529}
5530
Bram Moolenaar113e1072019-01-20 15:30:40 +01005531#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005533 * Unset local option value, similar to ":set opt<".
5534 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005536unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005537{
5538 struct vimoption *p;
5539 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005540 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005541
5542 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005543 if (opt_idx < 0)
5544 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005545 p = &(options[opt_idx]);
5546
5547 switch ((int)p->indir)
5548 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005549 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005550 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005551 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005552 break;
5553 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005554 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005555 break;
5556 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005557 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005558 break;
5559 case PV_AR:
5560 buf->b_p_ar = -1;
5561 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005562 case PV_BKC:
5563 clear_string_option(&buf->b_p_bkc);
5564 buf->b_bkc_flags = 0;
5565 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005566 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005567 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005568 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005569 case PV_TC:
5570 clear_string_option(&buf->b_p_tc);
5571 buf->b_tc_flags = 0;
5572 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005573 case PV_SISO:
5574 curwin->w_p_siso = -1;
5575 break;
5576 case PV_SO:
5577 curwin->w_p_so = -1;
5578 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005579# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005580 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005581 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005582 break;
5583 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005584 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005585 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005586# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005587 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005588 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005589 break;
5590 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005591 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005592 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005593# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005594 case PV_TSRFU:
5595 clear_string_option(&buf->b_p_tsrfu);
5596 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005597# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005598 case PV_FP:
5599 clear_string_option(&buf->b_p_fp);
5600 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005601# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005602 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005603 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005604 break;
5605 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005606 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005607 break;
5608 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005609 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005610 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005611# endif
5612# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005613 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005614 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005615 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005616# endif
5617# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005618 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005619 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005620 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005621# endif
5622# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01005623 case PV_SBR:
5624 clear_string_option(&((win_T *)from)->w_p_sbr);
5625 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005626# endif
5627# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005628 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005629 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005630 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005631# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005632 case PV_UL:
5633 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5634 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005635 case PV_LW:
5636 clear_string_option(&buf->b_p_lw);
5637 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005638 case PV_MENC:
5639 clear_string_option(&buf->b_p_menc);
5640 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005641 case PV_LCS:
5642 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005643 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005644 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005645 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005646 case PV_FCS:
5647 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005648 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005649 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005650 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005651 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005652 clear_string_option(&((win_T *)from)->w_p_ve);
5653 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005654 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005655 }
5656}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005657#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005658
5659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005661 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 */
5663 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005664get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005666 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 {
5668 if (p->var == VAR_WIN)
5669 return (char_u *)GLOBAL_WO(get_varp(p));
5670 return p->var;
5671 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005672 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
5674 switch ((int)p->indir)
5675 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005676 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005678 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5679 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5680 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005682 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5683 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5684 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005685 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005686 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005687 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005688 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5689 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005691 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5692 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005694 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5695 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005696#ifdef FEAT_COMPL_FUNC
5697 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5698#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005699#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5700 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5701#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005702#if defined(FEAT_CRYPT)
5703 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5704#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005705#ifdef FEAT_LINEBREAK
5706 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5707#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005708#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005709 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005710#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005711 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005712 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005713 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005714 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005715 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005716 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005717 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005718
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005720 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722 return get_varp(p);
5723}
5724
5725/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005726 * Get pointer to option variable at 'opt_idx', depending on local or global
5727 * scope.
5728 */
5729 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005730get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005731{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005732 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005733}
5734
5735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 * Get pointer to option variable.
5737 */
5738 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005739get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005741 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 if (p->var == NULL)
5743 return NULL;
5744
5745 switch ((int)p->indir)
5746 {
5747 case PV_NONE: return p->var;
5748
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005749 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005750 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005752 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005754 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005756 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005758 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005760 case PV_TC: return *curbuf->b_p_tc != NUL
5761 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005762 case PV_BKC: return *curbuf->b_p_bkc != NUL
5763 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005764 case PV_SISO: return curwin->w_p_siso >= 0
5765 ? (char_u *)&(curwin->w_p_siso) : p->var;
5766 case PV_SO: return curwin->w_p_so >= 0
5767 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005769 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005771 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5773#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005774 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005776 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005778#ifdef FEAT_COMPL_FUNC
5779 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5780 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5781#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005782 case PV_FP: return *curbuf->b_p_fp != NUL
5783 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005785 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005787 case PV_GP: return *curbuf->b_p_gp != NUL
5788 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5789 case PV_MP: return *curbuf->b_p_mp != NUL
5790 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005792#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5793 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5794 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5795#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005796#if defined(FEAT_CRYPT)
5797 case PV_CM: return *curbuf->b_p_cm != NUL
5798 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5799#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005800#ifdef FEAT_LINEBREAK
5801 case PV_SBR: return *curwin->w_p_sbr != NUL
5802 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5803#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005804#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005805 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005806 ? (char_u *)&(curwin->w_p_stl) : p->var;
5807#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005808 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5809 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005810 case PV_LW: return *curbuf->b_p_lw != NUL
5811 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005812 case PV_MENC: return *curbuf->b_p_menc != NUL
5813 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814#ifdef FEAT_ARABIC
5815 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5816#endif
5817 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005818 case PV_LCS: return *curwin->w_p_lcs != NUL
5819 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005820 case PV_FCS: return *curwin->w_p_fcs != NUL
5821 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005822 case PV_VE: return *curwin->w_p_ve != NUL
5823 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005824#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005825 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5826#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005827#ifdef FEAT_SYN_HL
5828 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5829 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005830 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005831 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833#ifdef FEAT_DIFF
5834 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5835#endif
5836#ifdef FEAT_FOLDING
5837 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5838 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5839 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5840 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5841 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5842 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5843 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5844# ifdef FEAT_EVAL
5845 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5846 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5847# endif
5848 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5849#endif
5850 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005851 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005852#ifdef FEAT_LINEBREAK
5853 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005856 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005857#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5859#endif
5860#ifdef FEAT_RIGHTLEFT
5861 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5862 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5863#endif
5864 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01005865 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5867#ifdef FEAT_LINEBREAK
5868 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005869 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5870 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005872 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005874 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005875#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005876 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5877 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5878#endif
5879#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005880 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5881 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5882 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884
5885 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5886 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5889 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5891 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5893 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5894 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01005895 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898#ifdef FEAT_FOLDING
5899 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005902#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005903 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005905#ifdef FEAT_COMPL_FUNC
5906 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005907 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005908#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005909#ifdef FEAT_EVAL
5910 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5911#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01005912 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005914 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005920 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5922 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5923 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5924 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5925#ifdef FEAT_FIND_ID
5926# ifdef FEAT_EVAL
5927 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5928# endif
5929#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01005930#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5932 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5933#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005934#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005935 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937#ifdef FEAT_CRYPT
5938 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01005941 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5943 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5944 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5945 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5946 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005948 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5955#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005956 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005957 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5958#endif
5959#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005960 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5961 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5962 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005963 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964#endif
5965 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5966 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5967 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5968 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005969#ifdef FEAT_PERSISTENT_UNDO
5970 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5973#ifdef FEAT_KEYMAP
5974 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5975#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005976#ifdef FEAT_SIGNS
5977 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5978#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005979#ifdef FEAT_VARTABS
5980 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5981 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5982#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005983 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005985 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 return (char_u *)&(curbuf->b_p_wm);
5987}
5988
5989/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005990 * Return a pointer to the variable for option at 'opt_idx'
5991 */
5992 char_u *
5993get_option_var(int opt_idx)
5994{
5995 return options[opt_idx].var;
5996}
5997
Dominique Pelle748b3082022-01-08 12:41:16 +00005998#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005999/*
6000 * Return the full name of the option at 'opt_idx'
6001 */
6002 char_u *
6003get_option_fullname(int opt_idx)
6004{
6005 return (char_u *)options[opt_idx].fullname;
6006}
Dominique Pelle748b3082022-01-08 12:41:16 +00006007#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006008
6009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 * Get the value of 'equalprg', either the buffer-local one or the global one.
6011 */
6012 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006013get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014{
6015 if (*curbuf->b_p_ep == NUL)
6016 return p_ep;
6017 return curbuf->b_p_ep;
6018}
6019
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020/*
6021 * Copy options from one window to another.
6022 * Used when splitting a window.
6023 */
6024 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006025win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
6027 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6028 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006029 after_copy_winopt(wp_to);
6030}
6031
6032/*
6033 * After copying window options: update variables depending on options.
6034 */
6035 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006036after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006037{
6038#ifdef FEAT_LINEBREAK
6039 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006040#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006041#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006042 fill_culopt_flags(NULL, wp);
6043 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006044#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006045 set_chars_option(wp, &wp->w_p_lcs, TRUE);
6046 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006047}
6048
6049 static char_u *
6050copy_option_val(char_u *val)
6051{
6052 if (val == empty_option)
6053 return empty_option; // no need to allocate memory
6054 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
6057/*
6058 * Copy the options from one winopt_T to another.
6059 * Doesn't free the old option values in "to", use clear_winopt() for that.
6060 * The 'scroll' option is not copied, because it depends on the window height.
6061 * The 'previewwindow' option is reset, there can be only one preview window.
6062 */
6063 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006064copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065{
6066#ifdef FEAT_ARABIC
6067 to->wo_arab = from->wo_arab;
6068#endif
6069 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006070 to->wo_lcs = copy_option_val(from->wo_lcs);
6071 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006073 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006074 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006075 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006076#ifdef FEAT_LINEBREAK
6077 to->wo_nuw = from->wo_nuw;
6078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079#ifdef FEAT_RIGHTLEFT
6080 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006081 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006083#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006084 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006085#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006086#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006087 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006090#ifdef FEAT_DIFF
6091 to->wo_wrap_save = from->wo_wrap_save;
6092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093#ifdef FEAT_LINEBREAK
6094 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006095 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006096 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006098 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006100 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006101 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006102 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006103 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006104#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006105 to->wo_spell = from->wo_spell;
6106#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006107#ifdef FEAT_SYN_HL
6108 to->wo_cuc = from->wo_cuc;
6109 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006110 to->wo_culopt = copy_option_val(from->wo_culopt);
6111 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113#ifdef FEAT_DIFF
6114 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006115 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006117#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006118 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006119 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006120#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006121#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006122 to->wo_twk = copy_option_val(from->wo_twk);
6123 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125#ifdef FEAT_FOLDING
6126 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006127 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006129 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006130 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 to->wo_fml = from->wo_fml;
6132 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006133 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006134 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006135 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006136 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 to->wo_fdn = from->wo_fdn;
6138# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006139 to->wo_fde = copy_option_val(from->wo_fde);
6140 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006142 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006144#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006145 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006146#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006147
6148#ifdef FEAT_EVAL
6149 // Copy the script context so that we know where the value was last set.
6150 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6151 sizeof(to->wo_script_ctx));
6152#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006153 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154}
6155
6156/*
6157 * Check string options in a window for a NULL value.
6158 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006160check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161{
6162 check_winopt(&win->w_onebuf_opt);
6163 check_winopt(&win->w_allbuf_opt);
6164}
6165
6166/*
6167 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6168 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006170check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172#ifdef FEAT_FOLDING
6173 check_string_option(&wop->wo_fdi);
6174 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006175 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176# ifdef FEAT_EVAL
6177 check_string_option(&wop->wo_fde);
6178 check_string_option(&wop->wo_fdt);
6179# endif
6180 check_string_option(&wop->wo_fmr);
6181#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006182#ifdef FEAT_SIGNS
6183 check_string_option(&wop->wo_scl);
6184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185#ifdef FEAT_RIGHTLEFT
6186 check_string_option(&wop->wo_rlc);
6187#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006188#ifdef FEAT_LINEBREAK
6189 check_string_option(&wop->wo_sbr);
6190#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006191#ifdef FEAT_STL_OPT
6192 check_string_option(&wop->wo_stl);
6193#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006194#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006195 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006196 check_string_option(&wop->wo_cc);
6197#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006198#ifdef FEAT_CONCEAL
6199 check_string_option(&wop->wo_cocu);
6200#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006201#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006202 check_string_option(&wop->wo_twk);
6203 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006204#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006205#ifdef FEAT_LINEBREAK
6206 check_string_option(&wop->wo_briopt);
6207#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006208 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006209 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006210 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006211 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212}
6213
6214/*
6215 * Free the allocated memory inside a winopt_T.
6216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006218clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219{
6220#ifdef FEAT_FOLDING
6221 clear_string_option(&wop->wo_fdi);
6222 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006223 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224# ifdef FEAT_EVAL
6225 clear_string_option(&wop->wo_fde);
6226 clear_string_option(&wop->wo_fdt);
6227# endif
6228 clear_string_option(&wop->wo_fmr);
6229#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006230#ifdef FEAT_SIGNS
6231 clear_string_option(&wop->wo_scl);
6232#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006233#ifdef FEAT_LINEBREAK
6234 clear_string_option(&wop->wo_briopt);
6235#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006236 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237#ifdef FEAT_RIGHTLEFT
6238 clear_string_option(&wop->wo_rlc);
6239#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006240#ifdef FEAT_LINEBREAK
6241 clear_string_option(&wop->wo_sbr);
6242#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006243#ifdef FEAT_STL_OPT
6244 clear_string_option(&wop->wo_stl);
6245#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006246#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006247 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006248 clear_string_option(&wop->wo_cc);
6249#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006250#ifdef FEAT_CONCEAL
6251 clear_string_option(&wop->wo_cocu);
6252#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006253#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006254 clear_string_option(&wop->wo_twk);
6255 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006256#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006257 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006258 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006259 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260}
6261
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006262#ifdef FEAT_EVAL
6263// Index into the options table for a buffer-local option enum.
6264static int buf_opt_idx[BV_COUNT];
6265# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6266
6267/*
6268 * Initialize buf_opt_idx[] if not done already.
6269 */
6270 static void
6271init_buf_opt_idx(void)
6272{
6273 static int did_init_buf_opt_idx = FALSE;
6274 int i;
6275
6276 if (did_init_buf_opt_idx)
6277 return;
6278 did_init_buf_opt_idx = TRUE;
6279 for (i = 0; !istermoption_idx(i); i++)
6280 if (options[i].indir & PV_BUF)
6281 buf_opt_idx[options[i].indir & PV_MASK] = i;
6282}
6283#else
6284# define COPY_OPT_SCTX(buf, bv)
6285#endif
6286
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287/*
6288 * Copy global option values to local options for one buffer.
6289 * Used when creating a new buffer and sometimes when entering a buffer.
6290 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006291 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6293 * appropriate.
6294 * BCO_NOHELP Don't copy the values to a help buffer.
6295 */
6296 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006297buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298{
6299 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006300 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 int dont_do_help;
6302 int did_isk = FALSE;
6303
6304 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 * Skip this when the option defaults have not been set yet. Happens when
6306 * main() allocates the first buffer.
6307 */
6308 if (p_cpo != NULL)
6309 {
6310 /*
6311 * Always copy when entering and 'cpo' contains 'S'.
6312 * Don't copy when already initialized.
6313 * Don't copy when 'cpo' contains 's' and not entering.
6314 * 'S' BCO_ENTER initialized 's' should_copy
6315 * yes yes X X TRUE
6316 * yes no yes X FALSE
6317 * no X yes X FALSE
6318 * X no no yes FALSE
6319 * X no no no TRUE
6320 * no yes no X TRUE
6321 */
6322 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6323 && (buf->b_p_initialized
6324 || (!(flags & BCO_ENTER)
6325 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6326 should_copy = FALSE;
6327
6328 if (should_copy || (flags & BCO_ALWAYS))
6329 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006330#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006331 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006332 init_buf_opt_idx();
6333#endif
6334 // Don't copy the options specific to a help buffer when
6335 // BCO_NOHELP is given or the options were initialized already
6336 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6338 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006339 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 {
6341 save_p_isk = buf->b_p_isk;
6342 buf->b_p_isk = NULL;
6343 }
6344 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02006345 * Always free the allocated strings. If not already initialized,
6346 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 */
6348 if (!buf->b_p_initialized)
6349 {
6350 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006351 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006354 switch (*p_ffs)
6355 {
6356 case 'm':
6357 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6358 case 'd':
6359 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6360 case 'u':
6361 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6362 default:
6363 buf->b_p_ff = vim_strsave(p_ff);
6364 }
6365 if (buf->b_p_ff != NULL)
6366 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 buf->b_p_bh = empty_option;
6368 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 }
6370 else
6371 free_buf_options(buf, FALSE);
6372
6373 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006374 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 buf->b_p_ai_nopaste = p_ai_nopaste;
6376 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006377 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006379 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 buf->b_p_tw_nopaste = p_tw_nopaste;
6381 buf->b_p_tw_nobin = p_tw_nobin;
6382 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006383 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 buf->b_p_wm_nopaste = p_wm_nopaste;
6385 buf->b_p_wm_nobin = p_wm_nobin;
6386 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006387 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006388 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006389 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006390 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006391 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006393 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006395 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006397 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 buf->b_p_ml_nobin = p_ml_nobin;
6399 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006400 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006401 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006402 buf->b_p_swf = FALSE;
6403 else
6404 {
6405 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006406 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006409 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006410#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006411 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006412 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006414#ifdef FEAT_COMPL_FUNC
6415 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006416 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006417 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006418 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006419 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006420 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006421#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006422#ifdef FEAT_EVAL
6423 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006424 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006425 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006428 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006430#ifdef FEAT_VARTABS
6431 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006432 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006433 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006434 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006435 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006436 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006437 buf->b_p_vsts_nopaste = p_vsts_nopaste
6438 ? vim_strsave(p_vsts_nopaste) : NULL;
6439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006441 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006443 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444#ifdef FEAT_FOLDING
6445 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006446 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447#endif
6448 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006449 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006450 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006451 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006452 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6453 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006455 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006457 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006459 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006461 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006462
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006464 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006466 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006468 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006469 buf->b_p_cinsd = vim_strsave(p_cinsd);
6470 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006471 buf->b_p_lop = vim_strsave(p_lop);
6472 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006473
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006474 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006477 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006479 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006481 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006483 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006485 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006486 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006487 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006488#endif
6489#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006490 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006491 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006492 (void)compile_cap_prog(&buf->b_s);
6493 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006494 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006495 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006496 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006497 buf->b_s.b_p_spo = vim_strsave(p_spo);
6498 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006500#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006502 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006504 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006506 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006507#if defined(FEAT_EVAL)
6508 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006509 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511#ifdef FEAT_CRYPT
6512 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006513 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006516 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517#ifdef FEAT_KEYMAP
6518 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006519 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 buf->b_kmap_state |= KEYMAP_INIT;
6521#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006522#ifdef FEAT_TERMINAL
6523 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006524 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006525#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006526 // This isn't really an option, but copying the langmap and IME
6527 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006529 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006531 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006533 // options that are normally global but also have a local value
6534 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006536 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006537 buf->b_p_bkc = empty_option;
6538 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539#ifdef FEAT_QUICKFIX
6540 buf->b_p_gp = empty_option;
6541 buf->b_p_mp = empty_option;
6542 buf->b_p_efm = empty_option;
6543#endif
6544 buf->b_p_ep = empty_option;
6545 buf->b_p_kp = empty_option;
6546 buf->b_p_path = empty_option;
6547 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006548 buf->b_p_tc = empty_option;
6549 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550#ifdef FEAT_FIND_ID
6551 buf->b_p_def = empty_option;
6552 buf->b_p_inc = empty_option;
6553# ifdef FEAT_EVAL
6554 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006555 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556# endif
6557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 buf->b_p_dict = empty_option;
6559 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006560#ifdef FEAT_COMPL_FUNC
6561 buf->b_p_tsrfu = empty_option;
6562#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006563 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006564 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006565#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6566 buf->b_p_bexpr = empty_option;
6567#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006568#if defined(FEAT_CRYPT)
6569 buf->b_p_cm = empty_option;
6570#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006571#ifdef FEAT_PERSISTENT_UNDO
6572 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006573 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006574#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006575 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006576 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578 /*
6579 * Don't copy the options set by ex_help(), use the saved values,
6580 * when going from a help buffer to a non-help buffer.
6581 * Don't touch these at all when BCO_NOHELP is used and going from
6582 * or to a help buffer.
6583 */
6584 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006587#ifdef FEAT_VARTABS
6588 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006589 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006590 else
6591 buf->b_p_vts_array = NULL;
6592#endif
6593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 else
6595 {
6596 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006597 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 did_isk = TRUE;
6599 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006600 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006601#ifdef FEAT_VARTABS
6602 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006603 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006604 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006605 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006606 else
6607 buf->b_p_vts_array = NULL;
6608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 if (buf->b_p_bt[0] == 'h')
6611 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006613 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 }
6615 }
6616
6617 /*
6618 * When the options should be copied (ignoring BCO_ALWAYS), set the
6619 * flag that indicates that the options have been initialized.
6620 */
6621 if (should_copy)
6622 buf->b_p_initialized = TRUE;
6623 }
6624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006625 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 if (did_isk)
6627 (void)buf_init_chartab(buf, FALSE);
6628}
6629
6630/*
6631 * Reset the 'modifiable' option and its default value.
6632 */
6633 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006634reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
6636 int opt_idx;
6637
6638 curbuf->b_p_ma = FALSE;
6639 p_ma = FALSE;
6640 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006641 if (opt_idx >= 0)
6642 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643}
6644
6645/*
6646 * Set the global value for 'iminsert' to the local value.
6647 */
6648 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006649set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
6651 p_iminsert = curbuf->b_p_iminsert;
6652}
6653
6654/*
6655 * Set the global value for 'imsearch' to the local value.
6656 */
6657 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006658set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
6660 p_imsearch = curbuf->b_p_imsearch;
6661}
6662
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663static int expand_option_idx = -1;
6664static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6665static int expand_option_flags = 0;
6666
6667 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006668set_context_in_set_cmd(
6669 expand_T *xp,
6670 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006671 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672{
6673 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006674 long_u flags = 0; // init for GCC
6675 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 char_u *p;
6677 char_u *s;
6678 int is_term_option = FALSE;
6679 int key;
6680
6681 expand_option_flags = opt_flags;
6682
6683 xp->xp_context = EXPAND_SETTINGS;
6684 if (*arg == NUL)
6685 {
6686 xp->xp_pattern = arg;
6687 return;
6688 }
6689 p = arg + STRLEN(arg) - 1;
6690 if (*p == ' ' && *(p - 1) != '\\')
6691 {
6692 xp->xp_pattern = p + 1;
6693 return;
6694 }
6695 while (p > arg)
6696 {
6697 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006698 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 if (*p == ' ' || *p == ',')
6700 {
6701 while (s > arg && *(s - 1) == '\\')
6702 --s;
6703 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006704 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 if (*p == ' ' && ((p - s) & 1) == 0)
6706 {
6707 ++p;
6708 break;
6709 }
6710 --p;
6711 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006712 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {
6714 xp->xp_context = EXPAND_BOOL_SETTINGS;
6715 p += 2;
6716 }
6717 if (STRNCMP(p, "inv", 3) == 0)
6718 {
6719 xp->xp_context = EXPAND_BOOL_SETTINGS;
6720 p += 3;
6721 }
6722 xp->xp_pattern = arg = p;
6723 if (*arg == '<')
6724 {
6725 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006726 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 return;
6728 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006729 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 {
6731 xp->xp_context = EXPAND_NOTHING;
6732 return;
6733 }
6734 nextchar = *++p;
6735 is_term_option = TRUE;
6736 expand_option_name[2] = KEY2TERMCAP0(key);
6737 expand_option_name[3] = KEY2TERMCAP1(key);
6738 }
6739 else
6740 {
6741 if (p[0] == 't' && p[1] == '_')
6742 {
6743 p += 2;
6744 if (*p != NUL)
6745 ++p;
6746 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006747 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 nextchar = *++p;
6749 is_term_option = TRUE;
6750 expand_option_name[2] = p[-2];
6751 expand_option_name[3] = p[-1];
6752 }
6753 else
6754 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006755 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6757 p++;
6758 if (*p == NUL)
6759 return;
6760 nextchar = *p;
6761 *p = NUL;
6762 opt_idx = findoption(arg);
6763 *p = nextchar;
6764 if (opt_idx == -1 || options[opt_idx].var == NULL)
6765 {
6766 xp->xp_context = EXPAND_NOTHING;
6767 return;
6768 }
6769 flags = options[opt_idx].flags;
6770 if (flags & P_BOOL)
6771 {
6772 xp->xp_context = EXPAND_NOTHING;
6773 return;
6774 }
6775 }
6776 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006777 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6779 {
6780 ++p;
6781 nextchar = '=';
6782 }
6783 if ((nextchar != '=' && nextchar != ':')
6784 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6785 {
6786 xp->xp_context = EXPAND_UNSUCCESSFUL;
6787 return;
6788 }
6789 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6790 {
6791 xp->xp_context = EXPAND_OLD_SETTING;
6792 if (is_term_option)
6793 expand_option_idx = -1;
6794 else
6795 expand_option_idx = opt_idx;
6796 xp->xp_pattern = p + 1;
6797 return;
6798 }
6799 xp->xp_context = EXPAND_NOTHING;
6800 if (is_term_option || (flags & P_NUM))
6801 return;
6802
6803 xp->xp_pattern = p + 1;
6804
6805 if (flags & P_EXPAND)
6806 {
6807 p = options[opt_idx].var;
6808 if (p == (char_u *)&p_bdir
6809 || p == (char_u *)&p_dir
6810 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006811 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814#ifdef FEAT_SESSION
6815 || p == (char_u *)&p_vdir
6816#endif
6817 )
6818 {
6819 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006820 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 xp->xp_backslash = XP_BS_THREE;
6822 else
6823 xp->xp_backslash = XP_BS_ONE;
6824 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006825 else if (p == (char_u *)&p_ft)
6826 {
6827 xp->xp_context = EXPAND_FILETYPE;
6828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 else
6830 {
6831 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006832 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 if (p == (char_u *)&p_tags)
6834 xp->xp_backslash = XP_BS_THREE;
6835 else
6836 xp->xp_backslash = XP_BS_ONE;
6837 }
6838 }
6839
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006840 // For an option that is a list of file names, find the start of the
6841 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6843 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006844 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 if (*p == ' ' || *p == ',')
6846 {
6847 s = p;
6848 while (s > xp->xp_pattern && *(s - 1) == '\\')
6849 --s;
6850 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6851 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6852 {
6853 xp->xp_pattern = p + 1;
6854 break;
6855 }
6856 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006857
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006858#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006859 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006860 if (options[opt_idx].var == (char_u *)&p_sps
6861 && STRNCMP(p, "file:", 5) == 0)
6862 {
6863 xp->xp_pattern = p + 5;
6864 break;
6865 }
6866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868}
6869
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006870/*
6871 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
6872 *
6873 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
6874 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
6875 *
6876 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
6877 * regular expression 'regmatch', then stores the match in matches[idx] and
6878 * returns TRUE.
6879 *
6880 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6881 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
6882 *
6883 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6884 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
6885 */
6886 static int
6887match_str(
6888 char_u *str,
6889 regmatch_T *regmatch,
6890 char_u **matches,
6891 int idx,
6892 int test_only,
6893 int fuzzy,
6894 char_u *fuzzystr,
6895 fuzmatch_str_T *fuzmatch)
6896{
6897 if (!fuzzy)
6898 {
6899 if (vim_regexec(regmatch, str, (colnr_T)0))
6900 {
6901 if (!test_only)
6902 matches[idx] = vim_strsave(str);
6903 return TRUE;
6904 }
6905 }
6906 else
6907 {
6908 int score;
6909
6910 score = fuzzy_match_str(str, fuzzystr);
6911 if (score != 0)
6912 {
6913 if (!test_only)
6914 {
6915 fuzmatch[idx].idx = idx;
6916 fuzmatch[idx].str = vim_strsave(str);
6917 fuzmatch[idx].score = score;
6918 }
6919
6920 return TRUE;
6921 }
6922 }
6923
6924 return FALSE;
6925}
6926
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006928ExpandSettings(
6929 expand_T *xp,
6930 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006931 char_u *fuzzystr,
6932 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01006933 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006934 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006936 int num_normal = 0; // Nr of matching non-term-code settings
6937 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 int opt_idx;
6939 int match;
6940 int count = 0;
6941 char_u *str;
6942 int loop;
6943 int is_term_opt;
6944 char_u name_buf[MAX_KEY_NAME_LEN];
6945 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006946 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006947 int fuzzy;
6948 fuzmatch_str_T *fuzmatch = NULL;
6949
Christian Brabandtcb747892022-05-08 21:10:56 +01006950 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006952 // do this loop twice:
6953 // loop == 0: count the number of matching options
6954 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 for (loop = 0; loop <= 1; ++loop)
6956 {
6957 regmatch->rm_ic = ic;
6958 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6959 {
K.Takataeeec2542021-06-02 13:28:16 +02006960 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006961 {
6962 if (match_str((char_u *)names[match], regmatch, *matches,
6963 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
6965 if (loop == 0)
6966 num_normal++;
6967 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006968 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 }
6972 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6973 opt_idx++)
6974 {
6975 if (options[opt_idx].var == NULL)
6976 continue;
6977 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6978 && !(options[opt_idx].flags & P_BOOL))
6979 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006980 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 if (is_term_opt && num_normal > 0)
6982 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006983
6984 if (match_str(str, regmatch, *matches, count, (loop == 0),
6985 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 {
6987 if (loop == 0)
6988 {
6989 if (is_term_opt)
6990 num_term++;
6991 else
6992 num_normal++;
6993 }
6994 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006995 count++;
6996 }
6997 else if (!fuzzy && options[opt_idx].shortname != NULL
6998 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006999 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007000 {
7001 // Compare against the abbreviated option name (for regular
7002 // expression match). Fuzzy matching (previous if) already
7003 // matches against both the expanded and abbreviated names.
7004 if (loop == 0)
7005 {
7006 if (is_term_opt)
7007 num_term++;
7008 else
7009 num_normal++;
7010 }
7011 else
7012 (*matches)[count++] = vim_strsave(str);
7013 }
7014 else if (is_term_opt)
7015 {
7016 name_buf[0] = '<';
7017 name_buf[1] = 't';
7018 name_buf[2] = '_';
7019 name_buf[3] = str[2];
7020 name_buf[4] = str[3];
7021 name_buf[5] = '>';
7022 name_buf[6] = NUL;
7023
7024 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7025 fuzzy, fuzzystr, fuzmatch))
7026 {
7027 if (loop == 0)
7028 num_term++;
7029 else
7030 count++;
7031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
7033 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007034
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 /*
7036 * Check terminal key codes, these are not in the option table
7037 */
7038 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7039 {
7040 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7041 {
7042 if (!isprint(str[0]) || !isprint(str[1]))
7043 continue;
7044
7045 name_buf[0] = 't';
7046 name_buf[1] = '_';
7047 name_buf[2] = str[0];
7048 name_buf[3] = str[1];
7049 name_buf[4] = NUL;
7050
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007051 if (match_str(name_buf, regmatch, *matches, count,
7052 (loop == 0), fuzzy, fuzzystr, fuzmatch))
7053 {
7054 if (loop == 0)
7055 num_term++;
7056 else
7057 count++;
7058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 else
7060 {
7061 name_buf[0] = '<';
7062 name_buf[1] = 't';
7063 name_buf[2] = '_';
7064 name_buf[3] = str[0];
7065 name_buf[4] = str[1];
7066 name_buf[5] = '>';
7067 name_buf[6] = NUL;
7068
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007069 if (match_str(name_buf, regmatch, *matches, count,
7070 (loop == 0), fuzzy, fuzzystr,
7071 fuzmatch))
7072 {
7073 if (loop == 0)
7074 num_term++;
7075 else
7076 count++;
7077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 }
7079 }
7080
7081 /*
7082 * Check special key names.
7083 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007084 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7086 {
7087 name_buf[0] = '<';
7088 STRCPY(name_buf + 1, str);
7089 STRCAT(name_buf, ">");
7090
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007091 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7092 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 {
7094 if (loop == 0)
7095 num_term++;
7096 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007097 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 }
7099 }
7100 }
7101 if (loop == 0)
7102 {
7103 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007104 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007106 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 else
7108 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007109 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007111 *matches = ALLOC_MULT(char_u *, *numMatches);
7112 if (*matches == NULL)
7113 {
7114 *matches = (char_u **)"";
7115 return FAIL;
7116 }
7117 }
7118 else
7119 {
7120 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7121 if (fuzmatch == NULL)
7122 {
7123 *matches = (char_u **)"";
7124 return FAIL;
7125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 }
7127 }
7128 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007129
7130 if (fuzzy &&
7131 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7132 return FAIL;
7133
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 return OK;
7135}
7136
7137 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007138ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007140 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 char_u *buf;
7142
zeertzjqb0d45ec2023-01-25 15:04:22 +00007143 *numMatches = 0;
7144 *matches = ALLOC_ONE(char_u *);
7145 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 return FAIL;
7147
7148 /*
7149 * For a terminal key code expand_option_idx is < 0.
7150 */
7151 if (expand_option_idx < 0)
7152 {
7153 var = find_termcode(expand_option_name + 2);
7154 if (var == NULL)
7155 expand_option_idx = findoption(expand_option_name);
7156 }
7157
7158 if (expand_option_idx >= 0)
7159 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007160 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 option_value2string(&options[expand_option_idx], expand_option_flags);
7162 var = NameBuff;
7163 }
7164 else if (var == NULL)
7165 var = (char_u *)"";
7166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007167 // A backslash is required before some characters. This is the reverse of
7168 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 buf = vim_strsave_escaped(var, escape_chars);
7170
7171 if (buf == NULL)
7172 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007173 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 return FAIL;
7175 }
7176
7177#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007178 // For MS-Windows et al. we don't double backslashes at the start and
7179 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007180 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 if (var[0] == '\\' && var[1] == '\\'
7182 && expand_option_idx >= 0
7183 && (options[expand_option_idx].flags & P_EXPAND)
7184 && vim_isfilec(var[2])
7185 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007186 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187#endif
7188
zeertzjqb0d45ec2023-01-25 15:04:22 +00007189 *matches[0] = buf;
7190 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 return OK;
7192}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
7194/*
7195 * Get the value for the numeric or string option *opp in a nice format into
7196 * NameBuff[]. Must not be called with a hidden option!
7197 */
7198 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007199option_value2string(
7200 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007201 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202{
7203 char_u *varp;
7204
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007205 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206
7207 if (opp->flags & P_NUM)
7208 {
7209 long wc = 0;
7210
7211 if (wc_use_keyname(varp, &wc))
7212 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7213 else if (wc != 0)
7214 STRCPY(NameBuff, transchar((int)wc));
7215 else
7216 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7217 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007218 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
7220 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007221 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 NameBuff[0] = NUL;
7223#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007224 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007225 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 STRCPY(NameBuff, "*****");
7227#endif
7228 else if (opp->flags & P_EXPAND)
7229 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007230 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 else if ((char_u **)opp->var == &p_pt)
7232 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7233 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007234 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 }
7236}
7237
7238/*
7239 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7240 * printed as a keyname.
7241 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7242 */
7243 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007244wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
7246 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7247 {
7248 *wcp = *(long *)varp;
7249 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7250 return TRUE;
7251 }
7252 return FALSE;
7253}
7254
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 * Return TRUE if "x" is present in 'shortmess' option, or
7257 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7258 */
7259 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007260shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007262 return p_shm != NULL &&
7263 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 || (vim_strchr(p_shm, 'a') != NULL
7265 && vim_strchr((char_u *)SHM_A, x) != NULL));
7266}
7267
7268/*
7269 * paste_option_changed() - Called after p_paste was set or reset.
7270 */
7271 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007272paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273{
7274 static int old_p_paste = FALSE;
7275 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007276 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278#ifdef FEAT_RIGHTLEFT
7279 static int save_ri = 0;
7280 static int save_hkmap = 0;
7281#endif
7282 buf_T *buf;
7283
7284 if (p_paste)
7285 {
7286 /*
7287 * Paste switched from off to on.
7288 * Save the current values, so they can be restored later.
7289 */
7290 if (!old_p_paste)
7291 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007292 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007293 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 {
7295 buf->b_p_tw_nopaste = buf->b_p_tw;
7296 buf->b_p_wm_nopaste = buf->b_p_wm;
7297 buf->b_p_sts_nopaste = buf->b_p_sts;
7298 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007299 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007300#ifdef FEAT_VARTABS
7301 if (buf->b_p_vsts_nopaste)
7302 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007303 buf->b_p_vsts_nopaste =
7304 buf->b_p_vsts && buf->b_p_vsts != empty_option
7305 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 }
7308
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007309 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007311 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313#ifdef FEAT_RIGHTLEFT
7314 save_ri = p_ri;
7315 save_hkmap = p_hkmap;
7316#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007317 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007318 p_ai_nopaste = p_ai;
7319 p_et_nopaste = p_et;
7320 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 p_tw_nopaste = p_tw;
7322 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007323#ifdef FEAT_VARTABS
7324 if (p_vsts_nopaste)
7325 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007326 p_vsts_nopaste = p_vsts && p_vsts != empty_option
7327 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 }
7330
7331 /*
7332 * Always set the option values, also when 'paste' is set when it is
7333 * already on.
7334 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007335 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007336 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007338 buf->b_p_tw = 0; // textwidth is 0
7339 buf->b_p_wm = 0; // wrapmargin is 0
7340 buf->b_p_sts = 0; // softtabstop is 0
7341 buf->b_p_ai = 0; // no auto-indent
7342 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007343#ifdef FEAT_VARTABS
7344 if (buf->b_p_vsts)
7345 free_string_option(buf->b_p_vsts);
7346 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007347 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
7350
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007351 // set global options
7352 p_sm = 0; // no showmatch
7353 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007355 status_redraw_all(); // redraw to remove the ruler
7356 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007358 p_ri = 0; // no reverse insert
7359 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007361 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 p_tw = 0;
7363 p_wm = 0;
7364 p_sts = 0;
7365 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007366#ifdef FEAT_VARTABS
7367 if (p_vsts)
7368 free_string_option(p_vsts);
7369 p_vsts = empty_option;
7370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 }
7372
7373 /*
7374 * Paste switched from on to off: Restore saved values.
7375 */
7376 else if (old_p_paste)
7377 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007378 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007379 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
7381 buf->b_p_tw = buf->b_p_tw_nopaste;
7382 buf->b_p_wm = buf->b_p_wm_nopaste;
7383 buf->b_p_sts = buf->b_p_sts_nopaste;
7384 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007385 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007386#ifdef FEAT_VARTABS
7387 if (buf->b_p_vsts)
7388 free_string_option(buf->b_p_vsts);
7389 buf->b_p_vsts = buf->b_p_vsts_nopaste
7390 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007391 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007392 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007393 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007394 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007395 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 }
7398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007399 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007401 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007403 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405#ifdef FEAT_RIGHTLEFT
7406 p_ri = save_ri;
7407 p_hkmap = save_hkmap;
7408#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007409 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007410 p_ai = p_ai_nopaste;
7411 p_et = p_et_nopaste;
7412 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 p_tw = p_tw_nopaste;
7414 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007415#ifdef FEAT_VARTABS
7416 if (p_vsts)
7417 free_string_option(p_vsts);
7418 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 }
7421
7422 old_p_paste = p_paste;
7423}
7424
7425/*
7426 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7427 *
7428 * Reset 'compatible' and set the values for options that didn't get set yet
7429 * to the Vim defaults.
7430 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007431 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 */
7433 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007434vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007436 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007437 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007438 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439
7440 if (!option_was_set((char_u *)"cp"))
7441 {
7442 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007443 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7445 set_option_default(opt_idx, OPT_FREE, FALSE);
7446 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007447 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007449
7450 if (fname != NULL)
7451 {
7452 p = vim_getenv(envname, &dofree);
7453 if (p == NULL)
7454 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007455 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007456 p = FullName_save(fname, FALSE);
7457 if (p != NULL)
7458 {
7459 vim_setenv(envname, p);
7460 vim_free(p);
7461 }
7462 }
7463 else if (dofree)
7464 vim_free(p);
7465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466}
7467
7468/*
7469 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7470 */
7471 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007472change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007474 int opt_idx;
7475
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 if (p_cp != on)
7477 {
7478 p_cp = on;
7479 compatible_set();
7480 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007481 opt_idx = findoption((char_u *)"cp");
7482 if (opt_idx >= 0)
7483 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484}
7485
7486/*
7487 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007488 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 */
7490 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007491option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492{
7493 int idx;
7494
7495 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007496 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 return FALSE;
7498 if (options[idx].flags & P_WAS_SET)
7499 return TRUE;
7500 return FALSE;
7501}
7502
7503/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007504 * Reset the flag indicating option "name" was set.
7505 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007506 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007507reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007508{
7509 int idx = findoption(name);
7510
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007511 if (idx < 0)
7512 return FAIL;
7513
7514 options[idx].flags &= ~P_WAS_SET;
7515 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007516}
7517
7518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 * compatible_set() - Called when 'compatible' has been set or unset.
7520 *
7521 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7522 * flag) to a Vi compatible value.
7523 * When 'compatible' is unset: Set all options that have a different default
7524 * for Vim (without the P_VI_DEF flag) to that default.
7525 */
7526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007527compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528{
7529 int opt_idx;
7530
Bram Moolenaardac13472019-09-16 21:06:21 +02007531 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7533 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7534 set_option_default(opt_idx, OPT_FREE, p_cp);
7535 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007536 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537}
7538
Bram Moolenaardac13472019-09-16 21:06:21 +02007539#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541/*
7542 * fill_breakat_flags() -- called when 'breakat' changes value.
7543 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007544 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007545fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007547 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 int i;
7549
7550 for (i = 0; i < 256; i++)
7551 breakat_flags[i] = FALSE;
7552
7553 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007554 for (p = p_breakat; *p; p++)
7555 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557#endif
7558
7559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 * Check if backspacing over something is allowed.
7561 */
7562 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007563can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007564 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007566#ifdef FEAT_JOB_CHANNEL
7567 if (what == BS_START && bt_prompt(curbuf))
7568 return FALSE;
7569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 switch (*p_bs)
7571 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007572 case '3': return TRUE;
7573 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 case '1': return (what != BS_START);
7575 case '0': return FALSE;
7576 }
7577 return vim_strchr(p_bs, what) != NULL;
7578}
7579
7580/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007581 * Return the effective 'scrolloff' value for the current window, using the
7582 * global value when appropriate.
7583 */
7584 long
7585get_scrolloff_value(void)
7586{
7587 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7588}
7589
7590/*
7591 * Return the effective 'sidescrolloff' value for the current window, using the
7592 * global value when appropriate.
7593 */
7594 long
7595get_sidescrolloff_value(void)
7596{
7597 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7598}
7599
7600/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007601 * Get the local or global value of 'backupcopy'.
7602 */
7603 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007604get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007605{
7606 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7607}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007608
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007609#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007610/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007611 * Get the local or global value of 'formatlistpat'.
7612 */
7613 char_u *
7614get_flp_value(buf_T *buf)
7615{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007616 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7617 return p_flp;
7618 return buf->b_p_flp;
7619}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007620#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007621
7622/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007623 * Get the local or global value of the 'virtualedit' flags.
7624 */
7625 unsigned int
7626get_ve_flags(void)
7627{
Gary Johnson51ad8502021-08-03 18:33:08 +02007628 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7629 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007630}
7631
Bram Moolenaaree857022019-11-09 23:26:40 +01007632#if defined(FEAT_LINEBREAK) || defined(PROTO)
7633/*
7634 * Get the local or global value of 'showbreak'.
7635 */
7636 char_u *
7637get_showbreak_value(win_T *win)
7638{
7639 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7640 return p_sbr;
7641 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7642 return empty_option;
7643 return win->w_p_sbr;
7644}
7645#endif
7646
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007647#if defined(FEAT_EVAL) || defined(PROTO)
7648/*
7649 * Get window or buffer local options.
7650 */
7651 dict_T *
7652get_winbuf_options(int bufopt)
7653{
7654 dict_T *d;
7655 int opt_idx;
7656
7657 d = dict_alloc();
7658 if (d == NULL)
7659 return NULL;
7660
Bram Moolenaardac13472019-09-16 21:06:21 +02007661 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007662 {
7663 struct vimoption *opt = &options[opt_idx];
7664
7665 if ((bufopt && (opt->indir & PV_BUF))
7666 || (!bufopt && (opt->indir & PV_WIN)))
7667 {
7668 char_u *varp = get_varp(opt);
7669
7670 if (varp != NULL)
7671 {
7672 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007673 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007674 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007675 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007676 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007677 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007678 }
7679 }
7680 }
7681
7682 return d;
7683}
7684#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007685
Bram Moolenaardac13472019-09-16 21:06:21 +02007686#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007687/*
7688 * This is called when 'culopt' is changed
7689 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007690 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007691fill_culopt_flags(char_u *val, win_T *wp)
7692{
7693 char_u *p;
7694 char_u culopt_flags_new = 0;
7695
7696 if (val == NULL)
7697 p = wp->w_p_culopt;
7698 else
7699 p = val;
7700 while (*p != NUL)
7701 {
7702 if (STRNCMP(p, "line", 4) == 0)
7703 {
7704 p += 4;
7705 culopt_flags_new |= CULOPT_LINE;
7706 }
7707 else if (STRNCMP(p, "both", 4) == 0)
7708 {
7709 p += 4;
7710 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7711 }
7712 else if (STRNCMP(p, "number", 6) == 0)
7713 {
7714 p += 6;
7715 culopt_flags_new |= CULOPT_NBR;
7716 }
7717 else if (STRNCMP(p, "screenline", 10) == 0)
7718 {
7719 p += 10;
7720 culopt_flags_new |= CULOPT_SCRLINE;
7721 }
7722
7723 if (*p != ',' && *p != NUL)
7724 return FAIL;
7725 if (*p == ',')
7726 ++p;
7727 }
7728
7729 // Can't have both "line" and "screenline".
7730 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7731 return FAIL;
7732 wp->w_p_culopt_flags = culopt_flags_new;
7733
7734 return OK;
7735}
7736#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007737
7738/*
7739 * Get the value of 'magic' adjusted for Vim9 script.
7740 */
7741 int
7742magic_isset(void)
7743{
7744 switch (magic_overruled)
7745 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007746 case OPTION_MAGIC_ON: return TRUE;
7747 case OPTION_MAGIC_OFF: return FALSE;
7748 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007749 }
7750#ifdef FEAT_EVAL
7751 if (in_vim9script())
7752 return TRUE;
7753#endif
7754 return p_magic;
7755}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007756
7757/*
7758 * Set the callback function value for an option that accepts a function name,
7759 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7760 * Returns OK if the option is successfully set to a function, otherwise
7761 * returns FAIL.
7762 */
7763 int
7764option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7765{
7766#ifdef FEAT_EVAL
7767 typval_T *tv;
7768 callback_T cb;
7769
7770 if (optval == NULL || *optval == NUL)
7771 {
7772 free_callback(optcb);
7773 return OK;
7774 }
7775
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007776 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007777 || (STRNCMP(optval, "function(", 9) == 0)
7778 || (STRNCMP(optval, "funcref(", 8) == 0))
7779 // Lambda expression or a funcref
7780 tv = eval_expr(optval, NULL);
7781 else
7782 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007783 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007784 if (tv == NULL)
7785 return FAIL;
7786
7787 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007788 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007789 {
7790 free_tv(tv);
7791 return FAIL;
7792 }
7793
7794 free_callback(optcb);
7795 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00007796 if (cb.cb_free_name)
7797 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007798 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007799
7800 // when using Vim9 style "import.funcname" it needs to be expanded to
7801 // "import#funcname".
7802 expand_autload_callback(optcb);
7803
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007804 return OK;
7805#else
7806 return FAIL;
7807#endif
7808}