blob: 66d681e48fff57935b51ae4a5647757aa5961dd6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000013 * - Put it in the options array in optiondefs.h (copy an existing entry).
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000016 * - Add a PV_XX macro definition to the optiondefs.h file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000019 * - For a window string option, add code to check_win_options() and
20 * clear_winopt().
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 * - For a buffer option, add some code to buf_copy_options().
22 * - For a buffer string option, add code to check_buf_options().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000023 * - If it's a numeric option, add any necessary bounds checks to
24 * set_num_option().
25 * - If it's a list of flags, add some code in did_set_string_option(), search
26 * for WW_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 * - When adding an option with expansion (P_EXPAND), but with a different
28 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020029 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * options.txt, and any other related places.
31 * - Add an entry in runtime/optwin.vim.
32 * When making changes:
33 * - Adjust the help for the option in doc/option.txt.
34 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
35 * comment at the help for the 'compatible' option.
36 */
37
38#define IN_OPTION_C
39#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020040#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010043static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static char_u *option_expand(int opt_idx, char_u *val);
46static void didset_options(void);
47static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000050#else
51# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
52#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010053static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
54static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020055static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020057static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010059static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
61static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020062static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000063static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000066static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void check_winopt(winopt_T *wop);
68static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void paste_option_changed(void);
70static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/*
73 * Initialize the options, first part.
74 *
75 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010076 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 */
78 void
Bram Moolenaar07268702018-03-01 21:57:32 +010079set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000080{
81 char_u *p;
82 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000083 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000084
85#ifdef FEAT_LANGMAP
86 langmap_init();
87#endif
88
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010089 // Be Vi compatible by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 p_cp = TRUE;
91
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010092 // Use POSIX compatibility when $VIM_POSIX is set.
Bram Moolenaar4399ef42005-02-12 14:29:27 +000093 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +000094 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +000095 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020096 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000097 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000098
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 /*
100 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 */
Bram Moolenaar7c626922005-02-07 22:01:03 +0000103 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100104#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000105 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100106 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000108 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200109#if defined(MSWIN)
110 {
111 // For MS-Windows put the path in quotes instead of escaping spaces.
112 char_u *cmd;
113 size_t len;
114
115 if (vim_strchr(p, ' ') != NULL)
116 {
117 len = STRLEN(p) + 3; // two quotes and a trailing NUL
118 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200119 if (cmd != NULL)
120 {
121 vim_snprintf((char *)cmd, len, "\"%s\"", p);
122 set_string_default("sh", cmd);
123 vim_free(cmd);
124 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200125 }
126 else
127 set_string_default("sh", p);
128 }
129#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100130 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 /*
134 * Set the default for 'backupskip' to include environment variables for
135 * temp files.
136 */
137 {
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100138#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100140#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100142#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000143 int len;
144 garray_T ga;
145 int mustfree;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200146 char_u *item;
147
148 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149
150 ga_init2(&ga, 1, 100);
K.Takataeeec2542021-06-02 13:28:16 +0200151 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000153 mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100154#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100156# ifdef MACOS_X
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200157 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100158# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160# endif
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100161 else
162#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 if (p != NULL && *p != NUL)
165 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100166 // First time count the NUL, otherwise count the ','.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000167 len = (int)STRLEN(p) + 3;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200168 item = alloc(len);
169 STRCPY(item, p);
170 add_pathsep(item);
171 STRCAT(item, "*");
172 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
173 == NULL
174 && ga_grow(&ga, len) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 {
176 if (ga.ga_len > 0)
177 STRCAT(ga.ga_data, ",");
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200178 STRCAT(ga.ga_data, item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 ga.ga_len += len;
180 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200181 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 if (mustfree)
184 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 }
186 if (ga.ga_data != NULL)
187 {
188 set_string_default("bsk", ga.ga_data);
189 vim_free(ga.ga_data);
190 }
191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193 /*
194 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
195 */
196 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000197 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000199#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
200 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
201#endif
202 {
ichizok02560422022-04-05 14:18:44 +0100203#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100204 // Use amount of memory available at this moment.
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200205 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100206#elif defined(HAVE_TOTAL_MEM)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100207 // Use amount of memory available to Vim.
Bram Moolenaar914572a2007-05-01 11:37:47 +0000208 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100209#else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000210 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000213 opt_idx = findoption((char_u *)"maxmem");
214 if (opt_idx >= 0)
215 {
216#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100217 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
218 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000219#endif
220 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
221 }
222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 }
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 {
226 char_u *cdpath;
227 char_u *buf;
228 int i;
229 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000230 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100232 // Initialize the 'cdpath' option's default value.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000233 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 if (cdpath != NULL)
235 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200236 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 if (buf != NULL)
238 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100239 buf[0] = ','; // start with ",", current dir first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 j = 1;
241 for (i = 0; cdpath[i] != NUL; ++i)
242 {
243 if (vim_ispathlistsep(cdpath[i]))
244 buf[j++] = ',';
245 else
246 {
247 if (cdpath[i] == ' ' || cdpath[i] == ',')
248 buf[j++] = '\\';
249 buf[j++] = cdpath[i];
250 }
251 }
252 buf[j] = NUL;
253 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000254 if (opt_idx >= 0)
255 {
256 options[opt_idx].def_val[VI_DEFAULT] = buf;
257 options[opt_idx].flags |= P_DEF_ALLOCED;
258 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200259 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100260 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000262 if (mustfree)
263 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 }
265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
ichizok02560422022-04-05 14:18:44 +0100267#if defined(FEAT_POSTSCRIPT) && \
268 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100269 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100271# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100273# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100275# elif defined(MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100277# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# endif
280 );
281#endif
282
283#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100284 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100286# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000287 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100288# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
290
ichizok02560422022-04-05 14:18:44 +0100291# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293# endif
294 );
295#endif
296
297 /*
298 * Set all the options (except the terminal options) to their default
299 * value. Also set the global value for local options.
300 */
301 set_options_default(0);
302
matveytadbb1bf2022-02-01 17:26:12 +0000303#ifdef UNIX
304 // Force restricted-mode on for "nologin" or "false" $SHELL
305 p = get_isolated_shell_name();
306 if (p != NULL)
307 {
308 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
309 restricted = TRUE;
310 vim_free(p);
311 }
312#endif
313
Bram Moolenaar07268702018-03-01 21:57:32 +0100314#ifdef CLEAN_RUNTIMEPATH
315 if (clean_arg)
316 {
317 opt_idx = findoption((char_u *)"runtimepath");
318 if (opt_idx >= 0)
319 {
320 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
321 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
322 }
323 opt_idx = findoption((char_u *)"packpath");
324 if (opt_idx >= 0)
325 {
326 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
327 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
328 }
329 }
330#endif
331
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#ifdef FEAT_GUI
333 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100334 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#endif
336
337 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100338 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100339 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 check_buf_options(curbuf);
341 check_win_options(curwin);
342 check_options();
343
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100344 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 didset_options();
346
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000347#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100348 // Use the current chartab for the generic chartab. This is not in
349 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000350 init_spell_chartab();
351#endif
352
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /*
354 * Expand environment variables and things like "~" for the defaults.
355 * If option_expand() returns non-NULL the variable is expanded. This can
356 * only happen for non-indirect options.
357 * Also set the default to the expanded value, so ":set" does not list
358 * them.
359 * Don't set the P_ALLOCED flag, because we don't want to free the
360 * default.
361 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200362 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
364 if ((options[opt_idx].flags & P_GETTEXT)
365 && options[opt_idx].var != NULL)
366 p = (char_u *)_(*(char **)options[opt_idx].var);
367 else
368 p = option_expand(opt_idx, NULL);
369 if (p != NULL && (p = vim_strsave(p)) != NULL)
370 {
371 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100372 // VIMEXP
373 // Defaults for all expanded options are currently the same for Vi
374 // and Vim. When this changes, add some code here! Also need to
375 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (options[opt_idx].flags & P_DEF_ALLOCED)
377 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
378 options[opt_idx].def_val[VI_DEFAULT] = p;
379 options[opt_idx].flags |= P_DEF_ALLOCED;
380 }
381 }
382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386 // Detect use of mlterm.
387 // Mlterm is a terminal emulator akin to xterm that has some special
388 // abilities (bidi namely).
389 // NOTE: mlterm's author is being asked to 'set' a variable
390 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100392 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#endif
394
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200395 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Bram Moolenaar4f974752019-02-17 17:44:42 +0100397# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 /*
399 * If $LANG isn't set, try to get a good value for it. This makes the
400 * right language be used automatically. Don't do this for English.
401 */
402 if (mch_getenv((char_u *)"LANG") == NULL)
403 {
404 char buf[20];
405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100406 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
407 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
408 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
410 (LPTSTR)buf, 20);
411 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
415 STRCPY(buf, "zh_TW");
416 else if (STRNICMP(buf, "chs", 3) == 0
417 || STRNICMP(buf, "zhc", 3) == 0)
418 STRCPY(buf, "zh_CN");
419 else if (STRNICMP(buf, "jp", 2) == 0)
420 STRCPY(buf, "ja");
421 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100423 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425 }
ichizok02560422022-04-05 14:18:44 +0100426# elif defined(MACOS_CONVERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100427 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000428 mac_lang_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429# endif
430
K.Takataf883d902021-05-30 18:04:19 +0200431# ifdef MSWIN
432 // MS-Windows has builtin support for conversion to and from Unicode, using
433 // "utf-8" for 'encoding' should work best for most users.
434 p = vim_strsave((char_u *)ENC_DFLT);
435# else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100436 // enc_locale() will try to find the encoding of the current locale.
K.Takataf883d902021-05-30 18:04:19 +0200437 // This works best for properly configured systems, old and new.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 p = enc_locale();
K.Takataf883d902021-05-30 18:04:19 +0200439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 if (p != NULL)
441 {
442 char_u *save_enc;
443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100444 // Try setting 'encoding' and check if the value is valid.
K.Takataf883d902021-05-30 18:04:19 +0200445 // If not, go back to the default encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 save_enc = p_enc;
447 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000448 if (STRCMP(p_enc, "gb18030") == 0)
449 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100450 // We don't support "gb18030", but "cp936" is a good substitute
451 // for practical purposes, thus use that. It's not an alias to
452 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000453 p_enc = vim_strsave((char_u *)"cp936");
454 vim_free(p);
455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 if (mb_init() == NULL)
457 {
458 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000459 if (opt_idx >= 0)
460 {
461 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
462 options[opt_idx].flags |= P_DEF_ALLOCED;
463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464
Bram Moolenaard0573012017-10-28 21:11:06 +0200465#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100466 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000467 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100468 // Adjust the default for 'isprint' and 'iskeyword' to match
469 // latin1. Also set the defaults for when 'nocompatible' is
470 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000471 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000472 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000473 set_string_option_direct((char_u *)"isk", -1,
474 ISK_LATIN1, OPT_FREE, SID_NONE);
475 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000476 if (opt_idx >= 0)
477 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000478 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000479 if (opt_idx >= 0)
480 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000481 (void)init_chartab();
482 }
483#endif
484
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200485#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100486 // Win32 console: When GetACP() returns a different value from
487 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200488 if (
489# ifdef VIMDLL
490 (!gui.in_use && !gui.starting) &&
491# endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100492 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 {
494 char buf[50];
495
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100496 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
497 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100498 if (GetConsoleCP() == 0)
499 sprintf(buf, "cp%ld", (long)GetACP());
500 else
501 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 p_tenc = vim_strsave((char_u *)buf);
503 if (p_tenc != NULL)
504 {
505 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000506 if (opt_idx >= 0)
507 {
508 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
509 options[opt_idx].flags |= P_DEF_ALLOCED;
510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 convert_setup(&input_conv, p_tenc, p_enc);
512 convert_setup(&output_conv, p_enc, p_tenc);
513 }
514 else
515 p_tenc = empty_option;
516 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100517#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100518#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100519 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000520 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 }
523 else
524 {
525 vim_free(p_enc);
526 p_enc = save_enc;
527 }
528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
530#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100531 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 set_helplang_default(get_mess_lang());
533#endif
534}
535
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200536static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
537
538/*
539 * Set the "fileencodings" option to the default value for when 'encoding' is
540 * utf-8.
541 */
542 void
543set_fencs_unicode()
544{
545 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
546 OPT_FREE, 0);
547}
548
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549/*
550 * Set an option to its default value.
551 * This does not take care of side effects!
552 */
553 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100554set_option_default(
555 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100556 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
557 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100559 char_u *varp; // pointer to variable for current option
560 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000562 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
564
565 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
566 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100567 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
569 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
570 if (flags & P_STRING)
571 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200572 // 'fencs' default value depends on 'encoding'
573 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
574 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100575 // Use set_string_option_direct() for local options to handle
576 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200577 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200578 set_string_option_direct(NULL, opt_idx,
579 options[opt_idx].def_val[dvi], opt_flags, 0);
580 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200582 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
583 free_string_option(*(char_u **)(varp));
584 *(char_u **)varp = options[opt_idx].def_val[dvi];
585 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587 }
588 else if (flags & P_NUM)
589 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000590 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 win_comp_scroll(curwin);
592 else
593 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100594 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
595
596 if ((long *)varp == &curwin->w_p_so
597 || (long *)varp == &curwin->w_p_siso)
598 // 'scrolloff' and 'sidescrolloff' local values have a
599 // different default value than the global default.
600 *(long *)varp = -1;
601 else
602 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100603 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 if (both)
605 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100606 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
608 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100609 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 // the cast to long is required for Manx C, long_i is needed for
612 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000613 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000614#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100615 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000616 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
617 *(int *)varp = FALSE;
618#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100619 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (both)
621 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
622 *(int *)varp;
623 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100625 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000626 flagsp = insecure_flag(opt_idx, opt_flags);
627 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
629
630#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200631 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#endif
633}
634
635/*
636 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200637 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 */
639 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100641 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000645 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
Bram Moolenaardac13472019-09-16 21:06:21 +0200647 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200648 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200649 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100650 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200651# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200652 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200653 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200654# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100655 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 set_option_default(i, opt_flags, p_cp);
657
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100658 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000659 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200661 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662}
663
664/*
665 * Set the Vi-default value of a string option.
666 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100667 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100669 static void
670set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
672 char_u *p;
673 int opt_idx;
674
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100675 if (escape && vim_strchr(val, ' ') != NULL)
676 p = vim_strsave_escaped(val, (char_u *)" ");
677 else
678 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000679 if (p == NULL) // we don't want a NULL
680 return;
681
682 opt_idx = findoption((char_u *)name);
683 if (opt_idx < 0)
684 return;
685
686 if (options[opt_idx].flags & P_DEF_ALLOCED)
687 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
688 options[opt_idx].def_val[VI_DEFAULT] = p;
689 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690}
691
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100692 void
693set_string_default(char *name, char_u *val)
694{
695 set_string_default_esc(name, val, FALSE);
696}
697
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200699 * For an option value that contains comma separated items, find "newval" in
700 * "origval". Return NULL if not found.
701 */
702 static char_u *
703find_dup_item(char_u *origval, char_u *newval, long_u flags)
704{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200705 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200706 size_t newlen;
707 char_u *s;
708
709 if (origval == NULL)
710 return NULL;
711
712 newlen = STRLEN(newval);
713 for (s = origval; *s != NUL; ++s)
714 {
715 if ((!(flags & P_COMMA)
716 || s == origval
717 || (s[-1] == ',' && !(bs & 1)))
718 && STRNCMP(s, newval, newlen) == 0
719 && (!(flags & P_COMMA)
720 || s[newlen] == ','
721 || s[newlen] == NUL))
722 return s;
723 // Count backslashes. Only a comma with an even number of backslashes
724 // or a single backslash preceded by a comma before it is recognized as
725 // a separator.
726 if ((s > origval + 1
727 && s[-1] == '\\'
728 && s[-2] != ',')
729 || (s == origval + 1
730 && s[-1] == '\\'))
731 ++bs;
732 else
733 bs = 0;
734 }
735 return NULL;
736}
737
738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 * Set the Vi-default value of a number option.
740 * Used for 'lines' and 'columns'.
741 */
742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100743set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000745 int opt_idx;
746
747 opt_idx = findoption((char_u *)name);
748 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000749 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750}
751
Dominique Pelle748b3082022-01-08 12:41:16 +0000752#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200753/*
754 * Set all window-local and buffer-local options to the Vim default.
755 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200756 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200757 */
758 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200759set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200760{
761 win_T *save_curwin = curwin;
762 int i;
763
764 curwin = wp;
765 curbuf = curwin->w_buffer;
766 block_autocmds();
767
Bram Moolenaardac13472019-09-16 21:06:21 +0200768 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200769 {
770 struct vimoption *p = &(options[i]);
771 char_u *varp = get_varp_scope(p, OPT_LOCAL);
772
773 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200774 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200775 && !(options[i].flags & P_NODEFAULT)
776 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200777 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200778 }
779
780 unblock_autocmds();
781 curwin = save_curwin;
782 curbuf = curwin->w_buffer;
783}
Dominique Pelle748b3082022-01-08 12:41:16 +0000784#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200785
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000786#if defined(EXITFREE) || defined(PROTO)
787/*
788 * Free all options.
789 */
790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100791free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000792{
793 int i;
794
Bram Moolenaardac13472019-09-16 21:06:21 +0200795 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000796 {
797 if (options[i].indir == PV_NONE)
798 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100799 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100800 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000801 free_string_option(*(char_u **)options[i].var);
802 if (options[i].flags & P_DEF_ALLOCED)
803 free_string_option(options[i].def_val[VI_DEFAULT]);
804 }
805 else if (options[i].var != VAR_WIN
806 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100807 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200808 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000809 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000810 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000811 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000812}
813#endif
814
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816/*
817 * Initialize the options, part two: After getting Rows and Columns and
818 * setting 'term'.
819 */
820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100821set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000823 int idx;
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100826 * 'scroll' defaults to half the window height. The stored default is zero,
827 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000829 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000830 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000831 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 comp_col();
833
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000834 /*
835 * 'window' is only for backwards compatibility with Vi.
836 * Default is Rows - 1.
837 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000838 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000839 p_window = Rows - 1;
840 set_number_default("window", Rows - 1);
841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100842 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100843#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000844 /*
845 * If 'background' wasn't set by the user, try guessing the value,
846 * depending on the terminal name. Only need to check for terminals
847 * with a dark background, that can handle color.
848 */
849 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000850 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
851 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000853 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 // don't mark it as set, when starting the GUI it may be
855 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000856 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000859
860#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000862#endif
863#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000865#endif
866#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100867 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869}
870
871/*
872 * Initialize the options, part three: After reading the .vimrc
873 */
874 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100875set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100877#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878/*
879 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
880 * This is done after other initializations, where 'shell' might have been
881 * set, but only if they have not been set before.
882 */
883 char_u *p;
884 int idx_srr;
885 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100886# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 int idx_sp;
888 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
891 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000892 if (idx_srr < 0)
893 do_srr = FALSE;
894 else
895 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100896# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000898 if (idx_sp < 0)
899 do_sp = FALSE;
900 else
901 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100902# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200903 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (p != NULL)
905 {
906 /*
907 * Default for p_sp is "| tee", for p_srr is ">".
908 * For known shells it is changed here to include stderr.
909 */
910 if ( fnamecmp(p, "csh") == 0
911 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100912# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 || fnamecmp(p, "csh.exe") == 0
914 || fnamecmp(p, "tcsh.exe") == 0
915# endif
916 )
917 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100918# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (do_sp)
920 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100921# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100923# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
927 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (do_srr)
930 {
931 p_srr = (char_u *)">&";
932 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
933 }
934 }
Mike Williams12795022021-06-28 20:53:58 +0200935# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200936 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
937 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200938 else if ( fnamecmp(p, "powershell") == 0
939 || fnamecmp(p, "powershell.exe") == 0
940 )
941 {
942# if defined(FEAT_QUICKFIX)
943 if (do_sp)
944 {
945 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
946 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
947 }
948# endif
949 if (do_srr)
950 {
951 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
952 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
953 }
954 }
955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 else
Natanael Copa56318362021-05-06 18:46:35 +0200957 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if ( fnamecmp(p, "sh") == 0
959 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200960 || fnamecmp(p, "mksh") == 0
961 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000963 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200965 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200966 || fnamecmp(p, "ash") == 0
967 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200968 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100969# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 || fnamecmp(p, "cmd") == 0
971 || fnamecmp(p, "sh.exe") == 0
972 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200973 || fnamecmp(p, "mksh.exe") == 0
974 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000976 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 || fnamecmp(p, "bash.exe") == 0
978 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200979 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200980 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100982 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100984# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (do_sp)
986 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100987# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100989# else
Mike Williamsa3d1b292021-06-30 20:56:00 +0200990 if (fnamecmp(p, "pwsh") == 0)
991 p_sp = (char_u *)">%s 2>&1";
992 else
993 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
996 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (do_srr)
999 {
1000 p_srr = (char_u *)">%s 2>&1";
1001 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1002 }
1003 }
1004 vim_free(p);
1005 }
1006#endif
1007
Bram Moolenaar4f974752019-02-17 17:44:42 +01001008#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001010 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1011 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001013 * set, but only if they have not been set before.
1014 * Default values depend on shell (cmd.exe is default shell):
1015 *
1016 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001017 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001018 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001019 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001020 * "sh" like shells - "-c" "\""
1021 *
1022 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 */
Mike Williams12795022021-06-28 20:53:58 +02001024 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1025 {
1026 int idx_opt;
1027
1028 idx_opt = findoption((char_u *)"shcf");
1029 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1030 {
1031 p_shcf = (char_u*)"-Command";
1032 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1033 }
1034
1035 idx_opt = findoption((char_u *)"sxq");
1036 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1037 {
1038 p_sxq = (char_u*)"\"";
1039 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1040 }
1041 }
1042 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 int idx3;
1045
1046 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001047 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
1049 p_shcf = (char_u *)"-c";
1050 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1051 }
1052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001053 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001055 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
1057 p_sxq = (char_u *)"\"";
1058 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001061 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1062 {
1063 int idx3;
1064
1065 /*
1066 * cmd.exe on Windows will strip the first and last double quote given
1067 * on the command line, e.g. most of the time things like:
1068 * cmd /c "my path/to/echo" "my args to echo"
1069 * become:
1070 * my path/to/echo" "my args to echo
1071 * when executed.
1072 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001073 * To avoid this, set shellxquote to surround the command in
1074 * parenthesis. This appears to make most commands work, without
1075 * breaking commands that worked previously, such as
1076 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001077 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001078 idx3 = findoption((char_u *)"sxq");
1079 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1080 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001081 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001082 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1083 }
1084
Bram Moolenaara64ba222012-02-12 23:23:31 +01001085 idx3 = findoption((char_u *)"shcf");
1086 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1087 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001088 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001089 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1090 }
1091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#endif
1093
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001094 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001095 {
1096 int idx_ffs = findoption((char_u *)"ffs");
1097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001098 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001099 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1100 set_fileformat(default_fileformat(), OPT_LOCAL);
1101 }
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104}
1105
1106#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1107/*
1108 * When 'helplang' is still at its default value, set it to "lang".
1109 * Only the first two characters of "lang" are used.
1110 */
1111 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001112set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
1114 int idx;
1115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001116 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 return;
1118 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001119 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1120 return;
1121
1122 if (options[idx].flags & P_ALLOCED)
1123 free_string_option(p_hlg);
1124 p_hlg = vim_strsave(lang);
1125 if (p_hlg == NULL)
1126 p_hlg = empty_option;
1127 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001129 // zh_CN becomes "cn", zh_TW becomes "tw"
1130 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001131 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001132 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1133 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001134 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001135 // any C like setting, such as C.UTF-8, becomes "en"
1136 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1137 {
1138 p_hlg[0] = 'e';
1139 p_hlg[1] = 'n';
1140 }
1141 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001143 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144}
1145#endif
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147/*
1148 * 'title' and 'icon' only default to true if they have not been set or reset
1149 * in .vimrc and we can read the old value.
1150 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1151 * they can be reset. This reduces startup time when using X on a remote
1152 * machine.
1153 */
1154 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001155set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
1157 int idx1;
1158 long val;
1159
1160 /*
1161 * If GUI is (going to be) used, we can always set the window title and
1162 * icon name. Saves a bit of time, because the X11 display server does
1163 * not need to be contacted.
1164 */
1165 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001166 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
1168#ifdef FEAT_GUI
1169 if (gui.starting || gui.in_use)
1170 val = TRUE;
1171 else
1172#endif
1173 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001174 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 p_title = val;
1176 }
1177 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001178 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001180 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001182
1183#ifdef FEAT_GUI
1184 if (gui.starting || gui.in_use)
1185 val = TRUE;
1186 else
1187#endif
1188 val = mch_can_restore_icon();
1189 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1190 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001193 void
1194ex_set(exarg_T *eap)
1195{
1196 int flags = 0;
1197
1198 if (eap->cmdidx == CMD_setlocal)
1199 flags = OPT_LOCAL;
1200 else if (eap->cmdidx == CMD_setglobal)
1201 flags = OPT_GLOBAL;
1202#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001203 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001204 ex_options(eap);
1205 else
1206#endif
1207 {
1208 if (eap->forceit)
1209 flags |= OPT_ONECOLUMN;
1210 (void)do_set(eap->arg, flags);
1211 }
1212}
1213
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001214/*
1215 * :set operator types
1216 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001217typedef enum {
1218 OP_NONE = 0,
1219 OP_ADDING, // "opt+=arg"
1220 OP_PREPENDING, // "opt^=arg"
1221 OP_REMOVING, // "opt-=arg"
1222} set_op_T;
1223
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001224typedef enum {
1225 PREFIX_NO = 0, // "no" prefix
1226 PREFIX_NONE, // no prefix
1227 PREFIX_INV, // "inv" prefix
1228} set_prefix_T;
1229
1230/*
1231 * Return the prefix type for the option name in *argp.
1232 */
1233 static set_prefix_T
1234get_option_prefix(char_u **argp)
1235{
1236 int prefix = PREFIX_NONE;
1237 char_u *arg = *argp;
1238
1239 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1240 {
1241 prefix = PREFIX_NO;
1242 arg += 2;
1243 }
1244 else if (STRNCMP(arg, "inv", 3) == 0)
1245 {
1246 prefix = PREFIX_INV;
1247 arg += 3;
1248 }
1249
1250 *argp = arg;
1251 return prefix;
1252}
1253
1254/*
1255 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1256 * and the option name length in "*lenp". For a <t_xx> option, return the key
1257 * number in "*keyp".
1258 *
1259 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1260 * otherwise returns OK.
1261 */
1262 static int
1263parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1264{
1265 int key = 0;
1266 int len;
1267 int opt_idx;
1268
1269 if (*arg == '<')
1270 {
1271 opt_idx = -1;
1272 // look out for <t_>;>
1273 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1274 len = 5;
1275 else
1276 {
1277 len = 1;
1278 while (arg[len] != NUL && arg[len] != '>')
1279 ++len;
1280 }
1281 if (arg[len] != '>')
1282 return FAIL;
1283
1284 arg[len] = NUL; // put NUL after name
1285 if (arg[1] == 't' && arg[2] == '_') // could be term code
1286 opt_idx = findoption(arg + 1);
1287 arg[len++] = '>'; // restore '>'
1288 if (opt_idx == -1)
1289 key = find_key_option(arg + 1, TRUE);
1290 }
1291 else
1292 {
1293 int nextchar; // next non-white char after option name
1294
1295 len = 0;
1296 /*
1297 * The two characters after "t_" may not be alphanumeric.
1298 */
1299 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1300 len = 4;
1301 else
1302 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1303 ++len;
1304 nextchar = arg[len];
1305 arg[len] = NUL; // put NUL after name
1306 opt_idx = findoption(arg);
1307 arg[len] = nextchar; // restore nextchar
1308 if (opt_idx == -1)
1309 key = find_key_option(arg, FALSE);
1310 }
1311
1312 *keyp = key;
1313 *lenp = len;
1314 *opt_idxp = opt_idx;
1315
1316 return OK;
1317}
1318
1319/*
1320 * Get the option operator (+=, ^=, -=).
1321 */
1322 static set_op_T
1323get_opt_op(char_u *arg)
1324{
1325 set_op_T op = OP_NONE;
1326
1327 if (*arg != NUL && *(arg + 1) == '=')
1328 {
1329 if (*arg == '+')
1330 op = OP_ADDING; // "+="
1331 else if (*arg == '^')
1332 op = OP_PREPENDING; // "^="
1333 else if (*arg == '-')
1334 op = OP_REMOVING; // "-="
1335 }
1336
1337 return op;
1338}
1339
1340/*
1341 * Validate whether the value of the option in "opt_idx" can be changed.
1342 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1343 * if it can be changed.
1344 */
1345 static int
1346validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1347{
1348 // Skip all options that are not window-local (used when showing
1349 // an already loaded buffer in a window).
1350 if ((opt_flags & OPT_WINONLY)
1351 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1352 return FAIL;
1353
1354 // Skip all options that are window-local (used for :vimgrep).
1355 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1356 && options[opt_idx].var == VAR_WIN)
1357 return FAIL;
1358
1359 // Disallow changing some options from modelines.
1360 if (opt_flags & OPT_MODELINE)
1361 {
1362 if (flags & (P_SECURE | P_NO_ML))
1363 {
1364 *errmsg = e_not_allowed_in_modeline;
1365 return FAIL;
1366 }
1367 if ((flags & P_MLE) && !p_mle)
1368 {
1369 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1370 return FAIL;
1371 }
1372#ifdef FEAT_DIFF
1373 // In diff mode some options are overruled. This avoids that
1374 // 'foldmethod' becomes "marker" instead of "diff" and that
1375 // "wrap" gets set.
1376 if (curwin->w_p_diff
1377 && opt_idx >= 0 // shut up coverity warning
1378 && (
1379# ifdef FEAT_FOLDING
1380 options[opt_idx].indir == PV_FDM ||
1381# endif
1382 options[opt_idx].indir == PV_WRAP))
1383 return FAIL;
1384#endif
1385 }
1386
1387#ifdef HAVE_SANDBOX
1388 // Disallow changing some options in the sandbox
1389 if (sandbox != 0 && (flags & P_SECURE))
1390 {
1391 *errmsg = e_not_allowed_in_sandbox;
1392 return FAIL;
1393 }
1394#endif
1395
1396 return OK;
1397}
1398
Bram Moolenaar47403942022-09-21 21:12:53 +01001399/*
1400 * Part of do_set() for string options.
1401 * Returns FAIL on failure, do not process further options.
1402 */
1403 static int
1404do_set_string(
1405 int opt_idx,
1406 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001407 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001408 int nextchar,
1409 set_op_T op_arg,
1410 int flags,
1411 int cp_val,
1412 char_u *varp_arg,
1413 char *errbuf,
1414 int *value_checked,
1415 char **errmsg)
1416{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001417 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001418 set_op_T op = op_arg;
1419 char_u *varp = varp_arg;
1420 char_u *save_arg = NULL;
1421 char_u *s = NULL;
1422 char_u *oldval = NULL; // previous value if *varp
1423 char_u *newval;
1424 char_u *origval = NULL;
1425 char_u *origval_l = NULL;
1426 char_u *origval_g = NULL;
1427#if defined(FEAT_EVAL)
1428 char_u *saved_origval = NULL;
1429 char_u *saved_origval_l = NULL;
1430 char_u *saved_origval_g = NULL;
1431 char_u *saved_newval = NULL;
1432#endif
1433 unsigned newlen;
1434 int comma;
1435 char_u whichwrap[80];
1436
1437 // When using ":set opt=val" for a global option
1438 // with a local value the local value will be
1439 // reset, use the global value here.
1440 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1441 && ((int)options[opt_idx].indir & PV_BOTH))
1442 varp = options[opt_idx].var;
1443
1444 // The old value is kept until we are sure that the new value is valid.
1445 oldval = *(char_u **)varp;
1446
1447 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1448 {
1449 origval_l = *(char_u **)get_varp_scope(
1450 &(options[opt_idx]), OPT_LOCAL);
1451 origval_g = *(char_u **)get_varp_scope(
1452 &(options[opt_idx]), OPT_GLOBAL);
1453
1454 // A global-local string option might have an empty option as value to
1455 // indicate that the global value should be used.
1456 if (((int)options[opt_idx].indir & PV_BOTH)
1457 && origval_l == empty_option)
1458 origval_l = origval_g;
1459 }
1460
1461 // When setting the local value of a global option, the old value may be
1462 // the global value.
1463 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1464 origval = *(char_u **)get_varp(&options[opt_idx]);
1465 else
1466 origval = oldval;
1467
1468 if (nextchar == '&') // set to default val
1469 {
1470 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1471 ? VI_DEFAULT : VIM_DEFAULT];
1472 if ((char_u **)varp == &p_bg)
1473 {
1474 // guess the value of 'background'
1475#ifdef FEAT_GUI
1476 if (gui.in_use)
1477 newval = gui_bg_default();
1478 else
1479#endif
1480 newval = term_bg_default();
1481 }
1482 else if ((char_u **)varp == &p_fencs && enc_utf8)
1483 newval = fencs_utf8_default;
1484
1485 // expand environment variables and ~ since the default value was
1486 // already expanded, only required when an environment variable was set
1487 // later
1488 if (newval == NULL)
1489 newval = empty_option;
1490 else
1491 {
1492 s = option_expand(opt_idx, newval);
1493 if (s == NULL)
1494 s = newval;
1495 newval = vim_strsave(s);
1496 }
1497 }
1498 else if (nextchar == '<') // set to global val
1499 {
1500 newval = vim_strsave(*(char_u **)get_varp_scope(
1501 &(options[opt_idx]), OPT_GLOBAL));
1502 }
1503 else
1504 {
Bram Moolenaar6f981142022-09-22 12:48:58 +01001505 ++arg; // jump to after the '=' or ':'
Bram Moolenaar47403942022-09-21 21:12:53 +01001506
1507 /*
1508 * Set 'keywordprg' to ":help" if an empty
1509 * value was passed to :set by the user.
Bram Moolenaar47403942022-09-21 21:12:53 +01001510 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001511 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
Bram Moolenaar47403942022-09-21 21:12:53 +01001512 {
Bram Moolenaar6f981142022-09-22 12:48:58 +01001513 save_arg = arg;
zeertzjqfcba86c2022-09-22 13:57:32 +01001514 arg = (char_u *)":help";
Bram Moolenaar47403942022-09-21 21:12:53 +01001515 }
1516 /*
1517 * Convert 'backspace' number to string, for
1518 * adding, prepending and removing string.
1519 */
1520 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1521 {
1522 int i = getdigits((char_u **)varp);
1523
1524 switch (i)
1525 {
1526 case 0:
1527 *(char_u **)varp = empty_option;
1528 break;
1529 case 1:
1530 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1531 break;
1532 case 2:
1533 *(char_u **)varp = vim_strsave(
1534 (char_u *)"indent,eol,start");
1535 break;
1536 case 3:
1537 *(char_u **)varp = vim_strsave(
1538 (char_u *)"indent,eol,nostop");
1539 break;
1540 }
1541 vim_free(oldval);
1542 if (origval == oldval)
1543 origval = *(char_u **)varp;
1544 if (origval_l == oldval)
1545 origval_l = *(char_u **)varp;
1546 if (origval_g == oldval)
1547 origval_g = *(char_u **)varp;
1548 oldval = *(char_u **)varp;
1549 }
1550 /*
1551 * Convert 'whichwrap' number to string, for backwards compatibility
1552 * with Vim 3.0.
1553 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001554 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
Bram Moolenaar47403942022-09-21 21:12:53 +01001555 {
1556 *whichwrap = NUL;
Bram Moolenaar6f981142022-09-22 12:48:58 +01001557 int i = getdigits(&arg);
Bram Moolenaar47403942022-09-21 21:12:53 +01001558 if (i & 1)
1559 STRCAT(whichwrap, "b,");
1560 if (i & 2)
1561 STRCAT(whichwrap, "s,");
1562 if (i & 4)
1563 STRCAT(whichwrap, "h,l,");
1564 if (i & 8)
1565 STRCAT(whichwrap, "<,>,");
1566 if (i & 16)
1567 STRCAT(whichwrap, "[,],");
1568 if (*whichwrap != NUL) // remove trailing ,
1569 whichwrap[STRLEN(whichwrap) - 1] = NUL;
Bram Moolenaar6f981142022-09-22 12:48:58 +01001570 save_arg = arg;
1571 arg = (char_u *)whichwrap;
Bram Moolenaar47403942022-09-21 21:12:53 +01001572 }
1573 /*
1574 * Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1575 * version 3.0
1576 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001577 else if (*arg == '>' && (varp == (char_u *)&p_dir
Bram Moolenaar47403942022-09-21 21:12:53 +01001578 || varp == (char_u *)&p_bdir))
Bram Moolenaar6f981142022-09-22 12:48:58 +01001579 ++arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001580
1581 /*
1582 * Copy the new string into allocated memory.
1583 * Can't use set_string_option_direct(), because we need to remove the
1584 * backslashes.
1585 */
1586 // get a bit too much
Bram Moolenaar6f981142022-09-22 12:48:58 +01001587 newlen = (unsigned)STRLEN(arg) + 1;
Bram Moolenaar47403942022-09-21 21:12:53 +01001588 if (op != OP_NONE)
1589 newlen += (unsigned)STRLEN(origval) + 1;
1590 newval = alloc(newlen);
1591 if (newval == NULL) // out of mem, don't change
1592 return FAIL;
1593 s = newval;
1594
1595 /*
1596 * Copy the string, skip over escaped chars.
1597 * For MS-DOS and WIN32 backslashes before normal file name characters
1598 * are not removed, and keep backslash at start, for "\\machine\path",
1599 * but do remove it for "\\\\machine\\path".
1600 * The reverse is found in ExpandOldSetting().
1601 */
zeertzjqfcba86c2022-09-22 13:57:32 +01001602 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar47403942022-09-21 21:12:53 +01001603 {
1604 int i;
1605
Bram Moolenaar6f981142022-09-22 12:48:58 +01001606 if (*arg == '\\' && arg[1] != NUL
Bram Moolenaar47403942022-09-21 21:12:53 +01001607#ifdef BACKSLASH_IN_FILENAME
1608 && !((flags & P_EXPAND)
Bram Moolenaar6f981142022-09-22 12:48:58 +01001609 && vim_isfilec(arg[1])
1610 && !VIM_ISWHITE(arg[1])
1611 && (arg[1] != '\\'
zeertzjqfcba86c2022-09-22 13:57:32 +01001612 || (s == newval && arg[2] != '\\')))
Bram Moolenaar47403942022-09-21 21:12:53 +01001613#endif
1614 )
Bram Moolenaar6f981142022-09-22 12:48:58 +01001615 ++arg; // remove backslash
1616 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar47403942022-09-21 21:12:53 +01001617 {
1618 // copy multibyte char
Bram Moolenaar6f981142022-09-22 12:48:58 +01001619 mch_memmove(s, arg, (size_t)i);
1620 arg += i;
Bram Moolenaar47403942022-09-21 21:12:53 +01001621 s += i;
1622 }
1623 else
Bram Moolenaar6f981142022-09-22 12:48:58 +01001624 *s++ = *arg++;
Bram Moolenaar47403942022-09-21 21:12:53 +01001625 }
1626 *s = NUL;
1627
1628 /*
1629 * Expand environment variables and ~.
1630 * Don't do it when adding without inserting a comma.
1631 */
1632 if (op == OP_NONE || (flags & P_COMMA))
1633 {
1634 s = option_expand(opt_idx, newval);
1635 if (s != NULL)
1636 {
1637 vim_free(newval);
1638 newlen = (unsigned)STRLEN(s) + 1;
1639 if (op != OP_NONE)
1640 newlen += (unsigned)STRLEN(origval) + 1;
1641 newval = alloc(newlen);
1642 if (newval == NULL)
1643 return FAIL;
1644 STRCPY(newval, s);
1645 }
1646 }
1647
1648 // locate newval[] in origval[] when removing it
1649 // and when adding to avoid duplicates
1650 int len = 0;
1651 if (op == OP_REMOVING || (flags & P_NODUP))
1652 {
1653 len = (int)STRLEN(newval);
1654 s = find_dup_item(origval, newval, flags);
1655
1656 // do not add if already there
1657 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1658 {
1659 op = OP_NONE;
1660 STRCPY(newval, origval);
1661 }
1662
1663 // if no duplicate, move pointer to end of original value
1664 if (s == NULL)
1665 s = origval + (int)STRLEN(origval);
1666 }
1667
1668 // concatenate the two strings; add a ',' if needed
1669 if (op == OP_ADDING || op == OP_PREPENDING)
1670 {
1671 comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1672 if (op == OP_ADDING)
1673 {
1674 len = (int)STRLEN(origval);
1675 // strip a trailing comma, would get 2
1676 if (comma && len > 1
1677 && (flags & P_ONECOMMA) == P_ONECOMMA
1678 && origval[len - 1] == ','
1679 && origval[len - 2] != '\\')
1680 len--;
1681 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1682 mch_memmove(newval, origval, (size_t)len);
1683 }
1684 else
1685 {
1686 len = (int)STRLEN(newval);
1687 STRMOVE(newval + len + comma, origval);
1688 }
1689 if (comma)
1690 newval[len] = ',';
1691 }
1692
1693 // Remove newval[] from origval[]. (Note: "len" has been set above and
1694 // is used here).
1695 if (op == OP_REMOVING)
1696 {
1697 STRCPY(newval, origval);
1698 if (*s)
1699 {
1700 // may need to remove a comma
1701 if (flags & P_COMMA)
1702 {
1703 if (s == origval)
1704 {
1705 // include comma after string
1706 if (s[len] == ',')
1707 ++len;
1708 }
1709 else
1710 {
1711 // include comma before string
1712 --s;
1713 ++len;
1714 }
1715 }
1716 STRMOVE(newval + (s - origval), s + len);
1717 }
1718 }
1719
1720 if (flags & P_FLAGLIST)
1721 {
1722 // Remove flags that appear twice.
1723 for (s = newval; *s;)
1724 {
1725 // if options have P_FLAGLIST and P_ONECOMMA such as
1726 // 'whichwrap'
1727 if (flags & P_ONECOMMA)
1728 {
1729 if (*s != ',' && *(s + 1) == ','
1730 && vim_strchr(s + 2, *s) != NULL)
1731 {
1732 // Remove the duplicated value and the next comma.
1733 STRMOVE(s, s + 2);
1734 continue;
1735 }
1736 }
1737 else
1738 {
1739 if ((!(flags & P_COMMA) || *s != ',')
1740 && vim_strchr(s + 1, *s) != NULL)
1741 {
1742 STRMOVE(s, s + 1);
1743 continue;
1744 }
1745 }
1746 ++s;
1747 }
1748 }
1749
zeertzjqfcba86c2022-09-22 13:57:32 +01001750 if (save_arg != NULL)
1751 arg = save_arg; // arg was temporarily changed, restore it
Bram Moolenaar47403942022-09-21 21:12:53 +01001752 }
1753
1754 /*
1755 * Set the new value.
1756 */
1757 *(char_u **)(varp) = newval;
1758
1759#if defined(FEAT_EVAL)
1760 if (!starting
1761# ifdef FEAT_CRYPT
1762 && options[opt_idx].indir != PV_KEY
1763# endif
1764 && origval != NULL && newval != NULL)
1765 {
1766 // origval may be freed by did_set_string_option(), make a copy.
1767 saved_origval = vim_strsave(origval);
1768 // newval (and varp) may become invalid if the buffer is closed by
1769 // autocommands.
1770 saved_newval = vim_strsave(newval);
1771 if (origval_l != NULL)
1772 saved_origval_l = vim_strsave(origval_l);
1773 if (origval_g != NULL)
1774 saved_origval_g = vim_strsave(origval_g);
1775 }
1776#endif
1777
1778 {
1779 long_u *p = insecure_flag(opt_idx, opt_flags);
1780 int secure_saved = secure;
1781
1782 // When an option is set in the sandbox, from a modeline or in secure
1783 // mode, then deal with side effects in secure mode. Also when the
1784 // value was set with the P_INSECURE flag and is not completely
1785 // replaced.
1786 if ((opt_flags & OPT_MODELINE)
1787#ifdef HAVE_SANDBOX
1788 || sandbox != 0
1789#endif
1790 || (op != OP_NONE && (*p & P_INSECURE)))
1791 secure = 1;
1792
1793 // Handle side effects, and set the global value for ":set" on local
1794 // options. Note: when setting 'syntax' or 'filetype' autocommands may
1795 // be triggered that can cause havoc.
1796 *errmsg = did_set_string_option(
1797 opt_idx, (char_u **)varp, oldval, errbuf,
1798 opt_flags, value_checked);
1799
1800 secure = secure_saved;
1801 }
1802
1803#if defined(FEAT_EVAL)
1804 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00001805 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01001806 saved_origval_l, saved_origval_g, saved_newval);
1807 vim_free(saved_origval);
1808 vim_free(saved_origval_l);
1809 vim_free(saved_origval_g);
1810 vim_free(saved_newval);
1811#endif
1812
Bram Moolenaar6f981142022-09-22 12:48:58 +01001813 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001814 return *errmsg == NULL ? OK : FAIL;
1815}
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817/*
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/*
3482 * Set the value of a number option, and take care of side effects.
3483 * Returns NULL for success, or an error message for an error.
3484 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003485 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003486set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003487 int opt_idx, // index in options[] table
3488 char_u *varp, // pointer to the option variable
3489 long value, // new value
3490 char *errbuf, // buffer for error messages
3491 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003492 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3493 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003495 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003497#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003498 long old_global_value = 0; // only used when setting a local and
3499 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003500#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003501 long old_Rows = Rows; // remember old Rows
3502 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 long *pp = (long *)varp;
3504
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003505 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003506 if ((secure
3507#ifdef HAVE_SANDBOX
3508 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003510 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003511 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003513#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003514 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003515 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003516 // value (since there is only one value).
3517 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003518 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3519 OPT_GLOBAL);
3520#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003521
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 *pp = value;
3523#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003524 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003525 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003527#ifdef FEAT_GUI
3528 need_mouse_correct = TRUE;
3529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
Bram Moolenaar14f24742012-08-08 18:01:05 +02003531 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003533 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003534#ifdef FEAT_VARTABS
3535 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3536 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003537 ? tabstop_first(curbuf->b_p_vts_array)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003538 : curbuf->b_p_ts;
3539#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543
3544 /*
3545 * Number options that need some action when changed
3546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (pp == &p_wh || pp == &p_hh)
3548 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003549 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (p_wh < 1)
3551 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003552 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 p_wh = 1;
3554 }
3555 if (p_wmh > p_wh)
3556 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003557 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 p_wh = p_wmh;
3559 }
3560 if (p_hh < 0)
3561 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003562 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 p_hh = 0;
3564 }
3565
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003566 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003567 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
3569 if (pp == &p_wh && curwin->w_height < p_wh)
3570 win_setheight((int)p_wh);
3571 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3572 win_setheight((int)p_hh);
3573 }
3574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 else if (pp == &p_wmh)
3576 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003577 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (p_wmh < 0)
3579 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003580 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 p_wmh = 0;
3582 }
3583 if (p_wmh > p_wh)
3584 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003585 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 p_wmh = p_wh;
3587 }
3588 win_setminheight();
3589 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003590 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003592 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 if (p_wiw < 1)
3594 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003595 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 p_wiw = 1;
3597 }
3598 if (p_wmw > p_wiw)
3599 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003600 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 p_wiw = p_wmw;
3602 }
3603
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003604 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003605 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 win_setwidth((int)p_wiw);
3607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 else if (pp == &p_wmw)
3609 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003610 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (p_wmw < 0)
3612 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003613 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 p_wmw = 0;
3615 }
3616 if (p_wmw > p_wiw)
3617 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003618 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 p_wmw = p_wiw;
3620 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003621 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003624 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 else if (pp == &p_ls)
3626 {
3627 last_status(FALSE);
3628 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003629
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003630 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003631 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003632 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003633 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
3636#ifdef FEAT_GUI
3637 else if (pp == &p_linespace)
3638 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003639 // Recompute gui.char_height and resize the Vim window to keep the
3640 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003641 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003642 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644#endif
3645
3646#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003647 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 else if (pp == &curwin->w_p_fdl)
3649 {
3650 if (curwin->w_p_fdl < 0)
3651 curwin->w_p_fdl = 0;
3652 newFoldLevel();
3653 }
3654
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003655 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 else if (pp == &curwin->w_p_fml)
3657 {
3658 foldUpdateAll(curwin);
3659 }
3660
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003661 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 else if (pp == &curwin->w_p_fdn)
3663 {
3664 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3665 foldUpdateAll(curwin);
3666 }
3667
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003668 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 else if (pp == &curwin->w_p_fdc)
3670 {
3671 if (curwin->w_p_fdc < 0)
3672 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003673 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 curwin->w_p_fdc = 0;
3675 }
3676 else if (curwin->w_p_fdc > 12)
3677 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003678 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 curwin->w_p_fdc = 12;
3680 }
3681 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003682#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003684 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3686 {
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003687#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 if (foldmethodIsIndent(curwin))
3689 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003690#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003691 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3692 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003693 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3694 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003697 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003698 else if (pp == &p_mco)
3699 {
3700 if (p_mco > MAX_MCO)
3701 p_mco = MAX_MCO;
3702 else if (p_mco < 0)
3703 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003704 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003705 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 else if (pp == &curbuf->b_p_iminsert)
3708 {
3709 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3710 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003711 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 curbuf->b_p_iminsert = B_IMODE_NONE;
3713 }
3714 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003715 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003717#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003718 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 status_redraw_curbuf();
3720#endif
3721 }
3722
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003723#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003724 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003725 else if (pp == &p_imst)
3726 {
3727 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003728 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003729 }
3730#endif
3731
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003732 else if (pp == &p_window)
3733 {
3734 if (p_window < 1)
3735 p_window = 1;
3736 else if (p_window >= Rows)
3737 p_window = Rows - 1;
3738 }
3739
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 else if (pp == &curbuf->b_p_imsearch)
3741 {
3742 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3743 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003744 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 curbuf->b_p_imsearch = B_IMODE_NONE;
3746 }
3747 p_imsearch = curbuf->b_p_imsearch;
3748 }
3749
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003750 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 else if (pp == &p_titlelen)
3752 {
3753 if (p_titlelen < 0)
3754 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003755 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 p_titlelen = 85;
3757 }
3758 if (starting != NO_SCREEN && old_value != p_titlelen)
3759 need_maketitle = TRUE;
3760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003762 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 else if (pp == &p_ch)
3764 {
Bram Moolenaara2a89732022-08-31 14:46:18 +01003765 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003767 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 p_ch = 1;
3769 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003770 if (p_ch > Rows - min_rows() + 1)
3771 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003773 // Only compute the new window layout when startup has been
3774 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaarc9f5f732022-10-06 11:39:06 +01003775 if ((p_ch != old_value
3776 || tabline_height() + topframe->fr_height != Rows - p_ch)
Bram Moolenaar0816f472022-10-05 15:42:32 +01003777 && full_screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778#ifdef FEAT_GUI
3779 && !gui.starting
3780#endif
Bram Moolenaar0816f472022-10-05 15:42:32 +01003781 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003782 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003785 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 else if (pp == &p_uc)
3787 {
3788 if (p_uc < 0)
3789 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003790 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 p_uc = 100;
3792 }
3793 if (p_uc && !old_value)
3794 ml_open_files();
3795 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003796#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003797 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003798 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003799 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003800 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003801 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003802 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003803 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003804 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003805 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003806 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003807 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003808 }
3809 }
3810#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003811#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003812 else if (pp == &p_mzq)
3813 mzvim_reset_timer();
3814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003816#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003817 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003818 else if (pp == &p_pyx)
3819 {
3820 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003821 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003822 }
3823#endif
3824
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003825 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 else if (pp == &p_ul)
3827 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003828 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003830 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 p_ul = value;
3832 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003833 else if (pp == &curbuf->b_p_ul)
3834 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003835 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003836 curbuf->b_p_ul = old_value;
3837 u_sync(TRUE);
3838 curbuf->b_p_ul = value;
3839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003841#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003842 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003843 else if (pp == &curwin->w_p_nuw)
3844 {
3845 if (curwin->w_p_nuw < 1)
3846 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003847 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003848 curwin->w_p_nuw = 1;
3849 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003850 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003851 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003852 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003853 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003854 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003855 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003856 }
3857#endif
3858
Bram Moolenaar1a384422010-07-14 19:53:30 +02003859 else if (pp == &curbuf->b_p_tw)
3860 {
3861 if (curbuf->b_p_tw < 0)
3862 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003863 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003864 curbuf->b_p_tw = 0;
3865 }
3866#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003867 {
3868 win_T *wp;
3869 tabpage_T *tp;
3870
3871 FOR_ALL_TAB_WINDOWS(tp, wp)
3872 check_colorcolumn(wp);
3873 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003874#endif
3875 }
3876
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 /*
3878 * Check the bounds for numeric options here
3879 */
3880 if (Rows < min_rows() && full_screen)
3881 {
3882 if (errbuf != NULL)
3883 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003884 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003885 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 errmsg = errbuf;
3887 }
3888 Rows = min_rows();
3889 }
3890 if (Columns < MIN_COLUMNS && full_screen)
3891 {
3892 if (errbuf != NULL)
3893 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003894 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003895 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 errmsg = errbuf;
3897 }
3898 Columns = MIN_COLUMNS;
3899 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003900 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 /*
3903 * If the screen (shell) height has been changed, assume it is the
3904 * physical screenheight.
3905 */
3906 if (old_Rows != Rows || old_Columns != Columns)
3907 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003908 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 if (updating_screen)
3910 *pp = old_value;
3911 else if (full_screen
3912#ifdef FEAT_GUI
3913 && !gui.starting
3914#endif
3915 )
3916 set_shellsize((int)Columns, (int)Rows, TRUE);
3917 else
3918 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003919 // Postpone the resizing; check the size and cmdline position for
3920 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 check_shellsize();
3922 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3923 cmdline_row = Rows - p_ch;
3924 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003925 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003926 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
3928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if (curbuf->b_p_ts <= 0)
3930 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003931 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 curbuf->b_p_ts = 8;
3933 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003934 else if (curbuf->b_p_ts > TABSTOP_MAX)
3935 {
3936 errmsg = e_invalid_argument;
3937 curbuf->b_p_ts = 8;
3938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 if (p_tm < 0)
3940 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003941 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 p_tm = 0;
3943 }
3944 if ((curwin->w_p_scr <= 0
3945 || (curwin->w_p_scr > curwin->w_height
3946 && curwin->w_height > 0))
3947 && full_screen)
3948 {
3949 if (pp == &(curwin->w_p_scr))
3950 {
3951 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003952 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 win_comp_scroll(curwin);
3954 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003955 // If 'scroll' became invalid because of a side effect silently adjust
3956 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 else if (curwin->w_p_scr <= 0)
3958 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003959 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 curwin->w_p_scr = curwin->w_height;
3961 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003962 if (p_hi < 0)
3963 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003964 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003965 p_hi = 0;
3966 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003967 else if (p_hi > 10000)
3968 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003969 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003970 p_hi = 10000;
3971 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003972 if (p_re < 0 || p_re > 2)
3973 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003974 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003975 p_re = 0;
3976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 if (p_report < 0)
3978 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003979 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 p_report = 1;
3981 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003982 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003984 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 p_sj = Rows / 2;
3986 else
3987 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003988 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 p_sj = 1;
3990 }
3991 }
3992 if (p_so < 0 && full_screen)
3993 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003994 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 p_so = 0;
3996 }
3997 if (p_siso < 0 && full_screen)
3998 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003999 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 p_siso = 0;
4001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 if (p_cwh < 1)
4003 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004004 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 p_cwh = 1;
4006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 if (p_ut < 0)
4008 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004009 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 p_ut = 2000;
4011 }
4012 if (p_ss < 0)
4013 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004014 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 p_ss = 0;
4016 }
4017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004018 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4020 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4021
4022 options[opt_idx].flags |= P_WAS_SET;
4023
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004024#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004025 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4026 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004027#endif
4028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004029 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004030 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004031 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004032 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004033 if ((opt_flags & OPT_NO_REDRAW) == 0)
4034 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035
4036 return errmsg;
4037}
4038
4039/*
4040 * Called after an option changed: check if something needs to be redrawn.
4041 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004042 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004043check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004045 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004046 int doclear = (flags & P_RCLR) == P_RCLR;
4047 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004049 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4053 changed_window_setting();
4054 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004055 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004056 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004057 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004058 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004059 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004061 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062}
4063
4064/*
4065 * Find index for option 'arg'.
4066 * Return -1 if not found.
4067 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004068 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004069findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
4071 int opt_idx;
4072 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004073 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 int is_term_opt;
4075
4076 /*
4077 * For first call: Initialize the quick-access table.
4078 * It contains the index for the first option that starts with a certain
4079 * letter. There are 26 letters, plus the first "t_" option.
4080 */
4081 if (quick_tab[1] == 0)
4082 {
4083 p = options[0].fullname;
4084 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4085 {
4086 if (s[0] != p[0])
4087 {
4088 if (s[0] == 't' && s[1] == '_')
4089 quick_tab[26] = opt_idx;
4090 else
4091 quick_tab[CharOrdLow(s[0])] = opt_idx;
4092 }
4093 p = s;
4094 }
4095 }
4096
4097 /*
4098 * Check for name starting with an illegal character.
4099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 return -1;
4102
4103 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4104 if (is_term_opt)
4105 opt_idx = quick_tab[26];
4106 else
4107 opt_idx = quick_tab[CharOrdLow(arg[0])];
4108 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4109 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004110 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 break;
4112 }
4113 if (s == NULL && !is_term_opt)
4114 {
4115 opt_idx = quick_tab[CharOrdLow(arg[0])];
4116 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4117 {
4118 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004119 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 break;
4121 s = NULL;
4122 }
4123 }
4124 if (s == NULL)
4125 opt_idx = -1;
4126 return opt_idx;
4127}
4128
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004129#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4130 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131/*
4132 * Get the value for an option.
4133 *
4134 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004135 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004136 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004137 * String option: gov_string, *stringval gets allocated string.
4138 * Hidden Number option: gov_hidden_number.
4139 * Hidden Toggle option: gov_hidden_bool.
4140 * Hidden String option: gov_hidden_string.
4141 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004142 *
4143 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004145 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004146get_option_value(
4147 char_u *name,
4148 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004149 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004150 int *flagsp,
4151 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
4153 int opt_idx;
4154 char_u *varp;
4155
4156 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004157 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004158 {
4159 int key;
4160
4161 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004162 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004163 {
4164 char_u key_name[2];
4165 char_u *p;
4166
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004167 if (flagsp != NULL)
4168 *flagsp = 0; // terminal option has no flags
4169
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004170 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004171 if (key < 0)
4172 {
4173 key_name[0] = KEY2TERMCAP0(key);
4174 key_name[1] = KEY2TERMCAP1(key);
4175 }
4176 else
4177 {
4178 key_name[0] = KS_KEY;
4179 key_name[1] = (key & 0xff);
4180 }
4181 p = find_termcode(key_name);
4182 if (p != NULL)
4183 {
4184 if (stringval != NULL)
4185 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004186 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004187 }
4188 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004189 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004192 varp = get_varp_scope(&(options[opt_idx]), scope);
4193
4194 if (flagsp != NULL)
4195 // Return the P_xxxx option flags.
4196 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
4198 if (options[opt_idx].flags & P_STRING)
4199 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004200 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004201 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 if (stringval != NULL)
4203 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004204 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004205 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4206 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004208 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004209 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004210 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004213 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 *stringval = vim_strsave(*(char_u **)(varp));
4215 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004216 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004219 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004220 return (options[opt_idx].flags & P_NUM)
4221 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 if (options[opt_idx].flags & P_NUM)
4223 *numval = *(long *)varp;
4224 else
4225 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004226 // Special case: 'modified' is b_changed, but we also want to consider
4227 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if ((int *)varp == &curbuf->b_changed)
4229 *numval = curbufIsChanged();
4230 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004231 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004233 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234}
4235#endif
4236
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004237#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004238/*
4239 * Returns the option attributes and its value. Unlike the above function it
4240 * will return either global value or local value of the option depending on
4241 * what was requested, but it will never return global value if it was
4242 * requested to return local one and vice versa. Neither it will return
4243 * buffer-local value if it was requested to return window-local one.
4244 *
4245 * Pretends that option is absent if it is not present in the requested scope
4246 * (i.e. has no global, window-local or buffer-local value depending on
4247 * opt_type). Uses
4248 *
4249 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004250 * 0 hidden or unknown option, also option that does not have requested
4251 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004252 * see SOPT_* in vim.h for other flags
4253 *
4254 * Possible opt_type values: see SREQ_* in vim.h
4255 */
4256 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004257get_option_value_strict(
4258 char_u *name,
4259 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004260 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004261 int opt_type,
4262 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004263{
4264 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004265 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004266 struct vimoption *p;
4267 int r = 0;
4268
4269 opt_idx = findoption(name);
4270 if (opt_idx < 0)
4271 return 0;
4272
4273 p = &(options[opt_idx]);
4274
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004275 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004276 if (p->var == NULL)
4277 return 0;
4278
4279 if (p->flags & P_BOOL)
4280 r |= SOPT_BOOL;
4281 else if (p->flags & P_NUM)
4282 r |= SOPT_NUM;
4283 else if (p->flags & P_STRING)
4284 r |= SOPT_STRING;
4285
4286 if (p->indir == PV_NONE)
4287 {
4288 if (opt_type == SREQ_GLOBAL)
4289 r |= SOPT_GLOBAL;
4290 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004291 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004292 }
4293 else
4294 {
4295 if (p->indir & PV_BOTH)
4296 r |= SOPT_GLOBAL;
4297 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004298 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004299
4300 if (p->indir & PV_WIN)
4301 {
4302 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004303 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004304 else
4305 r |= SOPT_WIN;
4306 }
4307 else if (p->indir & PV_BUF)
4308 {
4309 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004310 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004311 else
4312 r |= SOPT_BUF;
4313 }
4314 }
4315
4316 if (stringval == NULL)
4317 return r;
4318
4319 if (opt_type == SREQ_GLOBAL)
4320 varp = p->var;
4321 else
4322 {
4323 if (opt_type == SREQ_BUF)
4324 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004325 // Special case: 'modified' is b_changed, but we also want to
4326 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004327 if (p->indir == PV_MOD)
4328 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004329 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004330 varp = NULL;
4331 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004332# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004333 else if (p->indir == PV_KEY)
4334 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004335 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004336 *stringval = NULL;
4337 varp = NULL;
4338 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004339# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004340 else
4341 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004342 buf_T *save_curbuf = curbuf;
4343
4344 // only getting a pointer, no need to use aucmd_prepbuf()
4345 curbuf = (buf_T *)from;
4346 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004347 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004348 curbuf = save_curbuf;
4349 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004350 }
4351 }
4352 else if (opt_type == SREQ_WIN)
4353 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004354 win_T *save_curwin = curwin;
4355
4356 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004357 curbuf = curwin->w_buffer;
4358 varp = get_varp(p);
4359 curwin = save_curwin;
4360 curbuf = curwin->w_buffer;
4361 }
4362 if (varp == p->var)
4363 return (r | SOPT_UNSET);
4364 }
4365
4366 if (varp != NULL)
4367 {
4368 if (p->flags & P_STRING)
4369 *stringval = vim_strsave(*(char_u **)(varp));
4370 else if (p->flags & P_NUM)
4371 *numval = *(long *) varp;
4372 else
4373 *numval = *(int *)varp;
4374 }
4375
4376 return r;
4377}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004378
4379/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004380 * Iterate over options. First argument is a pointer to a pointer to a
4381 * structure inside options[] array, second is option type like in the above
4382 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004383 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004384 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004385 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004386 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004387 * first argument to NULL.
4388 *
4389 * Returns full option name for current option on each call.
4390 */
4391 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004392option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004393{
4394 struct vimoption *ret = NULL;
4395 do
4396 {
4397 if (*option == NULL)
4398 *option = (void *) options;
4399 else if (((struct vimoption *) (*option))->fullname == NULL)
4400 {
4401 *option = NULL;
4402 return NULL;
4403 }
4404 else
4405 *option = (void *) (((struct vimoption *) (*option)) + 1);
4406
4407 ret = ((struct vimoption *) (*option));
4408
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004409 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004410 if (ret->var == NULL)
4411 {
4412 ret = NULL;
4413 continue;
4414 }
4415
4416 switch (opt_type)
4417 {
4418 case SREQ_GLOBAL:
4419 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4420 ret = NULL;
4421 break;
4422 case SREQ_BUF:
4423 if (!(ret->indir & PV_BUF))
4424 ret = NULL;
4425 break;
4426 case SREQ_WIN:
4427 if (!(ret->indir & PV_WIN))
4428 ret = NULL;
4429 break;
4430 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004431 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004432 return NULL;
4433 }
4434 }
4435 while (ret == NULL);
4436
4437 return (char_u *)ret->fullname;
4438}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004439#endif
4440
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004442 * Return the flags for the option at 'opt_idx'.
4443 */
4444 long_u
4445get_option_flags(int opt_idx)
4446{
4447 return options[opt_idx].flags;
4448}
4449
4450/*
4451 * Set a flag for the option at 'opt_idx'.
4452 */
4453 void
4454set_option_flag(int opt_idx, long_u flag)
4455{
4456 options[opt_idx].flags |= flag;
4457}
4458
4459/*
4460 * Clear a flag for the option at 'opt_idx'.
4461 */
4462 void
4463clear_option_flag(int opt_idx, long_u flag)
4464{
4465 options[opt_idx].flags &= ~flag;
4466}
4467
4468/*
4469 * Returns TRUE if the option at 'opt_idx' is a global option
4470 */
4471 int
4472is_global_option(int opt_idx)
4473{
4474 return options[opt_idx].indir == PV_NONE;
4475}
4476
4477/*
4478 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4479 * local value.
4480 */
4481 int
4482is_global_local_option(int opt_idx)
4483{
4484 return options[opt_idx].indir & PV_BOTH;
4485}
4486
4487/*
4488 * Returns TRUE if the option at 'opt_idx' is a window-local option
4489 */
4490 int
4491is_window_local_option(int opt_idx)
4492{
4493 return options[opt_idx].var == VAR_WIN;
4494}
4495
4496/*
4497 * Returns TRUE if the option at 'opt_idx' is a hidden option
4498 */
4499 int
4500is_hidden_option(int opt_idx)
4501{
4502 return options[opt_idx].var == NULL;
4503}
4504
4505#if defined(FEAT_CRYPT) || defined(PROTO)
4506/*
4507 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4508 */
4509 int
4510is_crypt_key_option(int opt_idx)
4511{
4512 return options[opt_idx].indir == PV_KEY;
4513}
4514#endif
4515
4516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 * Set the value of option "name".
4518 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004519 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004520 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004522 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004523set_option_value(
4524 char_u *name,
4525 long number,
4526 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004527 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528{
4529 int opt_idx;
4530 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004531 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004534 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004535 {
4536 int key;
4537
4538 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004539 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004540 {
4541 char_u key_name[2];
4542
4543 if (key < 0)
4544 {
4545 key_name[0] = KEY2TERMCAP0(key);
4546 key_name[1] = KEY2TERMCAP1(key);
4547 }
4548 else
4549 {
4550 key_name[0] = KS_KEY;
4551 key_name[1] = (key & 0xff);
4552 }
4553 add_termcode(key_name, string, FALSE);
4554 if (full_screen)
4555 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004556 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01004557 return NULL;
4558 }
4559
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004560 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 else
4563 {
4564 flags = options[opt_idx].flags;
4565#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004566 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004568 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004569 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004570 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004573 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004574 return set_string_option(opt_idx, string, opt_flags);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004575
4576 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
4577 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004579 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004581 int idx;
4582
4583 // Either we are given a string or we are setting option
4584 // to zero.
4585 for (idx = 0; string[idx] == '0'; ++idx)
4586 ;
4587 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004588 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004589 // There's another character after zeros or the string
4590 // is empty. In both cases, we are trying to set a
4591 // num option using a string.
4592 semsg(_(e_number_required_after_str_equal_str),
4593 name, string);
4594 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004595
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004598 if (flags & P_NUM)
4599 return set_num_option(opt_idx, varp, number,
4600 NULL, 0, opt_flags);
4601 else
4602 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004604
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004606 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607}
4608
4609/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004610 * Call set_option_value() and when an error is returned report it.
4611 */
4612 void
4613set_option_value_give_err(
4614 char_u *name,
4615 long number,
4616 char_u *string,
4617 int opt_flags) // OPT_LOCAL or 0 (both)
4618{
4619 char *errmsg = set_option_value(name, number, string, opt_flags);
4620
4621 if (errmsg != NULL)
4622 emsg(_(errmsg));
4623}
4624
4625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 * Get the terminal code for a terminal option.
4627 * Returns NULL when not found.
4628 */
4629 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004630get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631{
4632 int opt_idx;
4633 char_u *varp;
4634
4635 if (tname[0] != 't' || tname[1] != '_' ||
4636 tname[2] == NUL || tname[3] == NUL)
4637 return NULL;
4638 if ((opt_idx = findoption(tname)) >= 0)
4639 {
4640 varp = get_varp(&(options[opt_idx]));
4641 if (varp != NULL)
4642 varp = *(char_u **)(varp);
4643 return varp;
4644 }
4645 return find_termcode(tname + 2);
4646}
4647
4648 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004649get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650{
4651 int i;
4652
4653 i = findoption((char_u *)"hl");
4654 if (i >= 0)
4655 return options[i].def_val[VI_DEFAULT];
4656 return (char_u *)NULL;
4657}
4658
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004659 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004660get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004661{
4662 int i;
4663
4664 i = findoption((char_u *)"enc");
4665 if (i >= 0)
4666 return options[i].def_val[VI_DEFAULT];
4667 return (char_u *)NULL;
4668}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004669
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004670#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004671 int
4672is_option_allocated(char *name)
4673{
4674 int idx = findoption((char_u *)name);
4675
4676 return idx >= 0 && (options[idx].flags & P_ALLOCED);
4677}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004678#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004679
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004680#if defined(FEAT_EVAL) || defined(PROTO)
4681/*
4682 * Return TRUE if "name" is a string option.
4683 * Returns FALSE if option "name" does not exist.
4684 */
4685 int
4686is_string_option(char_u *name)
4687{
4688 int idx = findoption(name);
4689
4690 return idx >= 0 && (options[idx].flags & P_STRING);
4691}
4692#endif
4693
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694/*
4695 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004696 * When "has_lt" is true there is a '<' before "*arg_arg".
4697 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 */
4699 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004700find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004702 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004704 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
4706 /*
4707 * Don't use get_special_key_code() for t_xx, we don't want it to call
4708 * add_termcap_entry().
4709 */
4710 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4711 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004712 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004714 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004716 key = find_special_key(&arg, &modifiers,
4717 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004718 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 key = 0;
4720 }
4721 return key;
4722}
4723
4724/*
4725 * if 'all' == 0: show changed options
4726 * if 'all' == 1: show all normal options
4727 * if 'all' == 2: show all terminal options
4728 */
4729 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004730showoptions(
4731 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004732 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
4734 struct vimoption *p;
4735 int col;
4736 int isterm;
4737 char_u *varp;
4738 struct vimoption **items;
4739 int item_count;
4740 int run;
4741 int row, rows;
4742 int cols;
4743 int i;
4744 int len;
4745
4746#define INC 20
4747#define GAP 3
4748
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004749 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 if (items == NULL)
4751 return;
4752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004753 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004755 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004757 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004759 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004761 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
4763 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004764 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 * 1. display the short items
4766 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004767 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 */
4769 for (run = 1; run <= 2 && !got_int; ++run)
4770 {
4771 /*
4772 * collect the items in items[]
4773 */
4774 item_count = 0;
4775 for (p = &options[0]; p->fullname != NULL; p++)
4776 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004777 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004778 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004779 continue;
4780
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 varp = NULL;
4782 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004783 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 {
4785 if (p->indir != PV_NONE && !isterm)
4786 varp = get_varp_scope(p, opt_flags);
4787 }
4788 else
4789 varp = get_varp(p);
4790 if (varp != NULL
4791 && ((all == 2 && isterm)
4792 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004793 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004795 if (opt_flags & OPT_ONECOLUMN)
4796 len = Columns;
4797 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004798 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 else
4800 {
4801 option_value2string(p, opt_flags);
4802 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4803 }
4804 if ((len <= INC - GAP && run == 1) ||
4805 (len > INC - GAP && run == 2))
4806 items[item_count++] = p;
4807 }
4808 }
4809
4810 /*
4811 * display the items
4812 */
4813 if (run == 1)
4814 {
4815 cols = (Columns + GAP - 3) / INC;
4816 if (cols == 0)
4817 cols = 1;
4818 rows = (item_count + cols - 1) / cols;
4819 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004820 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 rows = item_count;
4822 for (row = 0; row < rows && !got_int; ++row)
4823 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004824 msg_putchar('\n'); // go to next line
4825 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 break;
4827 col = 0;
4828 for (i = row; i < item_count; i += rows)
4829 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004830 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 showoneopt(items[i], opt_flags);
4832 col += INC;
4833 }
4834 out_flush();
4835 ui_breakcheck();
4836 }
4837 }
4838 vim_free(items);
4839}
4840
4841/*
4842 * Return TRUE if option "p" has its default value.
4843 */
4844 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004845optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846{
4847 int dvi;
4848
4849 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004850 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004851 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004853 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004855 // the cast to long is required for Manx C, long_i is
4856 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004857 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004858 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4860}
4861
4862/*
4863 * showoneopt: show the value of one option
4864 * must not be called with a hidden option!
4865 */
4866 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004867showoneopt(
4868 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004869 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004871 char_u *varp;
4872 int save_silent = silent_mode;
4873
4874 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004875 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
4877 varp = get_varp_scope(p, opt_flags);
4878
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004879 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4881 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004882 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004884 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004886 msg_puts(" ");
4887 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 if (!(p->flags & P_BOOL))
4889 {
4890 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004891 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 option_value2string(p, opt_flags);
4893 msg_outtrans(NameBuff);
4894 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004895
4896 silent_mode = save_silent;
4897 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898}
4899
4900/*
4901 * Write modified options as ":set" commands to a file.
4902 *
4903 * There are three values for "opt_flags":
4904 * OPT_GLOBAL: Write global option values and fresh values of
4905 * buffer-local options (used for start of a session
4906 * file).
4907 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4908 * curwin (used for a vimrc file).
4909 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4910 * and local values for window-local options of
4911 * curwin. Local values are also written when at the
4912 * default value, because a modeline or autocommand
4913 * may have set them when doing ":edit file" and the
4914 * user has set them back at the default or fresh
4915 * value.
4916 * When "local_only" is TRUE, don't write fresh
4917 * values, only local values (for ":mkview").
4918 * (fresh value = value used for a new buffer or window for a local option).
4919 *
4920 * Return FAIL on error, OK otherwise.
4921 */
4922 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004923makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924{
4925 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004926 char_u *varp; // currently used value
4927 char_u *varp_fresh; // local value
4928 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 char *cmd;
4930 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004931 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
4933 /*
4934 * The options that don't have a default (terminal name, columns, lines)
4935 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004936 * Do the loop over "options[]" twice: once for options with the
4937 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004939 for (pri = 1; pri >= 0; --pri)
4940 {
4941 for (p = &options[0]; !istermoption(p); p++)
4942 if (!(p->flags & P_NO_MKRC)
4943 && !istermoption(p)
4944 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004946 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4948 continue;
4949
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004950 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4951 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4953 continue;
4954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004955 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004957 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 continue;
4959
Bram Moolenaard23b7142021-04-17 21:04:34 +02004960 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4961 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004962 continue;
4963
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 round = 2;
4965 if (p->indir != PV_NONE)
4966 {
4967 if (p->var == VAR_WIN)
4968 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004969 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 if (!(opt_flags & OPT_LOCAL))
4971 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004972 // When fresh value of window-local option is not at the
4973 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4975 {
4976 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004977 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
4979 round = 1;
4980 varp_local = varp;
4981 varp = varp_fresh;
4982 }
4983 }
4984 }
4985 }
4986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004987 // Round 1: fresh value for window-local options.
4988 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 for ( ; round <= 2; varp = varp_local, ++round)
4990 {
4991 if (round == 1 || (opt_flags & OPT_GLOBAL))
4992 cmd = "set";
4993 else
4994 cmd = "setlocal";
4995
4996 if (p->flags & P_BOOL)
4997 {
4998 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4999 return FAIL;
5000 }
5001 else if (p->flags & P_NUM)
5002 {
5003 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5004 return FAIL;
5005 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005006 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005008 int do_endif = FALSE;
5009
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005010 // Don't set 'syntax' and 'filetype' again if the value is
5011 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005012 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005013#if defined(FEAT_SYN_HL)
5014 p->indir == PV_SYN ||
5015#endif
5016 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
5018 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5019 *(char_u **)(varp)) < 0
5020 || put_eol(fd) < 0)
5021 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005022 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005025 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005027 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
5029 if (put_line(fd, "endif") == FAIL)
5030 return FAIL;
5031 }
5032 }
5033 }
5034 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 return OK;
5037}
5038
5039#if defined(FEAT_FOLDING) || defined(PROTO)
5040/*
5041 * Generate set commands for the local fold options only. Used when
5042 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5043 */
5044 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005045makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005047 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005049 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 == FAIL
5051# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005052 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005054 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 == FAIL
5056 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5057 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5058 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5059 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5060 )
5061 return FAIL;
5062
5063 return OK;
5064}
5065#endif
5066
5067 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005068put_setstring(
5069 FILE *fd,
5070 char *cmd,
5071 char *name,
5072 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005073 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074{
5075 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005076 char_u *buf = NULL;
5077 char_u *part = NULL;
5078 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079
5080 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5081 return FAIL;
5082 if (*valuep != NULL)
5083 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005084 // Output 'pastetoggle' as key names. For other
5085 // options some characters have to be escaped with
5086 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 if (valuep == &p_pt)
5088 {
5089 s = *valuep;
5090 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005091 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 return FAIL;
5093 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005094 // expand the option value, replace $HOME by ~
5095 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005097 int size = (int)STRLEN(*valuep) + 1;
5098
5099 // replace home directory in the whole option value into "buf"
5100 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005101 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005102 goto fail;
5103 home_replace(NULL, *valuep, buf, size, FALSE);
5104
5105 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005106 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005107 // can be expanded when read back.
5108 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5109 && vim_strchr(*valuep, ',') != NULL)
5110 {
5111 part = alloc(size);
5112 if (part == NULL)
5113 goto fail;
5114
5115 // write line break to clear the option, e.g. ':set rtp='
5116 if (put_eol(fd) == FAIL)
5117 goto fail;
5118
5119 p = buf;
5120 while (*p != NUL)
5121 {
5122 // for each comma separated option part, append value to
5123 // the option, :set rtp+=value
5124 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5125 goto fail;
5126 (void)copy_option_part(&p, part, size, ",");
5127 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5128 goto fail;
5129 }
5130 vim_free(buf);
5131 vim_free(part);
5132 return OK;
5133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005135 {
5136 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005138 }
5139 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
5141 else if (put_escstr(fd, *valuep, 2) == FAIL)
5142 return FAIL;
5143 }
5144 if (put_eol(fd) < 0)
5145 return FAIL;
5146 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005147fail:
5148 vim_free(buf);
5149 vim_free(part);
5150 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151}
5152
5153 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005154put_setnum(
5155 FILE *fd,
5156 char *cmd,
5157 char *name,
5158 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159{
5160 long wc;
5161
5162 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5163 return FAIL;
5164 if (wc_use_keyname((char_u *)valuep, &wc))
5165 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005166 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5168 return FAIL;
5169 }
5170 else if (fprintf(fd, "%ld", *valuep) < 0)
5171 return FAIL;
5172 if (put_eol(fd) < 0)
5173 return FAIL;
5174 return OK;
5175}
5176
5177 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005178put_setbool(
5179 FILE *fd,
5180 char *cmd,
5181 char *name,
5182 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005184 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005185 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5187 || put_eol(fd) < 0)
5188 return FAIL;
5189 return OK;
5190}
5191
5192/*
5193 * Clear all the terminal options.
5194 * If the option has been allocated, free the memory.
5195 * Terminal options are never hidden or indirect.
5196 */
5197 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005198clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 /*
5201 * Reset a few things before clearing the old options. This may cause
5202 * outputting a few things that the terminal doesn't understand, but the
5203 * screen will be cleared later, so this is OK.
5204 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005205 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005206 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005208 // When starting the GUI close the display opened for the clipboard.
5209 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (gui.starting)
5211 clear_xterm_clip();
5212#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005213 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005215 free_termoptions();
5216}
5217
5218 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005219free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005220{
5221 struct vimoption *p;
5222
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005223 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 if (istermoption(p))
5225 {
5226 if (p->flags & P_ALLOCED)
5227 free_string_option(*(char_u **)(p->var));
5228 if (p->flags & P_DEF_ALLOCED)
5229 free_string_option(p->def_val[VI_DEFAULT]);
5230 *(char_u **)(p->var) = empty_option;
5231 p->def_val[VI_DEFAULT] = empty_option;
5232 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005233#ifdef FEAT_EVAL
5234 // remember where the option was cleared
5235 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238 clear_termcodes();
5239}
5240
5241/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005242 * Free the string for one term option, if it was allocated.
5243 * Set the string to empty_option and clear allocated flag.
5244 * "var" points to the option value.
5245 */
5246 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005247free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005248{
5249 struct vimoption *p;
5250
5251 for (p = &options[0]; p->fullname != NULL; p++)
5252 if (p->var == var)
5253 {
5254 if (p->flags & P_ALLOCED)
5255 free_string_option(*(char_u **)(p->var));
5256 *(char_u **)(p->var) = empty_option;
5257 p->flags &= ~P_ALLOCED;
5258 break;
5259 }
5260}
5261
5262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 * Set the terminal option defaults to the current value.
5264 * Used after setting the terminal name.
5265 */
5266 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005267set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
5269 struct vimoption *p;
5270
5271 for (p = &options[0]; p->fullname != NULL; p++)
5272 {
5273 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5274 {
5275 if (p->flags & P_DEF_ALLOCED)
5276 {
5277 free_string_option(p->def_val[VI_DEFAULT]);
5278 p->flags &= ~P_DEF_ALLOCED;
5279 }
5280 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5281 if (p->flags & P_ALLOCED)
5282 {
5283 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005284 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286 }
5287 }
5288}
5289
5290/*
5291 * return TRUE if 'p' starts with 't_'
5292 */
5293 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005294istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295{
5296 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5297}
5298
Bram Moolenaardac13472019-09-16 21:06:21 +02005299/*
5300 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5301 */
5302 int
5303istermoption_idx(int opt_idx)
5304{
5305 return istermoption(&options[opt_idx]);
5306}
5307
Bram Moolenaar113e1072019-01-20 15:30:40 +01005308#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005310 * Unset local option value, similar to ":set opt<".
5311 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005312 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005313unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005314{
5315 struct vimoption *p;
5316 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005317 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005318
5319 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005320 if (opt_idx < 0)
5321 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005322 p = &(options[opt_idx]);
5323
5324 switch ((int)p->indir)
5325 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005326 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005327 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005328 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005329 break;
5330 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005331 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005332 break;
5333 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005334 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005335 break;
5336 case PV_AR:
5337 buf->b_p_ar = -1;
5338 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005339 case PV_BKC:
5340 clear_string_option(&buf->b_p_bkc);
5341 buf->b_bkc_flags = 0;
5342 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005343 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005344 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005345 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005346 case PV_TC:
5347 clear_string_option(&buf->b_p_tc);
5348 buf->b_tc_flags = 0;
5349 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005350 case PV_SISO:
5351 curwin->w_p_siso = -1;
5352 break;
5353 case PV_SO:
5354 curwin->w_p_so = -1;
5355 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005356# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005357 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005358 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005359 break;
5360 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005361 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005362 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005363# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005364 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005365 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005366 break;
5367 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005368 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005369 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005370# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005371 case PV_TSRFU:
5372 clear_string_option(&buf->b_p_tsrfu);
5373 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005374# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005375 case PV_FP:
5376 clear_string_option(&buf->b_p_fp);
5377 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005378# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005379 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005380 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005381 break;
5382 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005383 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005384 break;
5385 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005386 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005387 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005388# endif
5389# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005390 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005391 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005392 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005393# endif
5394# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005395 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005396 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005397 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005398# endif
5399# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01005400 case PV_SBR:
5401 clear_string_option(&((win_T *)from)->w_p_sbr);
5402 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005403# endif
5404# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005405 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005406 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005407 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005408# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005409 case PV_UL:
5410 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5411 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005412 case PV_LW:
5413 clear_string_option(&buf->b_p_lw);
5414 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005415 case PV_MENC:
5416 clear_string_option(&buf->b_p_menc);
5417 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005418 case PV_LCS:
5419 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005420 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005421 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005422 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005423 case PV_FCS:
5424 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005425 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005426 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005427 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005428 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005429 clear_string_option(&((win_T *)from)->w_p_ve);
5430 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005431 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005432 }
5433}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005434#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005435
5436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005438 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 */
5440 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005441get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005443 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {
5445 if (p->var == VAR_WIN)
5446 return (char_u *)GLOBAL_WO(get_varp(p));
5447 return p->var;
5448 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005449 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
5451 switch ((int)p->indir)
5452 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005453 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005455 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5456 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5457 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005459 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5460 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5461 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005462 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005463 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005464 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005465 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5466 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005468 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5469 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005471 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5472 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005473#ifdef FEAT_COMPL_FUNC
5474 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5475#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005476#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5477 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5478#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005479#if defined(FEAT_CRYPT)
5480 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5481#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005482#ifdef FEAT_LINEBREAK
5483 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5484#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005485#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005486 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005487#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005488 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005489 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005490 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005491 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005492 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005493 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005494 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005495
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005497 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
5499 return get_varp(p);
5500}
5501
5502/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005503 * Get pointer to option variable at 'opt_idx', depending on local or global
5504 * scope.
5505 */
5506 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005507get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005508{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005509 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005510}
5511
5512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 * Get pointer to option variable.
5514 */
5515 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005516get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005518 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 if (p->var == NULL)
5520 return NULL;
5521
5522 switch ((int)p->indir)
5523 {
5524 case PV_NONE: return p->var;
5525
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005526 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005527 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005529 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005531 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005533 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005535 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005537 case PV_TC: return *curbuf->b_p_tc != NUL
5538 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005539 case PV_BKC: return *curbuf->b_p_bkc != NUL
5540 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005541 case PV_SISO: return curwin->w_p_siso >= 0
5542 ? (char_u *)&(curwin->w_p_siso) : p->var;
5543 case PV_SO: return curwin->w_p_so >= 0
5544 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005546 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005548 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5550#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005551 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005553 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005555#ifdef FEAT_COMPL_FUNC
5556 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5557 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5558#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005559 case PV_FP: return *curbuf->b_p_fp != NUL
5560 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005562 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005564 case PV_GP: return *curbuf->b_p_gp != NUL
5565 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5566 case PV_MP: return *curbuf->b_p_mp != NUL
5567 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005569#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5570 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5571 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5572#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005573#if defined(FEAT_CRYPT)
5574 case PV_CM: return *curbuf->b_p_cm != NUL
5575 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5576#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005577#ifdef FEAT_LINEBREAK
5578 case PV_SBR: return *curwin->w_p_sbr != NUL
5579 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5580#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005581#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005582 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005583 ? (char_u *)&(curwin->w_p_stl) : p->var;
5584#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005585 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5586 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005587 case PV_LW: return *curbuf->b_p_lw != NUL
5588 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005589 case PV_MENC: return *curbuf->b_p_menc != NUL
5590 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591#ifdef FEAT_ARABIC
5592 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5593#endif
5594 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005595 case PV_LCS: return *curwin->w_p_lcs != NUL
5596 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005597 case PV_FCS: return *curwin->w_p_fcs != NUL
5598 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005599 case PV_VE: return *curwin->w_p_ve != NUL
5600 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005601#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005602 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5603#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005604#ifdef FEAT_SYN_HL
5605 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5606 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005607 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005608 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610#ifdef FEAT_DIFF
5611 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5612#endif
5613#ifdef FEAT_FOLDING
5614 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5615 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5616 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5617 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5618 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5619 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5620 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5621# ifdef FEAT_EVAL
5622 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5623 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5624# endif
5625 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5626#endif
5627 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005628 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005629#ifdef FEAT_LINEBREAK
5630 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005633 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005634#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5636#endif
5637#ifdef FEAT_RIGHTLEFT
5638 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5639 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5640#endif
5641 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01005642 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5644#ifdef FEAT_LINEBREAK
5645 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005646 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5647 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005649 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005651 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005652#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005653 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5654 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5655#endif
5656#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005657 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5658 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5659 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
5662 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5663 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5666 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5668 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5670 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5671 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01005672 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675#ifdef FEAT_FOLDING
5676 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005679#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005680 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005682#ifdef FEAT_COMPL_FUNC
5683 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005684 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005685#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005686#ifdef FEAT_EVAL
5687 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5688#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01005689 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005691 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005697 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5699 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5700 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5701 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5702#ifdef FEAT_FIND_ID
5703# ifdef FEAT_EVAL
5704 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5705# endif
5706#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01005707#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5709 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5710#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005711#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005712 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714#ifdef FEAT_CRYPT
5715 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01005718 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5720 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5721 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5722 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5723 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005725 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5732#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005733 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005734 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5735#endif
5736#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005737 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5738 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5739 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005740 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741#endif
5742 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5743 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5744 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5745 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005746#ifdef FEAT_PERSISTENT_UNDO
5747 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5750#ifdef FEAT_KEYMAP
5751 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5752#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005753#ifdef FEAT_SIGNS
5754 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5755#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005756#ifdef FEAT_VARTABS
5757 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5758 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5759#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005760 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005762 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 return (char_u *)&(curbuf->b_p_wm);
5764}
5765
5766/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005767 * Return a pointer to the variable for option at 'opt_idx'
5768 */
5769 char_u *
5770get_option_var(int opt_idx)
5771{
5772 return options[opt_idx].var;
5773}
5774
Dominique Pelle748b3082022-01-08 12:41:16 +00005775#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005776/*
5777 * Return the full name of the option at 'opt_idx'
5778 */
5779 char_u *
5780get_option_fullname(int opt_idx)
5781{
5782 return (char_u *)options[opt_idx].fullname;
5783}
Dominique Pelle748b3082022-01-08 12:41:16 +00005784#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005785
5786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 * Get the value of 'equalprg', either the buffer-local one or the global one.
5788 */
5789 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005790get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791{
5792 if (*curbuf->b_p_ep == NUL)
5793 return p_ep;
5794 return curbuf->b_p_ep;
5795}
5796
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797/*
5798 * Copy options from one window to another.
5799 * Used when splitting a window.
5800 */
5801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005802win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803{
5804 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5805 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005806 after_copy_winopt(wp_to);
5807}
5808
5809/*
5810 * After copying window options: update variables depending on options.
5811 */
5812 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00005813after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005814{
5815#ifdef FEAT_LINEBREAK
5816 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005817#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005818#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005819 fill_culopt_flags(NULL, wp);
5820 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005821#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005822 set_chars_option(wp, &wp->w_p_lcs, TRUE);
5823 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005824}
5825
5826 static char_u *
5827copy_option_val(char_u *val)
5828{
5829 if (val == empty_option)
5830 return empty_option; // no need to allocate memory
5831 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
5834/*
5835 * Copy the options from one winopt_T to another.
5836 * Doesn't free the old option values in "to", use clear_winopt() for that.
5837 * The 'scroll' option is not copied, because it depends on the window height.
5838 * The 'previewwindow' option is reset, there can be only one preview window.
5839 */
5840 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005841copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842{
5843#ifdef FEAT_ARABIC
5844 to->wo_arab = from->wo_arab;
5845#endif
5846 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005847 to->wo_lcs = copy_option_val(from->wo_lcs);
5848 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005850 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005851 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02005852 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005853#ifdef FEAT_LINEBREAK
5854 to->wo_nuw = from->wo_nuw;
5855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856#ifdef FEAT_RIGHTLEFT
5857 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005858 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005860#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005861 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01005862#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005863#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005864 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005867#ifdef FEAT_DIFF
5868 to->wo_wrap_save = from->wo_wrap_save;
5869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870#ifdef FEAT_LINEBREAK
5871 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005872 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005873 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005875 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005877 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01005878 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005879 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005880 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005881#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005882 to->wo_spell = from->wo_spell;
5883#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005884#ifdef FEAT_SYN_HL
5885 to->wo_cuc = from->wo_cuc;
5886 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005887 to->wo_culopt = copy_option_val(from->wo_culopt);
5888 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#ifdef FEAT_DIFF
5891 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005892 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005894#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005895 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005896 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005897#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005898#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005899 to->wo_twk = copy_option_val(from->wo_twk);
5900 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902#ifdef FEAT_FOLDING
5903 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005904 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005906 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005907 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 to->wo_fml = from->wo_fml;
5909 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005910 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005911 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005912 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005913 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 to->wo_fdn = from->wo_fdn;
5915# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005916 to->wo_fde = copy_option_val(from->wo_fde);
5917 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005919 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005921#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005922 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005923#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005924
5925#ifdef FEAT_EVAL
5926 // Copy the script context so that we know where the value was last set.
5927 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5928 sizeof(to->wo_script_ctx));
5929#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005930 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931}
5932
5933/*
5934 * Check string options in a window for a NULL value.
5935 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005936 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005937check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938{
5939 check_winopt(&win->w_onebuf_opt);
5940 check_winopt(&win->w_allbuf_opt);
5941}
5942
5943/*
5944 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5945 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005946 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005947check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948{
5949#ifdef FEAT_FOLDING
5950 check_string_option(&wop->wo_fdi);
5951 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005952 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953# ifdef FEAT_EVAL
5954 check_string_option(&wop->wo_fde);
5955 check_string_option(&wop->wo_fdt);
5956# endif
5957 check_string_option(&wop->wo_fmr);
5958#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005959#ifdef FEAT_SIGNS
5960 check_string_option(&wop->wo_scl);
5961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962#ifdef FEAT_RIGHTLEFT
5963 check_string_option(&wop->wo_rlc);
5964#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005965#ifdef FEAT_LINEBREAK
5966 check_string_option(&wop->wo_sbr);
5967#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005968#ifdef FEAT_STL_OPT
5969 check_string_option(&wop->wo_stl);
5970#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005971#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005972 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005973 check_string_option(&wop->wo_cc);
5974#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005975#ifdef FEAT_CONCEAL
5976 check_string_option(&wop->wo_cocu);
5977#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005978#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005979 check_string_option(&wop->wo_twk);
5980 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005981#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005982#ifdef FEAT_LINEBREAK
5983 check_string_option(&wop->wo_briopt);
5984#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005985 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005986 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005987 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005988 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989}
5990
5991/*
5992 * Free the allocated memory inside a winopt_T.
5993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005995clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996{
5997#ifdef FEAT_FOLDING
5998 clear_string_option(&wop->wo_fdi);
5999 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006000 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001# ifdef FEAT_EVAL
6002 clear_string_option(&wop->wo_fde);
6003 clear_string_option(&wop->wo_fdt);
6004# endif
6005 clear_string_option(&wop->wo_fmr);
6006#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006007#ifdef FEAT_SIGNS
6008 clear_string_option(&wop->wo_scl);
6009#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006010#ifdef FEAT_LINEBREAK
6011 clear_string_option(&wop->wo_briopt);
6012#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006013 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014#ifdef FEAT_RIGHTLEFT
6015 clear_string_option(&wop->wo_rlc);
6016#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006017#ifdef FEAT_LINEBREAK
6018 clear_string_option(&wop->wo_sbr);
6019#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006020#ifdef FEAT_STL_OPT
6021 clear_string_option(&wop->wo_stl);
6022#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006023#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006024 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006025 clear_string_option(&wop->wo_cc);
6026#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006027#ifdef FEAT_CONCEAL
6028 clear_string_option(&wop->wo_cocu);
6029#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006030#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006031 clear_string_option(&wop->wo_twk);
6032 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006033#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006034 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006035 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006036 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037}
6038
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006039#ifdef FEAT_EVAL
6040// Index into the options table for a buffer-local option enum.
6041static int buf_opt_idx[BV_COUNT];
6042# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6043
6044/*
6045 * Initialize buf_opt_idx[] if not done already.
6046 */
6047 static void
6048init_buf_opt_idx(void)
6049{
6050 static int did_init_buf_opt_idx = FALSE;
6051 int i;
6052
6053 if (did_init_buf_opt_idx)
6054 return;
6055 did_init_buf_opt_idx = TRUE;
6056 for (i = 0; !istermoption_idx(i); i++)
6057 if (options[i].indir & PV_BUF)
6058 buf_opt_idx[options[i].indir & PV_MASK] = i;
6059}
6060#else
6061# define COPY_OPT_SCTX(buf, bv)
6062#endif
6063
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064/*
6065 * Copy global option values to local options for one buffer.
6066 * Used when creating a new buffer and sometimes when entering a buffer.
6067 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006068 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6070 * appropriate.
6071 * BCO_NOHELP Don't copy the values to a help buffer.
6072 */
6073 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006074buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075{
6076 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006077 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 int dont_do_help;
6079 int did_isk = FALSE;
6080
6081 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 * Skip this when the option defaults have not been set yet. Happens when
6083 * main() allocates the first buffer.
6084 */
6085 if (p_cpo != NULL)
6086 {
6087 /*
6088 * Always copy when entering and 'cpo' contains 'S'.
6089 * Don't copy when already initialized.
6090 * Don't copy when 'cpo' contains 's' and not entering.
6091 * 'S' BCO_ENTER initialized 's' should_copy
6092 * yes yes X X TRUE
6093 * yes no yes X FALSE
6094 * no X yes X FALSE
6095 * X no no yes FALSE
6096 * X no no no TRUE
6097 * no yes no X TRUE
6098 */
6099 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6100 && (buf->b_p_initialized
6101 || (!(flags & BCO_ENTER)
6102 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6103 should_copy = FALSE;
6104
6105 if (should_copy || (flags & BCO_ALWAYS))
6106 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006107#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006108 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006109 init_buf_opt_idx();
6110#endif
6111 // Don't copy the options specific to a help buffer when
6112 // BCO_NOHELP is given or the options were initialized already
6113 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6115 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006116 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 {
6118 save_p_isk = buf->b_p_isk;
6119 buf->b_p_isk = NULL;
6120 }
6121 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02006122 * Always free the allocated strings. If not already initialized,
6123 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 */
6125 if (!buf->b_p_initialized)
6126 {
6127 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006128 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006131 switch (*p_ffs)
6132 {
6133 case 'm':
6134 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6135 case 'd':
6136 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6137 case 'u':
6138 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6139 default:
6140 buf->b_p_ff = vim_strsave(p_ff);
6141 }
6142 if (buf->b_p_ff != NULL)
6143 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 buf->b_p_bh = empty_option;
6145 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 }
6147 else
6148 free_buf_options(buf, FALSE);
6149
6150 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006151 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 buf->b_p_ai_nopaste = p_ai_nopaste;
6153 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006154 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006156 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 buf->b_p_tw_nopaste = p_tw_nopaste;
6158 buf->b_p_tw_nobin = p_tw_nobin;
6159 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006160 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 buf->b_p_wm_nopaste = p_wm_nopaste;
6162 buf->b_p_wm_nobin = p_wm_nobin;
6163 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006164 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006165 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006166 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006167 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006168 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006170 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006172 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006174 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 buf->b_p_ml_nobin = p_ml_nobin;
6176 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006177 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006178 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006179 buf->b_p_swf = FALSE;
6180 else
6181 {
6182 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006183 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006186 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006187#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006188 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006189 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006191#ifdef FEAT_COMPL_FUNC
6192 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006193 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006194 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006195 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006196 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006197 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006198#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006199#ifdef FEAT_EVAL
6200 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006201 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006202 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006205 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006207#ifdef FEAT_VARTABS
6208 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006209 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006210 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006211 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006212 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006213 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006214 buf->b_p_vsts_nopaste = p_vsts_nopaste
6215 ? vim_strsave(p_vsts_nopaste) : NULL;
6216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006218 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006220 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221#ifdef FEAT_FOLDING
6222 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006223 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224#endif
6225 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006226 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006227 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006228 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006229 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6230 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006232 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006234 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006236 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006238 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006239
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006241 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006243 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006245 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006246 buf->b_p_cinsd = vim_strsave(p_cinsd);
6247 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006248 buf->b_p_lop = vim_strsave(p_lop);
6249 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006250
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006251 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006254 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006256 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006258 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006260 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006262 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006263 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006264 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006265#endif
6266#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006267 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006268 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006269 (void)compile_cap_prog(&buf->b_s);
6270 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006271 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006272 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006273 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006274 buf->b_s.b_p_spo = vim_strsave(p_spo);
6275 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006277#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006279 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006281 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006283 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006284#if defined(FEAT_EVAL)
6285 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006286 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288#ifdef FEAT_CRYPT
6289 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006290 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006293 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294#ifdef FEAT_KEYMAP
6295 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006296 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 buf->b_kmap_state |= KEYMAP_INIT;
6298#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006299#ifdef FEAT_TERMINAL
6300 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006301 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006302#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006303 // This isn't really an option, but copying the langmap and IME
6304 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006306 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006308 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006310 // options that are normally global but also have a local value
6311 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006313 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006314 buf->b_p_bkc = empty_option;
6315 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316#ifdef FEAT_QUICKFIX
6317 buf->b_p_gp = empty_option;
6318 buf->b_p_mp = empty_option;
6319 buf->b_p_efm = empty_option;
6320#endif
6321 buf->b_p_ep = empty_option;
6322 buf->b_p_kp = empty_option;
6323 buf->b_p_path = empty_option;
6324 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006325 buf->b_p_tc = empty_option;
6326 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327#ifdef FEAT_FIND_ID
6328 buf->b_p_def = empty_option;
6329 buf->b_p_inc = empty_option;
6330# ifdef FEAT_EVAL
6331 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006332 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333# endif
6334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 buf->b_p_dict = empty_option;
6336 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006337#ifdef FEAT_COMPL_FUNC
6338 buf->b_p_tsrfu = empty_option;
6339#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006340 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006341 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006342#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6343 buf->b_p_bexpr = empty_option;
6344#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006345#if defined(FEAT_CRYPT)
6346 buf->b_p_cm = empty_option;
6347#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006348#ifdef FEAT_PERSISTENT_UNDO
6349 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006350 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006351#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006352 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006353 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354
6355 /*
6356 * Don't copy the options set by ex_help(), use the saved values,
6357 * when going from a help buffer to a non-help buffer.
6358 * Don't touch these at all when BCO_NOHELP is used and going from
6359 * or to a help buffer.
6360 */
6361 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006364#ifdef FEAT_VARTABS
6365 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006366 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006367 else
6368 buf->b_p_vts_array = NULL;
6369#endif
6370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 else
6372 {
6373 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006374 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 did_isk = TRUE;
6376 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006377 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006378#ifdef FEAT_VARTABS
6379 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006380 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006381 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006382 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006383 else
6384 buf->b_p_vts_array = NULL;
6385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 if (buf->b_p_bt[0] == 'h')
6388 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006390 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 }
6392 }
6393
6394 /*
6395 * When the options should be copied (ignoring BCO_ALWAYS), set the
6396 * flag that indicates that the options have been initialized.
6397 */
6398 if (should_copy)
6399 buf->b_p_initialized = TRUE;
6400 }
6401
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006402 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (did_isk)
6404 (void)buf_init_chartab(buf, FALSE);
6405}
6406
6407/*
6408 * Reset the 'modifiable' option and its default value.
6409 */
6410 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006411reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 int opt_idx;
6414
6415 curbuf->b_p_ma = FALSE;
6416 p_ma = FALSE;
6417 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006418 if (opt_idx >= 0)
6419 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420}
6421
6422/*
6423 * Set the global value for 'iminsert' to the local value.
6424 */
6425 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006426set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
6428 p_iminsert = curbuf->b_p_iminsert;
6429}
6430
6431/*
6432 * Set the global value for 'imsearch' to the local value.
6433 */
6434 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006435set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
6437 p_imsearch = curbuf->b_p_imsearch;
6438}
6439
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440static int expand_option_idx = -1;
6441static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6442static int expand_option_flags = 0;
6443
6444 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006445set_context_in_set_cmd(
6446 expand_T *xp,
6447 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006448 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
6450 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006451 long_u flags = 0; // init for GCC
6452 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 char_u *p;
6454 char_u *s;
6455 int is_term_option = FALSE;
6456 int key;
6457
6458 expand_option_flags = opt_flags;
6459
6460 xp->xp_context = EXPAND_SETTINGS;
6461 if (*arg == NUL)
6462 {
6463 xp->xp_pattern = arg;
6464 return;
6465 }
6466 p = arg + STRLEN(arg) - 1;
6467 if (*p == ' ' && *(p - 1) != '\\')
6468 {
6469 xp->xp_pattern = p + 1;
6470 return;
6471 }
6472 while (p > arg)
6473 {
6474 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006475 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 if (*p == ' ' || *p == ',')
6477 {
6478 while (s > arg && *(s - 1) == '\\')
6479 --s;
6480 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006481 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if (*p == ' ' && ((p - s) & 1) == 0)
6483 {
6484 ++p;
6485 break;
6486 }
6487 --p;
6488 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006489 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 {
6491 xp->xp_context = EXPAND_BOOL_SETTINGS;
6492 p += 2;
6493 }
6494 if (STRNCMP(p, "inv", 3) == 0)
6495 {
6496 xp->xp_context = EXPAND_BOOL_SETTINGS;
6497 p += 3;
6498 }
6499 xp->xp_pattern = arg = p;
6500 if (*arg == '<')
6501 {
6502 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006503 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 return;
6505 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006506 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
6508 xp->xp_context = EXPAND_NOTHING;
6509 return;
6510 }
6511 nextchar = *++p;
6512 is_term_option = TRUE;
6513 expand_option_name[2] = KEY2TERMCAP0(key);
6514 expand_option_name[3] = KEY2TERMCAP1(key);
6515 }
6516 else
6517 {
6518 if (p[0] == 't' && p[1] == '_')
6519 {
6520 p += 2;
6521 if (*p != NUL)
6522 ++p;
6523 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006524 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 nextchar = *++p;
6526 is_term_option = TRUE;
6527 expand_option_name[2] = p[-2];
6528 expand_option_name[3] = p[-1];
6529 }
6530 else
6531 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006532 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6534 p++;
6535 if (*p == NUL)
6536 return;
6537 nextchar = *p;
6538 *p = NUL;
6539 opt_idx = findoption(arg);
6540 *p = nextchar;
6541 if (opt_idx == -1 || options[opt_idx].var == NULL)
6542 {
6543 xp->xp_context = EXPAND_NOTHING;
6544 return;
6545 }
6546 flags = options[opt_idx].flags;
6547 if (flags & P_BOOL)
6548 {
6549 xp->xp_context = EXPAND_NOTHING;
6550 return;
6551 }
6552 }
6553 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006554 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6556 {
6557 ++p;
6558 nextchar = '=';
6559 }
6560 if ((nextchar != '=' && nextchar != ':')
6561 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6562 {
6563 xp->xp_context = EXPAND_UNSUCCESSFUL;
6564 return;
6565 }
6566 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6567 {
6568 xp->xp_context = EXPAND_OLD_SETTING;
6569 if (is_term_option)
6570 expand_option_idx = -1;
6571 else
6572 expand_option_idx = opt_idx;
6573 xp->xp_pattern = p + 1;
6574 return;
6575 }
6576 xp->xp_context = EXPAND_NOTHING;
6577 if (is_term_option || (flags & P_NUM))
6578 return;
6579
6580 xp->xp_pattern = p + 1;
6581
6582 if (flags & P_EXPAND)
6583 {
6584 p = options[opt_idx].var;
6585 if (p == (char_u *)&p_bdir
6586 || p == (char_u *)&p_dir
6587 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006588 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591#ifdef FEAT_SESSION
6592 || p == (char_u *)&p_vdir
6593#endif
6594 )
6595 {
6596 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006597 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 xp->xp_backslash = XP_BS_THREE;
6599 else
6600 xp->xp_backslash = XP_BS_ONE;
6601 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006602 else if (p == (char_u *)&p_ft)
6603 {
6604 xp->xp_context = EXPAND_FILETYPE;
6605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 else
6607 {
6608 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006609 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 if (p == (char_u *)&p_tags)
6611 xp->xp_backslash = XP_BS_THREE;
6612 else
6613 xp->xp_backslash = XP_BS_ONE;
6614 }
6615 }
6616
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006617 // For an option that is a list of file names, find the start of the
6618 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006621 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 if (*p == ' ' || *p == ',')
6623 {
6624 s = p;
6625 while (s > xp->xp_pattern && *(s - 1) == '\\')
6626 --s;
6627 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6628 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6629 {
6630 xp->xp_pattern = p + 1;
6631 break;
6632 }
6633 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006634
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006635#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006636 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006637 if (options[opt_idx].var == (char_u *)&p_sps
6638 && STRNCMP(p, "file:", 5) == 0)
6639 {
6640 xp->xp_pattern = p + 5;
6641 break;
6642 }
6643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645}
6646
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006647/*
6648 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
6649 *
6650 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
6651 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
6652 *
6653 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
6654 * regular expression 'regmatch', then stores the match in matches[idx] and
6655 * returns TRUE.
6656 *
6657 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6658 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
6659 *
6660 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6661 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
6662 */
6663 static int
6664match_str(
6665 char_u *str,
6666 regmatch_T *regmatch,
6667 char_u **matches,
6668 int idx,
6669 int test_only,
6670 int fuzzy,
6671 char_u *fuzzystr,
6672 fuzmatch_str_T *fuzmatch)
6673{
6674 if (!fuzzy)
6675 {
6676 if (vim_regexec(regmatch, str, (colnr_T)0))
6677 {
6678 if (!test_only)
6679 matches[idx] = vim_strsave(str);
6680 return TRUE;
6681 }
6682 }
6683 else
6684 {
6685 int score;
6686
6687 score = fuzzy_match_str(str, fuzzystr);
6688 if (score != 0)
6689 {
6690 if (!test_only)
6691 {
6692 fuzmatch[idx].idx = idx;
6693 fuzmatch[idx].str = vim_strsave(str);
6694 fuzmatch[idx].score = score;
6695 }
6696
6697 return TRUE;
6698 }
6699 }
6700
6701 return FALSE;
6702}
6703
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006705ExpandSettings(
6706 expand_T *xp,
6707 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006708 char_u *fuzzystr,
6709 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01006710 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006711 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006713 int num_normal = 0; // Nr of matching non-term-code settings
6714 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 int opt_idx;
6716 int match;
6717 int count = 0;
6718 char_u *str;
6719 int loop;
6720 int is_term_opt;
6721 char_u name_buf[MAX_KEY_NAME_LEN];
6722 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006723 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006724 int fuzzy;
6725 fuzmatch_str_T *fuzmatch = NULL;
6726
Christian Brabandtcb747892022-05-08 21:10:56 +01006727 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006729 // do this loop twice:
6730 // loop == 0: count the number of matching options
6731 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 for (loop = 0; loop <= 1; ++loop)
6733 {
6734 regmatch->rm_ic = ic;
6735 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6736 {
K.Takataeeec2542021-06-02 13:28:16 +02006737 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006738 {
6739 if (match_str((char_u *)names[match], regmatch, *matches,
6740 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 {
6742 if (loop == 0)
6743 num_normal++;
6744 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006745 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 }
6749 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6750 opt_idx++)
6751 {
6752 if (options[opt_idx].var == NULL)
6753 continue;
6754 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6755 && !(options[opt_idx].flags & P_BOOL))
6756 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006757 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 if (is_term_opt && num_normal > 0)
6759 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006760
6761 if (match_str(str, regmatch, *matches, count, (loop == 0),
6762 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 {
6764 if (loop == 0)
6765 {
6766 if (is_term_opt)
6767 num_term++;
6768 else
6769 num_normal++;
6770 }
6771 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006772 count++;
6773 }
6774 else if (!fuzzy && options[opt_idx].shortname != NULL
6775 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006776 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006777 {
6778 // Compare against the abbreviated option name (for regular
6779 // expression match). Fuzzy matching (previous if) already
6780 // matches against both the expanded and abbreviated names.
6781 if (loop == 0)
6782 {
6783 if (is_term_opt)
6784 num_term++;
6785 else
6786 num_normal++;
6787 }
6788 else
6789 (*matches)[count++] = vim_strsave(str);
6790 }
6791 else if (is_term_opt)
6792 {
6793 name_buf[0] = '<';
6794 name_buf[1] = 't';
6795 name_buf[2] = '_';
6796 name_buf[3] = str[2];
6797 name_buf[4] = str[3];
6798 name_buf[5] = '>';
6799 name_buf[6] = NUL;
6800
6801 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6802 fuzzy, fuzzystr, fuzmatch))
6803 {
6804 if (loop == 0)
6805 num_term++;
6806 else
6807 count++;
6808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
6810 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006811
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 /*
6813 * Check terminal key codes, these are not in the option table
6814 */
6815 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6816 {
6817 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6818 {
6819 if (!isprint(str[0]) || !isprint(str[1]))
6820 continue;
6821
6822 name_buf[0] = 't';
6823 name_buf[1] = '_';
6824 name_buf[2] = str[0];
6825 name_buf[3] = str[1];
6826 name_buf[4] = NUL;
6827
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006828 if (match_str(name_buf, regmatch, *matches, count,
6829 (loop == 0), fuzzy, fuzzystr, fuzmatch))
6830 {
6831 if (loop == 0)
6832 num_term++;
6833 else
6834 count++;
6835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 else
6837 {
6838 name_buf[0] = '<';
6839 name_buf[1] = 't';
6840 name_buf[2] = '_';
6841 name_buf[3] = str[0];
6842 name_buf[4] = str[1];
6843 name_buf[5] = '>';
6844 name_buf[6] = NUL;
6845
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006846 if (match_str(name_buf, regmatch, *matches, count,
6847 (loop == 0), fuzzy, fuzzystr,
6848 fuzmatch))
6849 {
6850 if (loop == 0)
6851 num_term++;
6852 else
6853 count++;
6854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 }
6856 }
6857
6858 /*
6859 * Check special key names.
6860 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006861 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6863 {
6864 name_buf[0] = '<';
6865 STRCPY(name_buf + 1, str);
6866 STRCAT(name_buf, ">");
6867
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006868 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6869 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 {
6871 if (loop == 0)
6872 num_term++;
6873 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006874 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
6876 }
6877 }
6878 if (loop == 0)
6879 {
6880 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006881 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006883 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 else
6885 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006886 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006888 *matches = ALLOC_MULT(char_u *, *numMatches);
6889 if (*matches == NULL)
6890 {
6891 *matches = (char_u **)"";
6892 return FAIL;
6893 }
6894 }
6895 else
6896 {
6897 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
6898 if (fuzmatch == NULL)
6899 {
6900 *matches = (char_u **)"";
6901 return FAIL;
6902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 }
6904 }
6905 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006906
6907 if (fuzzy &&
6908 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
6909 return FAIL;
6910
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 return OK;
6912}
6913
6914 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00006915ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006917 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 char_u *buf;
6919
zeertzjqb0d45ec2023-01-25 15:04:22 +00006920 *numMatches = 0;
6921 *matches = ALLOC_ONE(char_u *);
6922 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 return FAIL;
6924
6925 /*
6926 * For a terminal key code expand_option_idx is < 0.
6927 */
6928 if (expand_option_idx < 0)
6929 {
6930 var = find_termcode(expand_option_name + 2);
6931 if (var == NULL)
6932 expand_option_idx = findoption(expand_option_name);
6933 }
6934
6935 if (expand_option_idx >= 0)
6936 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006937 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 option_value2string(&options[expand_option_idx], expand_option_flags);
6939 var = NameBuff;
6940 }
6941 else if (var == NULL)
6942 var = (char_u *)"";
6943
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006944 // A backslash is required before some characters. This is the reverse of
6945 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 buf = vim_strsave_escaped(var, escape_chars);
6947
6948 if (buf == NULL)
6949 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00006950 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 return FAIL;
6952 }
6953
6954#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006955 // For MS-Windows et al. we don't double backslashes at the start and
6956 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006957 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 if (var[0] == '\\' && var[1] == '\\'
6959 && expand_option_idx >= 0
6960 && (options[expand_option_idx].flags & P_EXPAND)
6961 && vim_isfilec(var[2])
6962 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006963 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964#endif
6965
zeertzjqb0d45ec2023-01-25 15:04:22 +00006966 *matches[0] = buf;
6967 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 return OK;
6969}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
6971/*
6972 * Get the value for the numeric or string option *opp in a nice format into
6973 * NameBuff[]. Must not be called with a hidden option!
6974 */
6975 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006976option_value2string(
6977 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006978 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979{
6980 char_u *varp;
6981
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006982 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
6984 if (opp->flags & P_NUM)
6985 {
6986 long wc = 0;
6987
6988 if (wc_use_keyname(varp, &wc))
6989 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6990 else if (wc != 0)
6991 STRCPY(NameBuff, transchar((int)wc));
6992 else
6993 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6994 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006995 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 {
6997 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006998 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 NameBuff[0] = NUL;
7000#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007001 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007002 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 STRCPY(NameBuff, "*****");
7004#endif
7005 else if (opp->flags & P_EXPAND)
7006 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007007 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 else if ((char_u **)opp->var == &p_pt)
7009 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7010 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007011 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 }
7013}
7014
7015/*
7016 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7017 * printed as a keyname.
7018 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7019 */
7020 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007021wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
7023 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7024 {
7025 *wcp = *(long *)varp;
7026 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7027 return TRUE;
7028 }
7029 return FALSE;
7030}
7031
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 * Return TRUE if "x" is present in 'shortmess' option, or
7034 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7035 */
7036 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007037shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007039 return p_shm != NULL &&
7040 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 || (vim_strchr(p_shm, 'a') != NULL
7042 && vim_strchr((char_u *)SHM_A, x) != NULL));
7043}
7044
7045/*
7046 * paste_option_changed() - Called after p_paste was set or reset.
7047 */
7048 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007049paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050{
7051 static int old_p_paste = FALSE;
7052 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007053 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055#ifdef FEAT_RIGHTLEFT
7056 static int save_ri = 0;
7057 static int save_hkmap = 0;
7058#endif
7059 buf_T *buf;
7060
7061 if (p_paste)
7062 {
7063 /*
7064 * Paste switched from off to on.
7065 * Save the current values, so they can be restored later.
7066 */
7067 if (!old_p_paste)
7068 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007069 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007070 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {
7072 buf->b_p_tw_nopaste = buf->b_p_tw;
7073 buf->b_p_wm_nopaste = buf->b_p_wm;
7074 buf->b_p_sts_nopaste = buf->b_p_sts;
7075 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007076 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007077#ifdef FEAT_VARTABS
7078 if (buf->b_p_vsts_nopaste)
7079 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007080 buf->b_p_vsts_nopaste =
7081 buf->b_p_vsts && buf->b_p_vsts != empty_option
7082 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 }
7085
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007086 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007088 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090#ifdef FEAT_RIGHTLEFT
7091 save_ri = p_ri;
7092 save_hkmap = p_hkmap;
7093#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007095 p_ai_nopaste = p_ai;
7096 p_et_nopaste = p_et;
7097 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 p_tw_nopaste = p_tw;
7099 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007100#ifdef FEAT_VARTABS
7101 if (p_vsts_nopaste)
7102 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007103 p_vsts_nopaste = p_vsts && p_vsts != empty_option
7104 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 }
7107
7108 /*
7109 * Always set the option values, also when 'paste' is set when it is
7110 * already on.
7111 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007112 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007113 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007115 buf->b_p_tw = 0; // textwidth is 0
7116 buf->b_p_wm = 0; // wrapmargin is 0
7117 buf->b_p_sts = 0; // softtabstop is 0
7118 buf->b_p_ai = 0; // no auto-indent
7119 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007120#ifdef FEAT_VARTABS
7121 if (buf->b_p_vsts)
7122 free_string_option(buf->b_p_vsts);
7123 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007124 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 }
7127
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007128 // set global options
7129 p_sm = 0; // no showmatch
7130 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007132 status_redraw_all(); // redraw to remove the ruler
7133 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007135 p_ri = 0; // no reverse insert
7136 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007138 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 p_tw = 0;
7140 p_wm = 0;
7141 p_sts = 0;
7142 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007143#ifdef FEAT_VARTABS
7144 if (p_vsts)
7145 free_string_option(p_vsts);
7146 p_vsts = empty_option;
7147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 }
7149
7150 /*
7151 * Paste switched from on to off: Restore saved values.
7152 */
7153 else if (old_p_paste)
7154 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007155 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007156 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 {
7158 buf->b_p_tw = buf->b_p_tw_nopaste;
7159 buf->b_p_wm = buf->b_p_wm_nopaste;
7160 buf->b_p_sts = buf->b_p_sts_nopaste;
7161 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007162 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007163#ifdef FEAT_VARTABS
7164 if (buf->b_p_vsts)
7165 free_string_option(buf->b_p_vsts);
7166 buf->b_p_vsts = buf->b_p_vsts_nopaste
7167 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007168 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007169 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007170 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007171 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007172 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 }
7175
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007176 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007178 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007180 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182#ifdef FEAT_RIGHTLEFT
7183 p_ri = save_ri;
7184 p_hkmap = save_hkmap;
7185#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007186 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007187 p_ai = p_ai_nopaste;
7188 p_et = p_et_nopaste;
7189 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 p_tw = p_tw_nopaste;
7191 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007192#ifdef FEAT_VARTABS
7193 if (p_vsts)
7194 free_string_option(p_vsts);
7195 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 }
7198
7199 old_p_paste = p_paste;
7200}
7201
7202/*
7203 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7204 *
7205 * Reset 'compatible' and set the values for options that didn't get set yet
7206 * to the Vim defaults.
7207 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007208 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 */
7210 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007211vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007213 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007214 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007215 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
7217 if (!option_was_set((char_u *)"cp"))
7218 {
7219 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007220 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7222 set_option_default(opt_idx, OPT_FREE, FALSE);
7223 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007224 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007226
7227 if (fname != NULL)
7228 {
7229 p = vim_getenv(envname, &dofree);
7230 if (p == NULL)
7231 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007232 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007233 p = FullName_save(fname, FALSE);
7234 if (p != NULL)
7235 {
7236 vim_setenv(envname, p);
7237 vim_free(p);
7238 }
7239 }
7240 else if (dofree)
7241 vim_free(p);
7242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243}
7244
7245/*
7246 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7247 */
7248 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007249change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007251 int opt_idx;
7252
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 if (p_cp != on)
7254 {
7255 p_cp = on;
7256 compatible_set();
7257 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007258 opt_idx = findoption((char_u *)"cp");
7259 if (opt_idx >= 0)
7260 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261}
7262
7263/*
7264 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007265 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 */
7267 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007268option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
7270 int idx;
7271
7272 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007273 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 return FALSE;
7275 if (options[idx].flags & P_WAS_SET)
7276 return TRUE;
7277 return FALSE;
7278}
7279
7280/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007281 * Reset the flag indicating option "name" was set.
7282 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007283 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007284reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007285{
7286 int idx = findoption(name);
7287
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007288 if (idx < 0)
7289 return FAIL;
7290
7291 options[idx].flags &= ~P_WAS_SET;
7292 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007293}
7294
7295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 * compatible_set() - Called when 'compatible' has been set or unset.
7297 *
7298 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7299 * flag) to a Vi compatible value.
7300 * When 'compatible' is unset: Set all options that have a different default
7301 * for Vim (without the P_VI_DEF flag) to that default.
7302 */
7303 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007304compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305{
7306 int opt_idx;
7307
Bram Moolenaardac13472019-09-16 21:06:21 +02007308 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7310 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7311 set_option_default(opt_idx, OPT_FREE, p_cp);
7312 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007313 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314}
7315
Bram Moolenaardac13472019-09-16 21:06:21 +02007316#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318/*
7319 * fill_breakat_flags() -- called when 'breakat' changes value.
7320 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007321 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007322fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007324 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 int i;
7326
7327 for (i = 0; i < 256; i++)
7328 breakat_flags[i] = FALSE;
7329
7330 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007331 for (p = p_breakat; *p; p++)
7332 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334#endif
7335
7336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 * Check if backspacing over something is allowed.
7338 */
7339 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007340can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007341 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007343#ifdef FEAT_JOB_CHANNEL
7344 if (what == BS_START && bt_prompt(curbuf))
7345 return FALSE;
7346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 switch (*p_bs)
7348 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007349 case '3': return TRUE;
7350 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 case '1': return (what != BS_START);
7352 case '0': return FALSE;
7353 }
7354 return vim_strchr(p_bs, what) != NULL;
7355}
7356
7357/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007358 * Return the effective 'scrolloff' value for the current window, using the
7359 * global value when appropriate.
7360 */
7361 long
7362get_scrolloff_value(void)
7363{
7364 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7365}
7366
7367/*
7368 * Return the effective 'sidescrolloff' value for the current window, using the
7369 * global value when appropriate.
7370 */
7371 long
7372get_sidescrolloff_value(void)
7373{
7374 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7375}
7376
7377/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007378 * Get the local or global value of 'backupcopy'.
7379 */
7380 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007381get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007382{
7383 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7384}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007385
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007386#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007387/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007388 * Get the local or global value of 'formatlistpat'.
7389 */
7390 char_u *
7391get_flp_value(buf_T *buf)
7392{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007393 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7394 return p_flp;
7395 return buf->b_p_flp;
7396}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007397#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007398
7399/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007400 * Get the local or global value of the 'virtualedit' flags.
7401 */
7402 unsigned int
7403get_ve_flags(void)
7404{
Gary Johnson51ad8502021-08-03 18:33:08 +02007405 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7406 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007407}
7408
Bram Moolenaaree857022019-11-09 23:26:40 +01007409#if defined(FEAT_LINEBREAK) || defined(PROTO)
7410/*
7411 * Get the local or global value of 'showbreak'.
7412 */
7413 char_u *
7414get_showbreak_value(win_T *win)
7415{
7416 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7417 return p_sbr;
7418 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7419 return empty_option;
7420 return win->w_p_sbr;
7421}
7422#endif
7423
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007424#if defined(FEAT_EVAL) || defined(PROTO)
7425/*
7426 * Get window or buffer local options.
7427 */
7428 dict_T *
7429get_winbuf_options(int bufopt)
7430{
7431 dict_T *d;
7432 int opt_idx;
7433
7434 d = dict_alloc();
7435 if (d == NULL)
7436 return NULL;
7437
Bram Moolenaardac13472019-09-16 21:06:21 +02007438 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007439 {
7440 struct vimoption *opt = &options[opt_idx];
7441
7442 if ((bufopt && (opt->indir & PV_BUF))
7443 || (!bufopt && (opt->indir & PV_WIN)))
7444 {
7445 char_u *varp = get_varp(opt);
7446
7447 if (varp != NULL)
7448 {
7449 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007450 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007451 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007452 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007453 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007454 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007455 }
7456 }
7457 }
7458
7459 return d;
7460}
7461#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007462
Bram Moolenaardac13472019-09-16 21:06:21 +02007463#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007464/*
7465 * This is called when 'culopt' is changed
7466 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007467 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007468fill_culopt_flags(char_u *val, win_T *wp)
7469{
7470 char_u *p;
7471 char_u culopt_flags_new = 0;
7472
7473 if (val == NULL)
7474 p = wp->w_p_culopt;
7475 else
7476 p = val;
7477 while (*p != NUL)
7478 {
7479 if (STRNCMP(p, "line", 4) == 0)
7480 {
7481 p += 4;
7482 culopt_flags_new |= CULOPT_LINE;
7483 }
7484 else if (STRNCMP(p, "both", 4) == 0)
7485 {
7486 p += 4;
7487 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7488 }
7489 else if (STRNCMP(p, "number", 6) == 0)
7490 {
7491 p += 6;
7492 culopt_flags_new |= CULOPT_NBR;
7493 }
7494 else if (STRNCMP(p, "screenline", 10) == 0)
7495 {
7496 p += 10;
7497 culopt_flags_new |= CULOPT_SCRLINE;
7498 }
7499
7500 if (*p != ',' && *p != NUL)
7501 return FAIL;
7502 if (*p == ',')
7503 ++p;
7504 }
7505
7506 // Can't have both "line" and "screenline".
7507 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7508 return FAIL;
7509 wp->w_p_culopt_flags = culopt_flags_new;
7510
7511 return OK;
7512}
7513#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007514
7515/*
7516 * Get the value of 'magic' adjusted for Vim9 script.
7517 */
7518 int
7519magic_isset(void)
7520{
7521 switch (magic_overruled)
7522 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007523 case OPTION_MAGIC_ON: return TRUE;
7524 case OPTION_MAGIC_OFF: return FALSE;
7525 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007526 }
7527#ifdef FEAT_EVAL
7528 if (in_vim9script())
7529 return TRUE;
7530#endif
7531 return p_magic;
7532}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007533
7534/*
7535 * Set the callback function value for an option that accepts a function name,
7536 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7537 * Returns OK if the option is successfully set to a function, otherwise
7538 * returns FAIL.
7539 */
7540 int
7541option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7542{
7543#ifdef FEAT_EVAL
7544 typval_T *tv;
7545 callback_T cb;
7546
7547 if (optval == NULL || *optval == NUL)
7548 {
7549 free_callback(optcb);
7550 return OK;
7551 }
7552
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007553 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007554 || (STRNCMP(optval, "function(", 9) == 0)
7555 || (STRNCMP(optval, "funcref(", 8) == 0))
7556 // Lambda expression or a funcref
7557 tv = eval_expr(optval, NULL);
7558 else
7559 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007560 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007561 if (tv == NULL)
7562 return FAIL;
7563
7564 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007565 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007566 {
7567 free_tv(tv);
7568 return FAIL;
7569 }
7570
7571 free_callback(optcb);
7572 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00007573 if (cb.cb_free_name)
7574 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007575 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007576
7577 // when using Vim9 style "import.funcname" it needs to be expanded to
7578 // "import#funcname".
7579 expand_autload_callback(optcb);
7580
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007581 return OK;
7582#else
7583 return FAIL;
7584#endif
7585}