blob: 1aed4e1a0d63223720b88026fb95bd50f1c32a86 [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
Bram Moolenaar47403942022-09-21 21:12:53 +01001214typedef enum {
1215 OP_NONE = 0,
1216 OP_ADDING, // "opt+=arg"
1217 OP_PREPENDING, // "opt^=arg"
1218 OP_REMOVING, // "opt-=arg"
1219} set_op_T;
1220
1221/*
1222 * Part of do_set() for string options.
1223 * Returns FAIL on failure, do not process further options.
1224 */
1225 static int
1226do_set_string(
1227 int opt_idx,
1228 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001229 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001230 int nextchar,
1231 set_op_T op_arg,
1232 int flags,
1233 int cp_val,
1234 char_u *varp_arg,
1235 char *errbuf,
1236 int *value_checked,
1237 char **errmsg)
1238{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001239 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001240 set_op_T op = op_arg;
1241 char_u *varp = varp_arg;
1242 char_u *save_arg = NULL;
1243 char_u *s = NULL;
1244 char_u *oldval = NULL; // previous value if *varp
1245 char_u *newval;
1246 char_u *origval = NULL;
1247 char_u *origval_l = NULL;
1248 char_u *origval_g = NULL;
1249#if defined(FEAT_EVAL)
1250 char_u *saved_origval = NULL;
1251 char_u *saved_origval_l = NULL;
1252 char_u *saved_origval_g = NULL;
1253 char_u *saved_newval = NULL;
1254#endif
1255 unsigned newlen;
1256 int comma;
1257 char_u whichwrap[80];
1258
1259 // When using ":set opt=val" for a global option
1260 // with a local value the local value will be
1261 // reset, use the global value here.
1262 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1263 && ((int)options[opt_idx].indir & PV_BOTH))
1264 varp = options[opt_idx].var;
1265
1266 // The old value is kept until we are sure that the new value is valid.
1267 oldval = *(char_u **)varp;
1268
1269 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1270 {
1271 origval_l = *(char_u **)get_varp_scope(
1272 &(options[opt_idx]), OPT_LOCAL);
1273 origval_g = *(char_u **)get_varp_scope(
1274 &(options[opt_idx]), OPT_GLOBAL);
1275
1276 // A global-local string option might have an empty option as value to
1277 // indicate that the global value should be used.
1278 if (((int)options[opt_idx].indir & PV_BOTH)
1279 && origval_l == empty_option)
1280 origval_l = origval_g;
1281 }
1282
1283 // When setting the local value of a global option, the old value may be
1284 // the global value.
1285 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1286 origval = *(char_u **)get_varp(&options[opt_idx]);
1287 else
1288 origval = oldval;
1289
1290 if (nextchar == '&') // set to default val
1291 {
1292 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1293 ? VI_DEFAULT : VIM_DEFAULT];
1294 if ((char_u **)varp == &p_bg)
1295 {
1296 // guess the value of 'background'
1297#ifdef FEAT_GUI
1298 if (gui.in_use)
1299 newval = gui_bg_default();
1300 else
1301#endif
1302 newval = term_bg_default();
1303 }
1304 else if ((char_u **)varp == &p_fencs && enc_utf8)
1305 newval = fencs_utf8_default;
1306
1307 // expand environment variables and ~ since the default value was
1308 // already expanded, only required when an environment variable was set
1309 // later
1310 if (newval == NULL)
1311 newval = empty_option;
1312 else
1313 {
1314 s = option_expand(opt_idx, newval);
1315 if (s == NULL)
1316 s = newval;
1317 newval = vim_strsave(s);
1318 }
1319 }
1320 else if (nextchar == '<') // set to global val
1321 {
1322 newval = vim_strsave(*(char_u **)get_varp_scope(
1323 &(options[opt_idx]), OPT_GLOBAL));
1324 }
1325 else
1326 {
Bram Moolenaar6f981142022-09-22 12:48:58 +01001327 ++arg; // jump to after the '=' or ':'
Bram Moolenaar47403942022-09-21 21:12:53 +01001328
1329 /*
1330 * Set 'keywordprg' to ":help" if an empty
1331 * value was passed to :set by the user.
Bram Moolenaar47403942022-09-21 21:12:53 +01001332 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001333 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
Bram Moolenaar47403942022-09-21 21:12:53 +01001334 {
Bram Moolenaar6f981142022-09-22 12:48:58 +01001335 save_arg = arg;
zeertzjqfcba86c2022-09-22 13:57:32 +01001336 arg = (char_u *)":help";
Bram Moolenaar47403942022-09-21 21:12:53 +01001337 }
1338 /*
1339 * Convert 'backspace' number to string, for
1340 * adding, prepending and removing string.
1341 */
1342 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1343 {
1344 int i = getdigits((char_u **)varp);
1345
1346 switch (i)
1347 {
1348 case 0:
1349 *(char_u **)varp = empty_option;
1350 break;
1351 case 1:
1352 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1353 break;
1354 case 2:
1355 *(char_u **)varp = vim_strsave(
1356 (char_u *)"indent,eol,start");
1357 break;
1358 case 3:
1359 *(char_u **)varp = vim_strsave(
1360 (char_u *)"indent,eol,nostop");
1361 break;
1362 }
1363 vim_free(oldval);
1364 if (origval == oldval)
1365 origval = *(char_u **)varp;
1366 if (origval_l == oldval)
1367 origval_l = *(char_u **)varp;
1368 if (origval_g == oldval)
1369 origval_g = *(char_u **)varp;
1370 oldval = *(char_u **)varp;
1371 }
1372 /*
1373 * Convert 'whichwrap' number to string, for backwards compatibility
1374 * with Vim 3.0.
1375 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001376 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
Bram Moolenaar47403942022-09-21 21:12:53 +01001377 {
1378 *whichwrap = NUL;
Bram Moolenaar6f981142022-09-22 12:48:58 +01001379 int i = getdigits(&arg);
Bram Moolenaar47403942022-09-21 21:12:53 +01001380 if (i & 1)
1381 STRCAT(whichwrap, "b,");
1382 if (i & 2)
1383 STRCAT(whichwrap, "s,");
1384 if (i & 4)
1385 STRCAT(whichwrap, "h,l,");
1386 if (i & 8)
1387 STRCAT(whichwrap, "<,>,");
1388 if (i & 16)
1389 STRCAT(whichwrap, "[,],");
1390 if (*whichwrap != NUL) // remove trailing ,
1391 whichwrap[STRLEN(whichwrap) - 1] = NUL;
Bram Moolenaar6f981142022-09-22 12:48:58 +01001392 save_arg = arg;
1393 arg = (char_u *)whichwrap;
Bram Moolenaar47403942022-09-21 21:12:53 +01001394 }
1395 /*
1396 * Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1397 * version 3.0
1398 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001399 else if (*arg == '>' && (varp == (char_u *)&p_dir
Bram Moolenaar47403942022-09-21 21:12:53 +01001400 || varp == (char_u *)&p_bdir))
Bram Moolenaar6f981142022-09-22 12:48:58 +01001401 ++arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001402
1403 /*
1404 * Copy the new string into allocated memory.
1405 * Can't use set_string_option_direct(), because we need to remove the
1406 * backslashes.
1407 */
1408 // get a bit too much
Bram Moolenaar6f981142022-09-22 12:48:58 +01001409 newlen = (unsigned)STRLEN(arg) + 1;
Bram Moolenaar47403942022-09-21 21:12:53 +01001410 if (op != OP_NONE)
1411 newlen += (unsigned)STRLEN(origval) + 1;
1412 newval = alloc(newlen);
1413 if (newval == NULL) // out of mem, don't change
1414 return FAIL;
1415 s = newval;
1416
1417 /*
1418 * Copy the string, skip over escaped chars.
1419 * For MS-DOS and WIN32 backslashes before normal file name characters
1420 * are not removed, and keep backslash at start, for "\\machine\path",
1421 * but do remove it for "\\\\machine\\path".
1422 * The reverse is found in ExpandOldSetting().
1423 */
zeertzjqfcba86c2022-09-22 13:57:32 +01001424 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar47403942022-09-21 21:12:53 +01001425 {
1426 int i;
1427
Bram Moolenaar6f981142022-09-22 12:48:58 +01001428 if (*arg == '\\' && arg[1] != NUL
Bram Moolenaar47403942022-09-21 21:12:53 +01001429#ifdef BACKSLASH_IN_FILENAME
1430 && !((flags & P_EXPAND)
Bram Moolenaar6f981142022-09-22 12:48:58 +01001431 && vim_isfilec(arg[1])
1432 && !VIM_ISWHITE(arg[1])
1433 && (arg[1] != '\\'
zeertzjqfcba86c2022-09-22 13:57:32 +01001434 || (s == newval && arg[2] != '\\')))
Bram Moolenaar47403942022-09-21 21:12:53 +01001435#endif
1436 )
Bram Moolenaar6f981142022-09-22 12:48:58 +01001437 ++arg; // remove backslash
1438 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar47403942022-09-21 21:12:53 +01001439 {
1440 // copy multibyte char
Bram Moolenaar6f981142022-09-22 12:48:58 +01001441 mch_memmove(s, arg, (size_t)i);
1442 arg += i;
Bram Moolenaar47403942022-09-21 21:12:53 +01001443 s += i;
1444 }
1445 else
Bram Moolenaar6f981142022-09-22 12:48:58 +01001446 *s++ = *arg++;
Bram Moolenaar47403942022-09-21 21:12:53 +01001447 }
1448 *s = NUL;
1449
1450 /*
1451 * Expand environment variables and ~.
1452 * Don't do it when adding without inserting a comma.
1453 */
1454 if (op == OP_NONE || (flags & P_COMMA))
1455 {
1456 s = option_expand(opt_idx, newval);
1457 if (s != NULL)
1458 {
1459 vim_free(newval);
1460 newlen = (unsigned)STRLEN(s) + 1;
1461 if (op != OP_NONE)
1462 newlen += (unsigned)STRLEN(origval) + 1;
1463 newval = alloc(newlen);
1464 if (newval == NULL)
1465 return FAIL;
1466 STRCPY(newval, s);
1467 }
1468 }
1469
1470 // locate newval[] in origval[] when removing it
1471 // and when adding to avoid duplicates
1472 int len = 0;
1473 if (op == OP_REMOVING || (flags & P_NODUP))
1474 {
1475 len = (int)STRLEN(newval);
1476 s = find_dup_item(origval, newval, flags);
1477
1478 // do not add if already there
1479 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1480 {
1481 op = OP_NONE;
1482 STRCPY(newval, origval);
1483 }
1484
1485 // if no duplicate, move pointer to end of original value
1486 if (s == NULL)
1487 s = origval + (int)STRLEN(origval);
1488 }
1489
1490 // concatenate the two strings; add a ',' if needed
1491 if (op == OP_ADDING || op == OP_PREPENDING)
1492 {
1493 comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1494 if (op == OP_ADDING)
1495 {
1496 len = (int)STRLEN(origval);
1497 // strip a trailing comma, would get 2
1498 if (comma && len > 1
1499 && (flags & P_ONECOMMA) == P_ONECOMMA
1500 && origval[len - 1] == ','
1501 && origval[len - 2] != '\\')
1502 len--;
1503 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1504 mch_memmove(newval, origval, (size_t)len);
1505 }
1506 else
1507 {
1508 len = (int)STRLEN(newval);
1509 STRMOVE(newval + len + comma, origval);
1510 }
1511 if (comma)
1512 newval[len] = ',';
1513 }
1514
1515 // Remove newval[] from origval[]. (Note: "len" has been set above and
1516 // is used here).
1517 if (op == OP_REMOVING)
1518 {
1519 STRCPY(newval, origval);
1520 if (*s)
1521 {
1522 // may need to remove a comma
1523 if (flags & P_COMMA)
1524 {
1525 if (s == origval)
1526 {
1527 // include comma after string
1528 if (s[len] == ',')
1529 ++len;
1530 }
1531 else
1532 {
1533 // include comma before string
1534 --s;
1535 ++len;
1536 }
1537 }
1538 STRMOVE(newval + (s - origval), s + len);
1539 }
1540 }
1541
1542 if (flags & P_FLAGLIST)
1543 {
1544 // Remove flags that appear twice.
1545 for (s = newval; *s;)
1546 {
1547 // if options have P_FLAGLIST and P_ONECOMMA such as
1548 // 'whichwrap'
1549 if (flags & P_ONECOMMA)
1550 {
1551 if (*s != ',' && *(s + 1) == ','
1552 && vim_strchr(s + 2, *s) != NULL)
1553 {
1554 // Remove the duplicated value and the next comma.
1555 STRMOVE(s, s + 2);
1556 continue;
1557 }
1558 }
1559 else
1560 {
1561 if ((!(flags & P_COMMA) || *s != ',')
1562 && vim_strchr(s + 1, *s) != NULL)
1563 {
1564 STRMOVE(s, s + 1);
1565 continue;
1566 }
1567 }
1568 ++s;
1569 }
1570 }
1571
zeertzjqfcba86c2022-09-22 13:57:32 +01001572 if (save_arg != NULL)
1573 arg = save_arg; // arg was temporarily changed, restore it
Bram Moolenaar47403942022-09-21 21:12:53 +01001574 }
1575
1576 /*
1577 * Set the new value.
1578 */
1579 *(char_u **)(varp) = newval;
1580
1581#if defined(FEAT_EVAL)
1582 if (!starting
1583# ifdef FEAT_CRYPT
1584 && options[opt_idx].indir != PV_KEY
1585# endif
1586 && origval != NULL && newval != NULL)
1587 {
1588 // origval may be freed by did_set_string_option(), make a copy.
1589 saved_origval = vim_strsave(origval);
1590 // newval (and varp) may become invalid if the buffer is closed by
1591 // autocommands.
1592 saved_newval = vim_strsave(newval);
1593 if (origval_l != NULL)
1594 saved_origval_l = vim_strsave(origval_l);
1595 if (origval_g != NULL)
1596 saved_origval_g = vim_strsave(origval_g);
1597 }
1598#endif
1599
1600 {
1601 long_u *p = insecure_flag(opt_idx, opt_flags);
1602 int secure_saved = secure;
1603
1604 // When an option is set in the sandbox, from a modeline or in secure
1605 // mode, then deal with side effects in secure mode. Also when the
1606 // value was set with the P_INSECURE flag and is not completely
1607 // replaced.
1608 if ((opt_flags & OPT_MODELINE)
1609#ifdef HAVE_SANDBOX
1610 || sandbox != 0
1611#endif
1612 || (op != OP_NONE && (*p & P_INSECURE)))
1613 secure = 1;
1614
1615 // Handle side effects, and set the global value for ":set" on local
1616 // options. Note: when setting 'syntax' or 'filetype' autocommands may
1617 // be triggered that can cause havoc.
1618 *errmsg = did_set_string_option(
1619 opt_idx, (char_u **)varp, oldval, errbuf,
1620 opt_flags, value_checked);
1621
1622 secure = secure_saved;
1623 }
1624
1625#if defined(FEAT_EVAL)
1626 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00001627 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01001628 saved_origval_l, saved_origval_g, saved_newval);
1629 vim_free(saved_origval);
1630 vim_free(saved_origval_l);
1631 vim_free(saved_origval_g);
1632 vim_free(saved_newval);
1633#endif
1634
Bram Moolenaar6f981142022-09-22 12:48:58 +01001635 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001636 return *errmsg == NULL ? OK : FAIL;
1637}
1638
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00001640 * Set an option to a new value.
1641 * Return NULL if OK, return an untranslated error message when something is
1642 * wrong. "errbuf[errbuflen]" can be used to create the error message.
1643 */
1644 static char *
1645do_set_option(
1646 int opt_flags,
1647 char_u **argp,
1648 char_u *arg_start,
1649 char_u **startarg,
1650 int *did_show,
1651 int *stopopteval,
1652 char *errbuf,
1653 size_t errbuflen)
1654{
1655 char *errmsg = NULL;
1656 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1657 int nextchar; // next non-white char after option name
1658 int afterchar; // character just after option name
1659 char_u *arg = *argp;
1660 int key;
1661 int opt_idx;
1662 int len;
1663 set_op_T op = 0;
1664 long_u flags; // flags for current option
1665 char_u *varp = NULL; // pointer to variable for current option
1666 char_u key_name[2];
1667 int cp_val = 0;
1668 varnumber_T value;
1669 int i;
1670
1671 prefix = 1;
1672 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1673 {
1674 prefix = 0;
1675 arg += 2;
1676 }
1677 else if (STRNCMP(arg, "inv", 3) == 0)
1678 {
1679 prefix = 2;
1680 arg += 3;
1681 }
1682
1683 // find end of name
1684 key = 0;
1685 if (*arg == '<')
1686 {
1687 opt_idx = -1;
1688 // look out for <t_>;>
1689 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1690 len = 5;
1691 else
1692 {
1693 len = 1;
1694 while (arg[len] != NUL && arg[len] != '>')
1695 ++len;
1696 }
1697 if (arg[len] != '>')
1698 {
1699 errmsg = e_invalid_argument;
1700 goto skip;
1701 }
1702 arg[len] = NUL; // put NUL after name
1703 if (arg[1] == 't' && arg[2] == '_') // could be term code
1704 opt_idx = findoption(arg + 1);
1705 arg[len++] = '>'; // restore '>'
1706 if (opt_idx == -1)
1707 key = find_key_option(arg + 1, TRUE);
1708 }
1709 else
1710 {
1711 len = 0;
1712 /*
1713 * The two characters after "t_" may not be alphanumeric.
1714 */
1715 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1716 len = 4;
1717 else
1718 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1719 ++len;
1720 nextchar = arg[len];
1721 arg[len] = NUL; // put NUL after name
1722 opt_idx = findoption(arg);
1723 arg[len] = nextchar; // restore nextchar
1724 if (opt_idx == -1)
1725 key = find_key_option(arg, FALSE);
1726 }
1727
1728 // remember character after option name
1729 afterchar = arg[len];
1730
1731 if (in_vim9script())
1732 {
1733 char_u *p = skipwhite(arg + len);
1734
1735 // disallow white space before =val, +=val, -=val, ^=val
1736 if (p > arg + len && (p[0] == '='
1737 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1738 && p[1] == '=')))
1739 {
1740 errmsg = e_no_white_space_allowed_between_option_and;
1741 arg = p;
1742 *startarg = p;
1743 goto skip;
1744 }
1745 }
1746 else
1747 // skip white space, allow ":set ai ?", ":set hlsearch !"
1748 while (VIM_ISWHITE(arg[len]))
1749 ++len;
1750
1751 op = OP_NONE;
1752 if (arg[len] != NUL && arg[len + 1] == '=')
1753 {
1754 if (arg[len] == '+')
1755 {
1756 op = OP_ADDING; // "+="
1757 ++len;
1758 }
1759 else if (arg[len] == '^')
1760 {
1761 op = OP_PREPENDING; // "^="
1762 ++len;
1763 }
1764 else if (arg[len] == '-')
1765 {
1766 op = OP_REMOVING; // "-="
1767 ++len;
1768 }
1769 }
1770 nextchar = arg[len];
1771
1772 if (opt_idx == -1 && key == 0) // found a mismatch: skip
1773 {
1774 if (in_vim9script() && arg > arg_start
1775 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1776 errmsg = e_no_white_space_allowed_between_option_and;
1777 else
1778 errmsg = e_unknown_option;
1779 goto skip;
1780 }
1781
1782 if (opt_idx >= 0)
1783 {
1784 if (options[opt_idx].var == NULL) // hidden option: skip
1785 {
1786 // Only give an error message when requesting the value of
1787 // a hidden option, ignore setting it.
1788 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1789 && (!(options[opt_idx].flags & P_BOOL)
1790 || nextchar == '?'))
1791 errmsg = e_option_not_supported;
1792 goto skip;
1793 }
1794
1795 flags = options[opt_idx].flags;
1796 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1797 }
1798 else
1799 {
1800 flags = P_STRING;
1801 if (key < 0)
1802 {
1803 key_name[0] = KEY2TERMCAP0(key);
1804 key_name[1] = KEY2TERMCAP1(key);
1805 }
1806 else
1807 {
1808 key_name[0] = KS_KEY;
1809 key_name[1] = (key & 0xff);
1810 }
1811 }
1812
1813 // Skip all options that are not window-local (used when showing
1814 // an already loaded buffer in a window).
1815 if ((opt_flags & OPT_WINONLY)
1816 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1817 goto skip;
1818
1819 // Skip all options that are window-local (used for :vimgrep).
1820 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1821 && options[opt_idx].var == VAR_WIN)
1822 goto skip;
1823
1824 // Disallow changing some options from modelines.
1825 if (opt_flags & OPT_MODELINE)
1826 {
1827 if (flags & (P_SECURE | P_NO_ML))
1828 {
1829 errmsg = e_not_allowed_in_modeline;
1830 goto skip;
1831 }
1832 if ((flags & P_MLE) && !p_mle)
1833 {
1834 errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1835 goto skip;
1836 }
1837#ifdef FEAT_DIFF
1838 // In diff mode some options are overruled. This avoids that
1839 // 'foldmethod' becomes "marker" instead of "diff" and that
1840 // "wrap" gets set.
1841 if (curwin->w_p_diff
1842 && opt_idx >= 0 // shut up coverity warning
1843 && (
1844#ifdef FEAT_FOLDING
1845 options[opt_idx].indir == PV_FDM ||
1846#endif
1847 options[opt_idx].indir == PV_WRAP))
1848 goto skip;
1849#endif
1850 }
1851
1852#ifdef HAVE_SANDBOX
1853 // Disallow changing some options in the sandbox
1854 if (sandbox != 0 && (flags & P_SECURE))
1855 {
1856 errmsg = e_not_allowed_in_sandbox;
1857 goto skip;
1858 }
1859#endif
1860
1861 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1862 {
1863 arg += len;
1864 cp_val = p_cp;
1865 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1866 {
1867 if (arg[3] == 'm') // "opt&vim": set to Vim default
1868 {
1869 cp_val = FALSE;
1870 arg += 3;
1871 }
1872 else // "opt&vi": set to Vi default
1873 {
1874 cp_val = TRUE;
1875 arg += 2;
1876 }
1877 }
1878 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
1879 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
1880 {
1881 errmsg = e_trailing_characters;
1882 goto skip;
1883 }
1884 }
1885
1886 /*
1887 * allow '=' and ':' for historical reasons (MSDOS command.com
1888 * allows only one '=' character per "set" command line. grrr. (jw)
1889 */
1890 if (nextchar == '?'
1891 || (prefix == 1
1892 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1893 && !(flags & P_BOOL)))
1894 {
1895 /*
1896 * print value
1897 */
1898 if (*did_show)
1899 msg_putchar('\n'); // cursor below last one
1900 else
1901 {
1902 gotocmdline(TRUE); // cursor at status line
1903 *did_show = TRUE; // remember that we did a line
1904 }
1905 if (opt_idx >= 0)
1906 {
1907 showoneopt(&options[opt_idx], opt_flags);
1908#ifdef FEAT_EVAL
1909 if (p_verbose > 0)
1910 {
1911 // Mention where the option was last set.
1912 if (varp == options[opt_idx].var)
1913 last_set_msg(options[opt_idx].script_ctx);
1914 else if ((int)options[opt_idx].indir & PV_WIN)
1915 last_set_msg(curwin->w_p_script_ctx[
1916 (int)options[opt_idx].indir & PV_MASK]);
1917 else if ((int)options[opt_idx].indir & PV_BUF)
1918 last_set_msg(curbuf->b_p_script_ctx[
1919 (int)options[opt_idx].indir & PV_MASK]);
1920 }
1921#endif
1922 }
1923 else
1924 {
1925 char_u *p;
1926
1927 p = find_termcode(key_name);
1928 if (p == NULL)
1929 {
1930 errmsg = e_key_code_not_set;
1931 goto skip;
1932 }
1933 else
1934 (void)show_one_termcode(key_name, p, TRUE);
1935 }
1936 if (nextchar != '?'
1937 && nextchar != NUL && !VIM_ISWHITE(afterchar))
1938 errmsg = e_trailing_characters;
1939 }
1940 else
1941 {
1942 int value_checked = FALSE;
1943
1944 if (flags & P_BOOL) // boolean
1945 {
1946 if (nextchar == '=' || nextchar == ':')
1947 {
1948 errmsg = e_invalid_argument;
1949 goto skip;
1950 }
1951
1952 /*
1953 * ":set opt!": invert
1954 * ":set opt&": reset to default value
1955 * ":set opt<": reset to global value
1956 */
1957 if (nextchar == '!')
1958 value = *(int *)(varp) ^ 1;
1959 else if (nextchar == '&')
1960 value = (int)(long)(long_i)options[opt_idx].def_val[
1961 ((flags & P_VI_DEF) || cp_val)
1962 ? VI_DEFAULT : VIM_DEFAULT];
1963 else if (nextchar == '<')
1964 {
1965 // For 'autoread' -1 means to use global value.
1966 if ((int *)varp == &curbuf->b_p_ar
1967 && opt_flags == OPT_LOCAL)
1968 value = -1;
1969 else
1970 value = *(int *)get_varp_scope(&(options[opt_idx]),
1971 OPT_GLOBAL);
1972 }
1973 else
1974 {
1975 /*
1976 * ":set invopt": invert
1977 * ":set opt" or ":set noopt": set or reset
1978 */
1979 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
1980 {
1981 errmsg = e_trailing_characters;
1982 goto skip;
1983 }
1984 if (prefix == 2) // inv
1985 value = *(int *)(varp) ^ 1;
1986 else
1987 value = prefix;
1988 }
1989
1990 errmsg = set_bool_option(opt_idx, varp, (int)value,
1991 opt_flags);
1992 }
1993 else // numeric or string
1994 {
1995 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1996 || prefix != 1)
1997 {
1998 errmsg = e_invalid_argument;
1999 goto skip;
2000 }
2001
2002 if (flags & P_NUM) // numeric
2003 {
2004 /*
2005 * Different ways to set a number option:
2006 * & set to default value
2007 * < set to global value
2008 * <xx> accept special key codes for 'wildchar'
2009 * c accept any non-digit for 'wildchar'
2010 * [-]0-9 set number
2011 * other error
2012 */
2013 ++arg;
2014 if (nextchar == '&')
2015 value = (long)(long_i)options[opt_idx].def_val[
2016 ((flags & P_VI_DEF) || cp_val)
2017 ? VI_DEFAULT : VIM_DEFAULT];
2018 else if (nextchar == '<')
2019 {
2020 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
2021 // use the global value.
2022 if ((long *)varp == &curbuf->b_p_ul
2023 && opt_flags == OPT_LOCAL)
2024 value = NO_LOCAL_UNDOLEVEL;
2025 else
2026 value = *(long *)get_varp_scope(
2027 &(options[opt_idx]), OPT_GLOBAL);
2028 }
2029 else if (((long *)varp == &p_wc
2030 || (long *)varp == &p_wcm)
2031 && (*arg == '<'
2032 || *arg == '^'
2033 || (*arg != NUL
2034 && (!arg[1] || VIM_ISWHITE(arg[1]))
2035 && !VIM_ISDIGIT(*arg))))
2036 {
2037 value = string_to_key(arg, FALSE);
2038 if (value == 0 && (long *)varp != &p_wcm)
2039 {
2040 errmsg = e_invalid_argument;
2041 goto skip;
2042 }
2043 }
2044 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2045 {
2046 // Allow negative (for 'undolevels'), octal and
2047 // hex numbers.
2048 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
2049 &value, NULL, 0, TRUE);
2050 if (i == 0 || (arg[i] != NUL
2051 && !VIM_ISWHITE(arg[i])))
2052 {
2053 errmsg = e_number_required_after_equal;
2054 goto skip;
2055 }
2056 }
2057 else
2058 {
2059 errmsg = e_number_required_after_equal;
2060 goto skip;
2061 }
2062
2063 if (op == OP_ADDING)
2064 value = *(long *)varp + value;
2065 else if (op == OP_PREPENDING)
2066 value = *(long *)varp * value;
2067 else if (op == OP_REMOVING)
2068 value = *(long *)varp - value;
2069 errmsg = set_num_option(opt_idx, varp, value,
2070 errbuf, errbuflen, opt_flags);
2071 }
2072 else if (opt_idx >= 0) // string
2073 {
2074 if (do_set_string(opt_idx, opt_flags, &arg, nextchar,
2075 op, flags, cp_val, varp, errbuf,
2076 &value_checked, &errmsg) == FAIL)
2077 {
2078 if (errmsg != NULL)
2079 goto skip;
2080 *stopopteval = TRUE;
2081 goto skip;
2082 }
2083 }
2084 else // key code option
2085 {
2086 char_u *p;
2087
2088 if (nextchar == '&')
2089 {
2090 if (add_termcap_entry(key_name, TRUE) == FAIL)
2091 errmsg = e_not_found_in_termcap;
2092 }
2093 else
2094 {
2095 ++arg; // jump to after the '=' or ':'
2096 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2097 if (*p == '\\' && p[1] != NUL)
2098 ++p;
2099 nextchar = *p;
2100 *p = NUL;
2101 add_termcode(key_name, arg, FALSE);
2102 *p = nextchar;
2103 }
2104 if (full_screen)
2105 ttest(FALSE);
2106 redraw_all_later(UPD_CLEAR);
2107 }
2108 }
2109
2110 if (opt_idx >= 0)
2111 did_set_option(
2112 opt_idx, opt_flags, op == OP_NONE, value_checked);
2113 }
2114
2115skip:
2116 *argp = arg;
2117 return errmsg;
2118}
2119
2120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 * Parse 'arg' for option settings.
2122 *
2123 * 'arg' may be IObuff, but only when no errors can be present and option
2124 * does not need to be expanded with option_expand().
2125 * "opt_flags":
2126 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002127 * OPT_GLOBAL for ":setglobal"
2128 * OPT_LOCAL for ":setlocal" and a modeline
2129 * OPT_MODELINE for a modeline
2130 * OPT_WINONLY to only set window-local options
2131 * OPT_NOWIN to skip setting window-local options
2132 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002134 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 */
2136 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002137do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002138 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002139 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002141 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002143 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
2145 if (*arg == NUL)
2146 {
2147 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002148 did_show = TRUE;
2149 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002152 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002154 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002155 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
2157 /*
2158 * ":set all" show all options.
2159 * ":set all&" set all options to their default value.
2160 */
2161 arg += 3;
2162 if (*arg == '&')
2163 {
2164 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002165 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002167 didset_options();
2168 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002169 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
2171 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002174 did_show = TRUE;
2175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002177 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
2179 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002180 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002181 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 arg += 7;
2183 }
2184 else
2185 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002186 int stopopteval = FALSE;
2187 char *errmsg = NULL;
2188 char errbuf[80];
2189 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002191 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2192 &did_show, &stopopteval, errbuf,
2193 sizeof(errbuf));
2194 if (stopopteval)
2195 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 /*
2198 * Advance to next argument.
2199 * - skip until a blank found, taking care of backslashes
2200 * - skip blanks
2201 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2202 */
2203 for (i = 0; i < 2 ; ++i)
2204 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002205 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 if (*arg++ == '\\' && *arg != NUL)
2207 ++arg;
2208 arg = skipwhite(arg);
2209 if (*arg != '=')
2210 break;
2211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002213 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002215 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2216 i = (int)STRLEN(IObuff) + 2;
2217 if (i + (arg - startarg) < IOSIZE)
2218 {
2219 // append the argument with the error
2220 STRCAT(IObuff, ": ");
2221 mch_memmove(IObuff + i, startarg, (arg - startarg));
2222 IObuff[i + (arg - startarg)] = NUL;
2223 }
2224 // make sure all characters are printable
2225 trans_characters(IObuff, IOSIZE);
2226
2227 ++no_wait_return; // wait_return() done later
2228 emsg((char *)IObuff); // show error highlighted
2229 --no_wait_return;
2230
2231 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 }
2234
2235 arg = skipwhite(arg);
2236 }
2237
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002238theend:
2239 if (silent_mode && did_show)
2240 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002241 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002242 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002243 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002244 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002245 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002246 out_flush();
2247 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002248 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002249 }
2250
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 return OK;
2252}
2253
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002254/*
2255 * Call this when an option has been given a new value through a user command.
2256 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2257 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002258 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002259did_set_option(
2260 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002261 int opt_flags, // possibly with OPT_MODELINE
2262 int new_value, // value was replaced completely
2263 int value_checked) // value was checked to be safe, no need to set the
2264 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002265{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002266 long_u *p;
2267
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002268 options[opt_idx].flags |= P_WAS_SET;
2269
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002270 // When an option is set in the sandbox, from a modeline or in secure mode
2271 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2272 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002273 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002274 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002275#ifdef HAVE_SANDBOX
2276 || sandbox != 0
2277#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002278 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002279 *p = *p | P_INSECURE;
2280 else if (new_value)
2281 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002282}
2283
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284/*
2285 * Convert a key name or string into a key value.
2286 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002287 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002289 int
2290string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002293 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 if (*arg == '^')
2295 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002296 if (multi_byte)
2297 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 return *arg;
2299}
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301/*
2302 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2303 * maketitle() to create and display it.
2304 * When switching the title or icon off, call mch_restore_title() to get
2305 * the old value back.
2306 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002307 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002308did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309{
2310 if (starting != NO_SCREEN
2311#ifdef FEAT_GUI
2312 && !gui.starting
2313#endif
2314 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318/*
2319 * set_options_bin - called when 'bin' changes value.
2320 */
2321 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002322set_options_bin(
2323 int oldval,
2324 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002325 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326{
2327 /*
2328 * The option values that are changed when 'bin' changes are
2329 * copied when 'bin is set and restored when 'bin' is reset.
2330 */
2331 if (newval)
2332 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002333 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
2335 if (!(opt_flags & OPT_GLOBAL))
2336 {
2337 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2338 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2339 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2340 curbuf->b_p_et_nobin = curbuf->b_p_et;
2341 }
2342 if (!(opt_flags & OPT_LOCAL))
2343 {
2344 p_tw_nobin = p_tw;
2345 p_wm_nobin = p_wm;
2346 p_ml_nobin = p_ml;
2347 p_et_nobin = p_et;
2348 }
2349 }
2350
2351 if (!(opt_flags & OPT_GLOBAL))
2352 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002353 curbuf->b_p_tw = 0; // no automatic line wrap
2354 curbuf->b_p_wm = 0; // no automatic line wrap
2355 curbuf->b_p_ml = 0; // no modelines
2356 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
2358 if (!(opt_flags & OPT_LOCAL))
2359 {
2360 p_tw = 0;
2361 p_wm = 0;
2362 p_ml = FALSE;
2363 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002364 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002367 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
2369 if (!(opt_flags & OPT_GLOBAL))
2370 {
2371 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2372 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2373 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2374 curbuf->b_p_et = curbuf->b_p_et_nobin;
2375 }
2376 if (!(opt_flags & OPT_LOCAL))
2377 {
2378 p_tw = p_tw_nobin;
2379 p_wm = p_wm_nobin;
2380 p_ml = p_ml_nobin;
2381 p_et = p_et_nobin;
2382 }
2383 }
2384}
2385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386/*
2387 * Expand environment variables for some string options.
2388 * These string options cannot be indirect!
2389 * If "val" is NULL expand the current value of the option.
2390 * Return pointer to NameBuff, or NULL when not expanded.
2391 */
2392 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002393option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002395 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2397 return NULL;
2398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002399 // If val is longer than MAXPATHL no meaningful expansion can be done,
2400 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 if (val != NULL && STRLEN(val) > MAXPATHL)
2402 return NULL;
2403
2404 if (val == NULL)
2405 val = *(char_u **)options[opt_idx].var;
2406
2407 /*
2408 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2409 * Escape spaces when expanding 'tags', they are used to separate file
2410 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002411 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 */
2413 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002414 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002415#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002416 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2417#endif
2418 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002419 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 return NULL;
2421
2422 return NameBuff;
2423}
2424
2425/*
2426 * After setting various option values: recompute variables that depend on
2427 * option values.
2428 */
2429 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002430didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002432 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 (void)init_chartab();
2434
Bram Moolenaardac13472019-09-16 21:06:21 +02002435 didset_string_options();
2436
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002437#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002438 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002439 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002440 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002441 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002442#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002443 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 (void)check_cedit();
Bram Moolenaar597a4222014-06-25 14:39:50 +02002445#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002446 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002447 fill_breakat_flags();
2448#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002449 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002450}
2451
2452/*
2453 * More side effects of setting options.
2454 */
2455 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002456didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002457{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002458 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002459 (void)highlight_changed();
2460
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002461 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002462 check_opt_wim();
2463
Bram Moolenaareed9d462021-02-15 20:38:25 +01002464 // Parse default for 'listchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002465 (void)set_chars_option(curwin, &curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002467 // Parse default for 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002468 (void)set_chars_option(curwin, &curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002469
2470#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002471 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002472 (void)check_clipboard_option();
2473#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002474#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002475 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002476 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002477 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002478 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480}
2481
2482/*
2483 * Check for string options that are NULL (normally only termcap options).
2484 */
2485 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002486check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
2488 int opt_idx;
2489
2490 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2491 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2492 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2493}
2494
2495/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002496 * Return the option index found by a pointer into term_strings[].
2497 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002499 int
2500get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002502 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503
2504 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2505 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002506 return opt_idx;
2507 return -1; // cannot happen: didn't find it!
2508}
2509
2510/*
2511 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2512 * Return the option index or -1 if not found.
2513 */
2514 int
2515set_term_option_alloced(char_u **p)
2516{
2517 int opt_idx = get_term_opt_idx(p);
2518
2519 if (opt_idx >= 0)
2520 options[opt_idx].flags |= P_ALLOCED;
2521 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522}
2523
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002524#if defined(FEAT_EVAL) || defined(PROTO)
2525/*
2526 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2527 * Return FALSE when it wasn't.
2528 * Return -1 for an unknown option.
2529 */
2530 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002531was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002532{
2533 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002535
2536 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002537 {
2538 flagp = insecure_flag(idx, opt_flags);
2539 return (*flagp & P_INSECURE) != 0;
2540 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002541 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002542 return -1;
2543}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002544
2545/*
2546 * Get a pointer to the flags used for the P_INSECURE flag of option
2547 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002548 * NOTE: Caller must make sure that "curwin" is set to the window from which
2549 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002550 */
2551 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002552insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002553{
2554 if (opt_flags & OPT_LOCAL)
2555 switch ((int)options[opt_idx].indir)
2556 {
2557#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002558 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002559#endif
2560#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002561# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002562 case PV_FDE: return &curwin->w_p_fde_flags;
2563 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002564# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002565# ifdef FEAT_BEVAL
2566 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2567# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002568 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002569 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002570# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002571 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002572# endif
2573#endif
2574 }
2575
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002576 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002577 return &options[opt_idx].flags;
2578}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002579#endif
2580
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002581/*
2582 * Redraw the window title and/or tab page text later.
2583 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002584void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002585{
2586 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002587 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002588}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002589
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002591 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2592 * characters or characters in "allowed".
2593 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002594 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002595valid_name(char_u *val, char *allowed)
2596{
2597 char_u *s;
2598
2599 for (s = val; *s != NUL; ++s)
2600 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2601 return FALSE;
2602 return TRUE;
2603}
2604
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002605#if defined(FEAT_EVAL) || defined(PROTO)
2606/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002607 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002608 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002609 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002610 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002611set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002612{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002613 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2614 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002615 sctx_T new_script_ctx = script_ctx;
2616
Bram Moolenaar51258742020-05-03 17:19:33 +02002617 // Modeline already has the line number set.
2618 if (!(opt_flags & OPT_MODELINE))
2619 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002620
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002621 // Remember where the option was set. For local options need to do that
2622 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002623 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002624 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002625 if (both || (opt_flags & OPT_LOCAL))
2626 {
2627 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002628 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002629 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002630 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002631 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002632 if (both)
2633 // also setting the "all buffers" value
2634 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2635 new_script_ctx;
2636 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002637 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002638}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002639
2640/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002641 * Get the script context of global option "name".
2642 *
2643 */
2644 sctx_T *
2645get_option_sctx(char *name)
2646{
2647 int idx = findoption((char_u *)name);
2648
2649 if (idx >= 0)
2650 return &options[idx].script_ctx;
2651 siemsg("no such option: %s", name);
2652 return NULL;
2653}
2654
2655/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002656 * Set the script_ctx for a termcap option.
2657 * "name" must be the two character code, e.g. "RV".
2658 * When "name" is NULL use "opt_idx".
2659 */
2660 void
2661set_term_option_sctx_idx(char *name, int opt_idx)
2662{
2663 char_u buf[5];
2664 int idx;
2665
2666 if (name == NULL)
2667 idx = opt_idx;
2668 else
2669 {
2670 buf[0] = 't';
2671 buf[1] = '_';
2672 buf[2] = name[0];
2673 buf[3] = name[1];
2674 buf[4] = 0;
2675 idx = findoption(buf);
2676 }
2677 if (idx >= 0)
2678 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2679}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002680#endif
2681
Bram Moolenaarf4140482020-02-15 23:06:45 +01002682#if defined(FEAT_EVAL)
2683/*
2684 * Apply the OptionSet autocommand.
2685 */
2686 static void
2687apply_optionset_autocmd(
2688 int opt_idx,
2689 long opt_flags,
2690 long oldval,
2691 long oldval_g,
2692 long newval,
2693 char *errmsg)
2694{
2695 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2696
2697 // Don't do this while starting up, failure or recursively.
2698 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2699 return;
2700
2701 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2702 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2703 oldval_g);
2704 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2705 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2706 (opt_flags & OPT_LOCAL) ? "local" : "global");
2707 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2708 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2709 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2710 if (opt_flags & OPT_LOCAL)
2711 {
2712 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2713 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2714 }
2715 if (opt_flags & OPT_GLOBAL)
2716 {
2717 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2718 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2719 }
2720 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2721 {
2722 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2723 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2724 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2725 }
2726 if (opt_flags & OPT_MODELINE)
2727 {
2728 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2729 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2730 }
2731 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2732 NULL, FALSE, NULL);
2733 reset_v_option_vars();
2734}
2735#endif
2736
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737/*
2738 * Set the value of a boolean option, and take care of side effects.
2739 * Returns NULL for success, or an error message for an error.
2740 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002741 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002742set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 int opt_idx, // index in options[] table
2744 char_u *varp, // pointer to the option variable
2745 int value, // new value
2746 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747{
2748 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002749#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002750 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002751#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002752 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 if ((secure
2756#ifdef HAVE_SANDBOX
2757 || sandbox != 0
2758#endif
2759 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002760 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002762#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002763 // Save the global value before changing anything. This is needed as for
2764 // a global-only option setting the "local value" in fact sets the global
2765 // value (since there is only one value).
2766 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2767 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2768 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002769#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002774 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775#endif
2776
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002777#ifdef FEAT_GUI
2778 need_mouse_correct = TRUE;
2779#endif
2780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002781 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2783 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2784
2785 /*
2786 * Handle side effects of changing a bool option.
2787 */
2788
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002789 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
Bram Moolenaar920694c2016-08-21 17:45:02 +02002793#ifdef FEAT_LANGMAP
2794 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002795 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002796 p_lnr = !p_lrm;
2797 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002798 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002799 p_lrm = !p_lnr;
2800#endif
2801
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002802#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002803 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002804 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2805 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002806 // Only take action when the option was set. When reset we do not
2807 // delete the undo file, the option may be set again without making
2808 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002809 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002810 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002811 char_u hash[UNDO_HASH_SIZE];
2812 buf_T *save_curbuf = curbuf;
2813
Bram Moolenaar29323592016-07-24 22:04:11 +02002814 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002815 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002816 // When 'undofile' is set globally: for every buffer, otherwise
2817 // only for the current buffer: Try to read in the undofile,
2818 // if one exists, the buffer wasn't changed and the buffer was
2819 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002820 if ((curbuf == save_curbuf
2821 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2822 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2823 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002824#ifdef FEAT_CRYPT
2825 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2826 continue;
2827#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002828 u_compute_hash(hash);
2829 u_read_undo(NULL, hash, curbuf->b_fname);
2830 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002831 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002832 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002833 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002834 }
2835#endif
2836
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 else if ((int *)varp == &curbuf->b_p_ro)
2838 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002839 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2841 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002843 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002844 if (curbuf->b_p_ro)
2845 curbuf->b_did_warn = FALSE;
2846
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002847 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
2849
Bram Moolenaar9c449722010-07-20 18:44:27 +02002850#ifdef FEAT_GUI
2851 else if ((int *)varp == &p_mh)
2852 {
2853 if (!p_mh)
2854 gui_mch_mousehide(FALSE);
2855 }
2856#endif
2857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002860 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002861# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002862 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002863 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2864 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002865 {
2866 curbuf->b_p_ma = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002867 return e_cannot_make_terminal_with_running_job_modifiable;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002868 }
2869# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002870 redraw_titles();
2871 }
K.Takata845bbb72022-11-05 18:31:19 +00002872 // redraw the window title and tab page text when 'endoffile', 'endofline',
2873 // 'fixeol' or 'bomb' is changed
2874 else if ((int *)varp == &curbuf->b_p_eof
2875 || (int *)varp == &curbuf->b_p_eol
2876 || (int *)varp == &curbuf->b_p_fixeol
2877 || (int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002878 {
2879 redraw_titles();
2880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002882 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 else if ((int *)varp == &curbuf->b_p_bin)
2884 {
2885 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002886 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
2888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2891 {
2892 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2893 NULL, NULL, TRUE, curbuf);
2894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002896 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 else if ((int *)varp == &curbuf->b_p_swf)
2898 {
2899 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002900 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002902 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2903 // buf->b_p_swf
2904 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002907 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 else if ((int *)varp == &p_terse)
2909 {
2910 char_u *p;
2911
2912 p = vim_strchr(p_shm, SHM_SEARCH);
2913
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002914 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 if (p_terse && p == NULL)
2916 {
2917 STRCPY(IObuff, p_shm);
2918 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002919 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002921 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002923 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 }
2925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 else if ((int *)varp == &p_paste)
2928 {
2929 paste_option_changed();
2930 }
2931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 else if ((int *)varp == &p_im)
2934 {
2935 if (p_im)
2936 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002937 if ((State & MODE_INSERT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 need_start_insertmode = TRUE;
2939 stop_insert_mode = FALSE;
2940 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002942 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 {
2944 need_start_insertmode = FALSE;
2945 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002946 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002947 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 restart_edit = 0;
2949 }
2950 }
2951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 else if ((int *)varp == &p_ic && p_hls)
2954 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002955 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 }
2957
2958#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002959 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 else if ((int *)varp == &p_hls)
2961 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002962 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
2964#endif
2965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002966 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2967 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 else if ((int *)varp == &curwin->w_p_scb)
2969 {
2970 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002971 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002973 curwin->w_scbind_pos = curwin->w_topline;
2974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976
Bram Moolenaar4033c552017-09-16 20:54:51 +02002977#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002978 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 else if ((int *)varp == &curwin->w_p_pvw)
2980 {
2981 if (curwin->w_p_pvw)
2982 {
2983 win_T *win;
2984
Bram Moolenaar29323592016-07-24 22:04:11 +02002985 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 if (win->w_p_pvw && win != curwin)
2987 {
2988 curwin->w_p_pvw = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002989 return e_preview_window_already_exists;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 }
2991 }
2992 }
2993#endif
2994
Bram Moolenaarf6196f42022-10-02 21:29:55 +01002995 else if ((int *)varp == &curwin->w_p_sms)
2996 {
2997 if (!curwin->w_p_sms)
2998 {
2999 curwin->w_skipcol = 0;
3000 changed_line_abv_curs();
3001 }
3002 }
3003
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003004 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 else if ((int *)varp == &curbuf->b_p_tx)
3006 {
3007 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
3008 }
3009
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003010 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 set_string_option_direct((char_u *)"ffs", -1,
3014 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003015 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017
3018 /*
3019 * When 'lisp' option changes include/exclude '-' in
3020 * keyword characters.
3021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 else if (varp == (char_u *)&(curbuf->b_p_lisp))
3023 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003024 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003027 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02003028 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003030 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
3033 else if ((int *)varp == &curbuf->b_changed)
3034 {
3035 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003036 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003037 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 }
3040
3041#ifdef BACKSLASH_IN_FILENAME
3042 else if ((int *)varp == &p_ssl)
3043 {
3044 if (p_ssl)
3045 {
3046 psepc = '/';
3047 psepcN = '\\';
3048 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
3050 else
3051 {
3052 psepc = '\\';
3053 psepcN = '/';
3054 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 }
3056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003057 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 buflist_slash_adjust();
3059 alist_slash_adjust();
3060# ifdef FEAT_EVAL
3061 scriptnames_slash_adjust();
3062# endif
3063 }
3064#endif
3065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003066 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 else if ((int *)varp == &curwin->w_p_wrap)
3068 {
3069 if (curwin->w_p_wrap)
3070 curwin->w_leftcol = 0;
3071 }
3072
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 else if ((int *)varp == &p_ea)
3074 {
3075 if (p_ea && !old_value)
3076 win_equal(curwin, FALSE, 0);
3077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
3079 else if ((int *)varp == &p_wiv)
3080 {
3081 /*
3082 * When 'weirdinvert' changed, set/reset 't_xs'.
3083 * Then set 'weirdinvert' according to value of 't_xs'.
3084 */
3085 if (p_wiv && !old_value)
3086 T_XS = (char_u *)"y";
3087 else if (!p_wiv && old_value)
3088 T_XS = empty_option;
3089 p_wiv = (*T_XS != NUL);
3090 }
3091
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003092#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 else if ((int *)varp == &p_beval)
3094 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003095 if (!balloonEvalForTerm)
3096 {
3097 if (p_beval && !old_value)
3098 gui_mch_enable_beval_area(balloonEval);
3099 else if (!p_beval && old_value)
3100 gui_mch_disable_beval_area(balloonEval);
3101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003103#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003104#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003105 else if ((int *)varp == &p_bevalterm)
3106 {
3107 mch_bevalterm_changed();
3108 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003111#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 else if ((int *)varp == &p_acd)
3113 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003114 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003115 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
3117#endif
3118
3119#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003120 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 else if ((int *)varp == &curwin->w_p_diff)
3122 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003123 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003124 diff_buf_adjust(curwin);
3125# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (foldmethodIsDiff(curwin))
3127 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 }
3130#endif
3131
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003132#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003133 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 else if ((int *)varp == &p_imdisable)
3135 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003136 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 if (p_imdisable)
3138 im_set_active(FALSE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003139 else if (State & MODE_INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003140 // When the option is set from an autocommand, it may need to take
3141 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003142 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
3144#endif
3145
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003146#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003147 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003148 else if ((int *)varp == &curwin->w_p_spell)
3149 {
3150 if (curwin->w_p_spell)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003151 errmsg = did_set_spelllang(curwin);
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003152 }
3153#endif
3154
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#ifdef FEAT_ARABIC
3156 if ((int *)varp == &curwin->w_p_arab)
3157 {
3158 if (curwin->w_p_arab)
3159 {
3160 /*
3161 * 'arabic' is set, handle various sub-settings.
3162 */
3163 if (!p_tbidi)
3164 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003165 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (!curwin->w_p_rl)
3167 {
3168 curwin->w_p_rl = TRUE;
3169 changed_window_setting();
3170 }
3171
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003172 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 if (!p_arshape)
3174 {
3175 p_arshape = TRUE;
3176 redraw_later_clear();
3177 }
3178 }
3179
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003180 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003181 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003183 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003184 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3185
Bram Moolenaar8820b482017-03-16 17:23:31 +01003186 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003187 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003188#ifdef FEAT_EVAL
3189 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3190#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003193 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003197 // Force-set the necessary keymap for arabic
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003198 errmsg = set_option_value((char_u *)"keymap",
3199 0L, (char_u *)"arabic", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 }
3202 else
3203 {
3204 /*
3205 * 'arabic' is reset, handle various sub-settings.
3206 */
3207 if (!p_tbidi)
3208 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003209 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 if (curwin->w_p_rl)
3211 {
3212 curwin->w_p_rl = FALSE;
3213 changed_window_setting();
3214 }
3215
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003216 // 'arabicshape' isn't reset, it is a global option and
3217 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 }
3219
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003220 // 'delcombine' isn't reset, it is a global option and another
3221 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003224 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 curbuf->b_p_iminsert = B_IMODE_NONE;
3226 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3227# endif
3228 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003229 }
3230
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003231#endif
3232
Bram Moolenaar44826212019-08-22 21:23:20 +02003233#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3234 else if (((int *)varp == &curwin->w_p_nu
3235 || (int *)varp == &curwin->w_p_rnu)
3236 && gui.in_use
3237 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3238 && curbuf->b_signlist != NULL)
3239 {
3240 // If the 'number' or 'relativenumber' options are modified and
3241 // 'signcolumn' is set to 'number', then clear the screen for a full
3242 // refresh. Otherwise the sign icons are not displayed properly in the
3243 // number column. If the 'number' option is set and only the
3244 // 'relativenumber' option is toggled, then don't refresh the screen
3245 // (optimization).
3246 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003247 redraw_all_later(UPD_CLEAR);
Bram Moolenaar44826212019-08-22 21:23:20 +02003248 }
3249#endif
3250
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003251#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003252 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003253 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003254 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003255# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003256 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003257 if (
3258# ifdef VIMDLL
3259 !gui.in_use && !gui.starting &&
3260# endif
3261 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003262 {
3263 p_tgc = 0;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003264 return e_24_bit_colors_are_not_supported_on_this_environment;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003265 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003266 if (is_term_win32())
3267 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003268# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003269# ifdef FEAT_GUI
3270 if (!gui.in_use && !gui.starting)
3271# endif
3272 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003273# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003274 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003275 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003276 {
3277 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003278 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003279 init_highlight(TRUE, FALSE);
3280 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003281# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003282# ifdef FEAT_TERMINAL
3283 term_update_colors_all();
LemonBoyb2b3acb2022-05-20 10:10:34 +01003284 term_update_palette_all();
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003285 term_update_wincolor_all();
3286# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003287 }
3288#endif
3289
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 /*
3291 * End of handling side effects for bool options.
3292 */
3293
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003294 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003295
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 options[opt_idx].flags |= P_WAS_SET;
3297
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003298#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003299 apply_optionset_autocmd(opt_idx, opt_flags,
3300 (long)(old_value ? TRUE : FALSE),
3301 (long)(old_global_value ? TRUE : FALSE),
3302 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003303#endif
3304
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003305 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003306 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003307 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003308 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003309
3310 if ((opt_flags & OPT_NO_REDRAW) == 0)
3311 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003313 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314}
3315
3316/*
3317 * Set the value of a number option, and take care of side effects.
3318 * Returns NULL for success, or an error message for an error.
3319 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003320 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003321set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003322 int opt_idx, // index in options[] table
3323 char_u *varp, // pointer to the option variable
3324 long value, // new value
3325 char *errbuf, // buffer for error messages
3326 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003327 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3328 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003330 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003332#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003333 long old_global_value = 0; // only used when setting a local and
3334 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003335#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003336 long old_Rows = Rows; // remember old Rows
3337 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 long *pp = (long *)varp;
3339
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003340 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003341 if ((secure
3342#ifdef HAVE_SANDBOX
3343 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003345 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003346 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003348#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003349 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003350 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003351 // value (since there is only one value).
3352 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003353 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3354 OPT_GLOBAL);
3355#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003356
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 *pp = value;
3358#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003359 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003360 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003362#ifdef FEAT_GUI
3363 need_mouse_correct = TRUE;
3364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
Bram Moolenaar14f24742012-08-08 18:01:05 +02003366 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003368 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003369#ifdef FEAT_VARTABS
3370 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3371 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003372 ? tabstop_first(curbuf->b_p_vts_array)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003373 : curbuf->b_p_ts;
3374#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
3378
3379 /*
3380 * Number options that need some action when changed
3381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (pp == &p_wh || pp == &p_hh)
3383 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003384 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 if (p_wh < 1)
3386 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003387 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 p_wh = 1;
3389 }
3390 if (p_wmh > p_wh)
3391 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003392 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 p_wh = p_wmh;
3394 }
3395 if (p_hh < 0)
3396 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003397 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 p_hh = 0;
3399 }
3400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003401 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003402 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
3404 if (pp == &p_wh && curwin->w_height < p_wh)
3405 win_setheight((int)p_wh);
3406 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3407 win_setheight((int)p_hh);
3408 }
3409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 else if (pp == &p_wmh)
3411 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003412 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 if (p_wmh < 0)
3414 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003415 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 p_wmh = 0;
3417 }
3418 if (p_wmh > p_wh)
3419 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003420 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 p_wmh = p_wh;
3422 }
3423 win_setminheight();
3424 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003425 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003427 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 if (p_wiw < 1)
3429 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003430 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 p_wiw = 1;
3432 }
3433 if (p_wmw > p_wiw)
3434 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003435 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 p_wiw = p_wmw;
3437 }
3438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003439 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003440 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 win_setwidth((int)p_wiw);
3442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 else if (pp == &p_wmw)
3444 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003445 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (p_wmw < 0)
3447 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003448 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 p_wmw = 0;
3450 }
3451 if (p_wmw > p_wiw)
3452 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003453 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 p_wmw = p_wiw;
3455 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003456 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003459 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 else if (pp == &p_ls)
3461 {
3462 last_status(FALSE);
3463 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003464
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003465 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003466 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003467 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003468 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
3471#ifdef FEAT_GUI
3472 else if (pp == &p_linespace)
3473 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003474 // Recompute gui.char_height and resize the Vim window to keep the
3475 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003476 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003477 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
3479#endif
3480
3481#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003482 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 else if (pp == &curwin->w_p_fdl)
3484 {
3485 if (curwin->w_p_fdl < 0)
3486 curwin->w_p_fdl = 0;
3487 newFoldLevel();
3488 }
3489
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003490 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 else if (pp == &curwin->w_p_fml)
3492 {
3493 foldUpdateAll(curwin);
3494 }
3495
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003496 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 else if (pp == &curwin->w_p_fdn)
3498 {
3499 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3500 foldUpdateAll(curwin);
3501 }
3502
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003503 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 else if (pp == &curwin->w_p_fdc)
3505 {
3506 if (curwin->w_p_fdc < 0)
3507 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003508 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 curwin->w_p_fdc = 0;
3510 }
3511 else if (curwin->w_p_fdc > 12)
3512 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003513 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 curwin->w_p_fdc = 12;
3515 }
3516 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003517#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003519 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3521 {
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003522#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (foldmethodIsIndent(curwin))
3524 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003525#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003526 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3527 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003528 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3529 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003532 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003533 else if (pp == &p_mco)
3534 {
3535 if (p_mco > MAX_MCO)
3536 p_mco = MAX_MCO;
3537 else if (p_mco < 0)
3538 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003539 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003540 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003541
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 else if (pp == &curbuf->b_p_iminsert)
3543 {
3544 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3545 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003546 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 curbuf->b_p_iminsert = B_IMODE_NONE;
3548 }
3549 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003550 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003552#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003553 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 status_redraw_curbuf();
3555#endif
3556 }
3557
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003558#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003559 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003560 else if (pp == &p_imst)
3561 {
3562 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003563 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003564 }
3565#endif
3566
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003567 else if (pp == &p_window)
3568 {
3569 if (p_window < 1)
3570 p_window = 1;
3571 else if (p_window >= Rows)
3572 p_window = Rows - 1;
3573 }
3574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 else if (pp == &curbuf->b_p_imsearch)
3576 {
3577 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3578 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003579 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 curbuf->b_p_imsearch = B_IMODE_NONE;
3581 }
3582 p_imsearch = curbuf->b_p_imsearch;
3583 }
3584
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003585 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 else if (pp == &p_titlelen)
3587 {
3588 if (p_titlelen < 0)
3589 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003590 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 p_titlelen = 85;
3592 }
3593 if (starting != NO_SCREEN && old_value != p_titlelen)
3594 need_maketitle = TRUE;
3595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003597 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 else if (pp == &p_ch)
3599 {
Bram Moolenaara2a89732022-08-31 14:46:18 +01003600 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003602 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 p_ch = 1;
3604 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003605 if (p_ch > Rows - min_rows() + 1)
3606 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003608 // Only compute the new window layout when startup has been
3609 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaarc9f5f732022-10-06 11:39:06 +01003610 if ((p_ch != old_value
3611 || tabline_height() + topframe->fr_height != Rows - p_ch)
Bram Moolenaar0816f472022-10-05 15:42:32 +01003612 && full_screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613#ifdef FEAT_GUI
3614 && !gui.starting
3615#endif
Bram Moolenaar0816f472022-10-05 15:42:32 +01003616 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003617 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003620 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 else if (pp == &p_uc)
3622 {
3623 if (p_uc < 0)
3624 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003625 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 p_uc = 100;
3627 }
3628 if (p_uc && !old_value)
3629 ml_open_files();
3630 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003631#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003632 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003633 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003634 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003635 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003636 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003637 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003638 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003639 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003640 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003641 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003642 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003643 }
3644 }
3645#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003646#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003647 else if (pp == &p_mzq)
3648 mzvim_reset_timer();
3649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003651#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003652 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003653 else if (pp == &p_pyx)
3654 {
3655 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003656 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003657 }
3658#endif
3659
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003660 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 else if (pp == &p_ul)
3662 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003663 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003665 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 p_ul = value;
3667 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003668 else if (pp == &curbuf->b_p_ul)
3669 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003670 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003671 curbuf->b_p_ul = old_value;
3672 u_sync(TRUE);
3673 curbuf->b_p_ul = value;
3674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003676#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003677 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003678 else if (pp == &curwin->w_p_nuw)
3679 {
3680 if (curwin->w_p_nuw < 1)
3681 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003682 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003683 curwin->w_p_nuw = 1;
3684 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003685 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003686 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003687 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003688 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003689 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003690 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003691 }
3692#endif
3693
Bram Moolenaar1a384422010-07-14 19:53:30 +02003694 else if (pp == &curbuf->b_p_tw)
3695 {
3696 if (curbuf->b_p_tw < 0)
3697 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003698 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003699 curbuf->b_p_tw = 0;
3700 }
3701#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003702 {
3703 win_T *wp;
3704 tabpage_T *tp;
3705
3706 FOR_ALL_TAB_WINDOWS(tp, wp)
3707 check_colorcolumn(wp);
3708 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003709#endif
3710 }
3711
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 /*
3713 * Check the bounds for numeric options here
3714 */
3715 if (Rows < min_rows() && full_screen)
3716 {
3717 if (errbuf != NULL)
3718 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003719 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003720 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 errmsg = errbuf;
3722 }
3723 Rows = min_rows();
3724 }
3725 if (Columns < MIN_COLUMNS && full_screen)
3726 {
3727 if (errbuf != NULL)
3728 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003729 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003730 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 errmsg = errbuf;
3732 }
3733 Columns = MIN_COLUMNS;
3734 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003735 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 /*
3738 * If the screen (shell) height has been changed, assume it is the
3739 * physical screenheight.
3740 */
3741 if (old_Rows != Rows || old_Columns != Columns)
3742 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003743 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if (updating_screen)
3745 *pp = old_value;
3746 else if (full_screen
3747#ifdef FEAT_GUI
3748 && !gui.starting
3749#endif
3750 )
3751 set_shellsize((int)Columns, (int)Rows, TRUE);
3752 else
3753 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003754 // Postpone the resizing; check the size and cmdline position for
3755 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 check_shellsize();
3757 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3758 cmdline_row = Rows - p_ch;
3759 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003760 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003761 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 if (curbuf->b_p_ts <= 0)
3765 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003766 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 curbuf->b_p_ts = 8;
3768 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003769 else if (curbuf->b_p_ts > TABSTOP_MAX)
3770 {
3771 errmsg = e_invalid_argument;
3772 curbuf->b_p_ts = 8;
3773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 if (p_tm < 0)
3775 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003776 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 p_tm = 0;
3778 }
3779 if ((curwin->w_p_scr <= 0
3780 || (curwin->w_p_scr > curwin->w_height
3781 && curwin->w_height > 0))
3782 && full_screen)
3783 {
3784 if (pp == &(curwin->w_p_scr))
3785 {
3786 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003787 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 win_comp_scroll(curwin);
3789 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003790 // If 'scroll' became invalid because of a side effect silently adjust
3791 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 else if (curwin->w_p_scr <= 0)
3793 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003794 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 curwin->w_p_scr = curwin->w_height;
3796 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003797 if (p_hi < 0)
3798 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003799 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003800 p_hi = 0;
3801 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003802 else if (p_hi > 10000)
3803 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003804 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003805 p_hi = 10000;
3806 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003807 if (p_re < 0 || p_re > 2)
3808 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003809 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003810 p_re = 0;
3811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (p_report < 0)
3813 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003814 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 p_report = 1;
3816 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003817 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003819 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 p_sj = Rows / 2;
3821 else
3822 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003823 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 p_sj = 1;
3825 }
3826 }
3827 if (p_so < 0 && full_screen)
3828 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003829 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 p_so = 0;
3831 }
3832 if (p_siso < 0 && full_screen)
3833 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003834 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 p_siso = 0;
3836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 if (p_cwh < 1)
3838 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003839 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 p_cwh = 1;
3841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 if (p_ut < 0)
3843 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003844 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 p_ut = 2000;
3846 }
3847 if (p_ss < 0)
3848 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003849 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 p_ss = 0;
3851 }
3852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003853 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3855 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3856
3857 options[opt_idx].flags |= P_WAS_SET;
3858
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003859#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003860 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3861 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003862#endif
3863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003864 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003865 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003866 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003867 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003868 if ((opt_flags & OPT_NO_REDRAW) == 0)
3869 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870
3871 return errmsg;
3872}
3873
3874/*
3875 * Called after an option changed: check if something needs to be redrawn.
3876 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003877 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003878check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003880 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003881 int doclear = (flags & P_RCLR) == P_RCLR;
3882 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003884 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886
3887 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3888 changed_window_setting();
3889 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003890 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003891 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003892 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003893 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003894 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003896 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897}
3898
3899/*
3900 * Find index for option 'arg'.
3901 * Return -1 if not found.
3902 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003903 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003904findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905{
3906 int opt_idx;
3907 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003908 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 int is_term_opt;
3910
3911 /*
3912 * For first call: Initialize the quick-access table.
3913 * It contains the index for the first option that starts with a certain
3914 * letter. There are 26 letters, plus the first "t_" option.
3915 */
3916 if (quick_tab[1] == 0)
3917 {
3918 p = options[0].fullname;
3919 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3920 {
3921 if (s[0] != p[0])
3922 {
3923 if (s[0] == 't' && s[1] == '_')
3924 quick_tab[26] = opt_idx;
3925 else
3926 quick_tab[CharOrdLow(s[0])] = opt_idx;
3927 }
3928 p = s;
3929 }
3930 }
3931
3932 /*
3933 * Check for name starting with an illegal character.
3934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 return -1;
3937
3938 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3939 if (is_term_opt)
3940 opt_idx = quick_tab[26];
3941 else
3942 opt_idx = quick_tab[CharOrdLow(arg[0])];
3943 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3944 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003945 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 break;
3947 }
3948 if (s == NULL && !is_term_opt)
3949 {
3950 opt_idx = quick_tab[CharOrdLow(arg[0])];
3951 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3952 {
3953 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003954 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 break;
3956 s = NULL;
3957 }
3958 }
3959 if (s == NULL)
3960 opt_idx = -1;
3961 return opt_idx;
3962}
3963
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00003964#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
3965 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966/*
3967 * Get the value for an option.
3968 *
3969 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003970 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003971 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003972 * String option: gov_string, *stringval gets allocated string.
3973 * Hidden Number option: gov_hidden_number.
3974 * Hidden Toggle option: gov_hidden_bool.
3975 * Hidden String option: gov_hidden_string.
3976 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003977 *
3978 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003980 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003981get_option_value(
3982 char_u *name,
3983 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003984 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003985 int *flagsp,
3986 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987{
3988 int opt_idx;
3989 char_u *varp;
3990
3991 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003992 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003993 {
3994 int key;
3995
3996 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003997 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003998 {
3999 char_u key_name[2];
4000 char_u *p;
4001
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004002 if (flagsp != NULL)
4003 *flagsp = 0; // terminal option has no flags
4004
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004005 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004006 if (key < 0)
4007 {
4008 key_name[0] = KEY2TERMCAP0(key);
4009 key_name[1] = KEY2TERMCAP1(key);
4010 }
4011 else
4012 {
4013 key_name[0] = KS_KEY;
4014 key_name[1] = (key & 0xff);
4015 }
4016 p = find_termcode(key_name);
4017 if (p != NULL)
4018 {
4019 if (stringval != NULL)
4020 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004021 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004022 }
4023 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004024 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004027 varp = get_varp_scope(&(options[opt_idx]), scope);
4028
4029 if (flagsp != NULL)
4030 // Return the P_xxxx option flags.
4031 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
4033 if (options[opt_idx].flags & P_STRING)
4034 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004035 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004036 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 if (stringval != NULL)
4038 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004039 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004040 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4041 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004043 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004044 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004045 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004048 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 *stringval = vim_strsave(*(char_u **)(varp));
4050 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004051 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
4053
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004054 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004055 return (options[opt_idx].flags & P_NUM)
4056 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 if (options[opt_idx].flags & P_NUM)
4058 *numval = *(long *)varp;
4059 else
4060 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004061 // Special case: 'modified' is b_changed, but we also want to consider
4062 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 if ((int *)varp == &curbuf->b_changed)
4064 *numval = curbufIsChanged();
4065 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004066 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004068 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069}
4070#endif
4071
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004072#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004073/*
4074 * Returns the option attributes and its value. Unlike the above function it
4075 * will return either global value or local value of the option depending on
4076 * what was requested, but it will never return global value if it was
4077 * requested to return local one and vice versa. Neither it will return
4078 * buffer-local value if it was requested to return window-local one.
4079 *
4080 * Pretends that option is absent if it is not present in the requested scope
4081 * (i.e. has no global, window-local or buffer-local value depending on
4082 * opt_type). Uses
4083 *
4084 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004085 * 0 hidden or unknown option, also option that does not have requested
4086 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004087 * see SOPT_* in vim.h for other flags
4088 *
4089 * Possible opt_type values: see SREQ_* in vim.h
4090 */
4091 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004092get_option_value_strict(
4093 char_u *name,
4094 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004095 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004096 int opt_type,
4097 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004098{
4099 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004100 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004101 struct vimoption *p;
4102 int r = 0;
4103
4104 opt_idx = findoption(name);
4105 if (opt_idx < 0)
4106 return 0;
4107
4108 p = &(options[opt_idx]);
4109
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004110 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004111 if (p->var == NULL)
4112 return 0;
4113
4114 if (p->flags & P_BOOL)
4115 r |= SOPT_BOOL;
4116 else if (p->flags & P_NUM)
4117 r |= SOPT_NUM;
4118 else if (p->flags & P_STRING)
4119 r |= SOPT_STRING;
4120
4121 if (p->indir == PV_NONE)
4122 {
4123 if (opt_type == SREQ_GLOBAL)
4124 r |= SOPT_GLOBAL;
4125 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004126 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004127 }
4128 else
4129 {
4130 if (p->indir & PV_BOTH)
4131 r |= SOPT_GLOBAL;
4132 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004133 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004134
4135 if (p->indir & PV_WIN)
4136 {
4137 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004138 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004139 else
4140 r |= SOPT_WIN;
4141 }
4142 else if (p->indir & PV_BUF)
4143 {
4144 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004145 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004146 else
4147 r |= SOPT_BUF;
4148 }
4149 }
4150
4151 if (stringval == NULL)
4152 return r;
4153
4154 if (opt_type == SREQ_GLOBAL)
4155 varp = p->var;
4156 else
4157 {
4158 if (opt_type == SREQ_BUF)
4159 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004160 // Special case: 'modified' is b_changed, but we also want to
4161 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004162 if (p->indir == PV_MOD)
4163 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004164 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004165 varp = NULL;
4166 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004167# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004168 else if (p->indir == PV_KEY)
4169 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004170 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004171 *stringval = NULL;
4172 varp = NULL;
4173 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004174# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004175 else
4176 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004177 buf_T *save_curbuf = curbuf;
4178
4179 // only getting a pointer, no need to use aucmd_prepbuf()
4180 curbuf = (buf_T *)from;
4181 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004182 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004183 curbuf = save_curbuf;
4184 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004185 }
4186 }
4187 else if (opt_type == SREQ_WIN)
4188 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004189 win_T *save_curwin = curwin;
4190
4191 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004192 curbuf = curwin->w_buffer;
4193 varp = get_varp(p);
4194 curwin = save_curwin;
4195 curbuf = curwin->w_buffer;
4196 }
4197 if (varp == p->var)
4198 return (r | SOPT_UNSET);
4199 }
4200
4201 if (varp != NULL)
4202 {
4203 if (p->flags & P_STRING)
4204 *stringval = vim_strsave(*(char_u **)(varp));
4205 else if (p->flags & P_NUM)
4206 *numval = *(long *) varp;
4207 else
4208 *numval = *(int *)varp;
4209 }
4210
4211 return r;
4212}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004213
4214/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004215 * Iterate over options. First argument is a pointer to a pointer to a
4216 * structure inside options[] array, second is option type like in the above
4217 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004218 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004219 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004220 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004221 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004222 * first argument to NULL.
4223 *
4224 * Returns full option name for current option on each call.
4225 */
4226 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004227option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004228{
4229 struct vimoption *ret = NULL;
4230 do
4231 {
4232 if (*option == NULL)
4233 *option = (void *) options;
4234 else if (((struct vimoption *) (*option))->fullname == NULL)
4235 {
4236 *option = NULL;
4237 return NULL;
4238 }
4239 else
4240 *option = (void *) (((struct vimoption *) (*option)) + 1);
4241
4242 ret = ((struct vimoption *) (*option));
4243
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004244 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004245 if (ret->var == NULL)
4246 {
4247 ret = NULL;
4248 continue;
4249 }
4250
4251 switch (opt_type)
4252 {
4253 case SREQ_GLOBAL:
4254 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4255 ret = NULL;
4256 break;
4257 case SREQ_BUF:
4258 if (!(ret->indir & PV_BUF))
4259 ret = NULL;
4260 break;
4261 case SREQ_WIN:
4262 if (!(ret->indir & PV_WIN))
4263 ret = NULL;
4264 break;
4265 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004266 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004267 return NULL;
4268 }
4269 }
4270 while (ret == NULL);
4271
4272 return (char_u *)ret->fullname;
4273}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004274#endif
4275
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004277 * Return the flags for the option at 'opt_idx'.
4278 */
4279 long_u
4280get_option_flags(int opt_idx)
4281{
4282 return options[opt_idx].flags;
4283}
4284
4285/*
4286 * Set a flag for the option at 'opt_idx'.
4287 */
4288 void
4289set_option_flag(int opt_idx, long_u flag)
4290{
4291 options[opt_idx].flags |= flag;
4292}
4293
4294/*
4295 * Clear a flag for the option at 'opt_idx'.
4296 */
4297 void
4298clear_option_flag(int opt_idx, long_u flag)
4299{
4300 options[opt_idx].flags &= ~flag;
4301}
4302
4303/*
4304 * Returns TRUE if the option at 'opt_idx' is a global option
4305 */
4306 int
4307is_global_option(int opt_idx)
4308{
4309 return options[opt_idx].indir == PV_NONE;
4310}
4311
4312/*
4313 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4314 * local value.
4315 */
4316 int
4317is_global_local_option(int opt_idx)
4318{
4319 return options[opt_idx].indir & PV_BOTH;
4320}
4321
4322/*
4323 * Returns TRUE if the option at 'opt_idx' is a window-local option
4324 */
4325 int
4326is_window_local_option(int opt_idx)
4327{
4328 return options[opt_idx].var == VAR_WIN;
4329}
4330
4331/*
4332 * Returns TRUE if the option at 'opt_idx' is a hidden option
4333 */
4334 int
4335is_hidden_option(int opt_idx)
4336{
4337 return options[opt_idx].var == NULL;
4338}
4339
4340#if defined(FEAT_CRYPT) || defined(PROTO)
4341/*
4342 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4343 */
4344 int
4345is_crypt_key_option(int opt_idx)
4346{
4347 return options[opt_idx].indir == PV_KEY;
4348}
4349#endif
4350
4351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 * Set the value of option "name".
4353 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004354 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004355 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004357 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004358set_option_value(
4359 char_u *name,
4360 long number,
4361 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004362 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363{
4364 int opt_idx;
4365 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004366 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
4368 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004369 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004370 {
4371 int key;
4372
4373 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004374 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004375 {
4376 char_u key_name[2];
4377
4378 if (key < 0)
4379 {
4380 key_name[0] = KEY2TERMCAP0(key);
4381 key_name[1] = KEY2TERMCAP1(key);
4382 }
4383 else
4384 {
4385 key_name[0] = KS_KEY;
4386 key_name[1] = (key & 0xff);
4387 }
4388 add_termcode(key_name, string, FALSE);
4389 if (full_screen)
4390 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004391 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01004392 return NULL;
4393 }
4394
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004395 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 else
4398 {
4399 flags = options[opt_idx].flags;
4400#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004401 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004403 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004404 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004405 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004408 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004409 return set_string_option(opt_idx, string, opt_flags);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004410
4411 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
4412 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004414 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004416 int idx;
4417
4418 // Either we are given a string or we are setting option
4419 // to zero.
4420 for (idx = 0; string[idx] == '0'; ++idx)
4421 ;
4422 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004423 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004424 // There's another character after zeros or the string
4425 // is empty. In both cases, we are trying to set a
4426 // num option using a string.
4427 semsg(_(e_number_required_after_str_equal_str),
4428 name, string);
4429 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004430
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004433 if (flags & P_NUM)
4434 return set_num_option(opt_idx, varp, number,
4435 NULL, 0, opt_flags);
4436 else
4437 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004441 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442}
4443
4444/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004445 * Call set_option_value() and when an error is returned report it.
4446 */
4447 void
4448set_option_value_give_err(
4449 char_u *name,
4450 long number,
4451 char_u *string,
4452 int opt_flags) // OPT_LOCAL or 0 (both)
4453{
4454 char *errmsg = set_option_value(name, number, string, opt_flags);
4455
4456 if (errmsg != NULL)
4457 emsg(_(errmsg));
4458}
4459
4460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 * Get the terminal code for a terminal option.
4462 * Returns NULL when not found.
4463 */
4464 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004465get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466{
4467 int opt_idx;
4468 char_u *varp;
4469
4470 if (tname[0] != 't' || tname[1] != '_' ||
4471 tname[2] == NUL || tname[3] == NUL)
4472 return NULL;
4473 if ((opt_idx = findoption(tname)) >= 0)
4474 {
4475 varp = get_varp(&(options[opt_idx]));
4476 if (varp != NULL)
4477 varp = *(char_u **)(varp);
4478 return varp;
4479 }
4480 return find_termcode(tname + 2);
4481}
4482
4483 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004484get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485{
4486 int i;
4487
4488 i = findoption((char_u *)"hl");
4489 if (i >= 0)
4490 return options[i].def_val[VI_DEFAULT];
4491 return (char_u *)NULL;
4492}
4493
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004494 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004495get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004496{
4497 int i;
4498
4499 i = findoption((char_u *)"enc");
4500 if (i >= 0)
4501 return options[i].def_val[VI_DEFAULT];
4502 return (char_u *)NULL;
4503}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004504
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004505#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004506 int
4507is_option_allocated(char *name)
4508{
4509 int idx = findoption((char_u *)name);
4510
4511 return idx >= 0 && (options[idx].flags & P_ALLOCED);
4512}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004513#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004514
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004515#if defined(FEAT_EVAL) || defined(PROTO)
4516/*
4517 * Return TRUE if "name" is a string option.
4518 * Returns FALSE if option "name" does not exist.
4519 */
4520 int
4521is_string_option(char_u *name)
4522{
4523 int idx = findoption(name);
4524
4525 return idx >= 0 && (options[idx].flags & P_STRING);
4526}
4527#endif
4528
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529/*
4530 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004531 * When "has_lt" is true there is a '<' before "*arg_arg".
4532 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 */
4534 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004535find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004537 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004539 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540
4541 /*
4542 * Don't use get_special_key_code() for t_xx, we don't want it to call
4543 * add_termcap_entry().
4544 */
4545 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4546 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004547 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004549 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004551 key = find_special_key(&arg, &modifiers,
4552 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004553 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 key = 0;
4555 }
4556 return key;
4557}
4558
4559/*
4560 * if 'all' == 0: show changed options
4561 * if 'all' == 1: show all normal options
4562 * if 'all' == 2: show all terminal options
4563 */
4564 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004565showoptions(
4566 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004567 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568{
4569 struct vimoption *p;
4570 int col;
4571 int isterm;
4572 char_u *varp;
4573 struct vimoption **items;
4574 int item_count;
4575 int run;
4576 int row, rows;
4577 int cols;
4578 int i;
4579 int len;
4580
4581#define INC 20
4582#define GAP 3
4583
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004584 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 if (items == NULL)
4586 return;
4587
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004588 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004590 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004592 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004594 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004596 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
4598 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004599 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 * 1. display the short items
4601 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004602 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 */
4604 for (run = 1; run <= 2 && !got_int; ++run)
4605 {
4606 /*
4607 * collect the items in items[]
4608 */
4609 item_count = 0;
4610 for (p = &options[0]; p->fullname != NULL; p++)
4611 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004612 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004613 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004614 continue;
4615
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 varp = NULL;
4617 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004618 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
4620 if (p->indir != PV_NONE && !isterm)
4621 varp = get_varp_scope(p, opt_flags);
4622 }
4623 else
4624 varp = get_varp(p);
4625 if (varp != NULL
4626 && ((all == 2 && isterm)
4627 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004628 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004630 if (opt_flags & OPT_ONECOLUMN)
4631 len = Columns;
4632 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004633 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 else
4635 {
4636 option_value2string(p, opt_flags);
4637 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4638 }
4639 if ((len <= INC - GAP && run == 1) ||
4640 (len > INC - GAP && run == 2))
4641 items[item_count++] = p;
4642 }
4643 }
4644
4645 /*
4646 * display the items
4647 */
4648 if (run == 1)
4649 {
4650 cols = (Columns + GAP - 3) / INC;
4651 if (cols == 0)
4652 cols = 1;
4653 rows = (item_count + cols - 1) / cols;
4654 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004655 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 rows = item_count;
4657 for (row = 0; row < rows && !got_int; ++row)
4658 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004659 msg_putchar('\n'); // go to next line
4660 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 break;
4662 col = 0;
4663 for (i = row; i < item_count; i += rows)
4664 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004665 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 showoneopt(items[i], opt_flags);
4667 col += INC;
4668 }
4669 out_flush();
4670 ui_breakcheck();
4671 }
4672 }
4673 vim_free(items);
4674}
4675
4676/*
4677 * Return TRUE if option "p" has its default value.
4678 */
4679 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004680optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681{
4682 int dvi;
4683
4684 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004685 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004686 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004688 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004690 // the cast to long is required for Manx C, long_i is
4691 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004692 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004693 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4695}
4696
4697/*
4698 * showoneopt: show the value of one option
4699 * must not be called with a hidden option!
4700 */
4701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004702showoneopt(
4703 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004704 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004706 char_u *varp;
4707 int save_silent = silent_mode;
4708
4709 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004710 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711
4712 varp = get_varp_scope(p, opt_flags);
4713
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004714 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4716 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004717 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004719 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004721 msg_puts(" ");
4722 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 if (!(p->flags & P_BOOL))
4724 {
4725 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004726 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 option_value2string(p, opt_flags);
4728 msg_outtrans(NameBuff);
4729 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004730
4731 silent_mode = save_silent;
4732 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733}
4734
4735/*
4736 * Write modified options as ":set" commands to a file.
4737 *
4738 * There are three values for "opt_flags":
4739 * OPT_GLOBAL: Write global option values and fresh values of
4740 * buffer-local options (used for start of a session
4741 * file).
4742 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4743 * curwin (used for a vimrc file).
4744 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4745 * and local values for window-local options of
4746 * curwin. Local values are also written when at the
4747 * default value, because a modeline or autocommand
4748 * may have set them when doing ":edit file" and the
4749 * user has set them back at the default or fresh
4750 * value.
4751 * When "local_only" is TRUE, don't write fresh
4752 * values, only local values (for ":mkview").
4753 * (fresh value = value used for a new buffer or window for a local option).
4754 *
4755 * Return FAIL on error, OK otherwise.
4756 */
4757 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004758makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759{
4760 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004761 char_u *varp; // currently used value
4762 char_u *varp_fresh; // local value
4763 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 char *cmd;
4765 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004766 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767
4768 /*
4769 * The options that don't have a default (terminal name, columns, lines)
4770 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004771 * Do the loop over "options[]" twice: once for options with the
4772 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004774 for (pri = 1; pri >= 0; --pri)
4775 {
4776 for (p = &options[0]; !istermoption(p); p++)
4777 if (!(p->flags & P_NO_MKRC)
4778 && !istermoption(p)
4779 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004781 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4783 continue;
4784
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004785 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4786 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4788 continue;
4789
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004790 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004792 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 continue;
4794
Bram Moolenaard23b7142021-04-17 21:04:34 +02004795 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4796 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004797 continue;
4798
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 round = 2;
4800 if (p->indir != PV_NONE)
4801 {
4802 if (p->var == VAR_WIN)
4803 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004804 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (!(opt_flags & OPT_LOCAL))
4806 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004807 // When fresh value of window-local option is not at the
4808 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4810 {
4811 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004812 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
4814 round = 1;
4815 varp_local = varp;
4816 varp = varp_fresh;
4817 }
4818 }
4819 }
4820 }
4821
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004822 // Round 1: fresh value for window-local options.
4823 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 for ( ; round <= 2; varp = varp_local, ++round)
4825 {
4826 if (round == 1 || (opt_flags & OPT_GLOBAL))
4827 cmd = "set";
4828 else
4829 cmd = "setlocal";
4830
4831 if (p->flags & P_BOOL)
4832 {
4833 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4834 return FAIL;
4835 }
4836 else if (p->flags & P_NUM)
4837 {
4838 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4839 return FAIL;
4840 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004841 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004843 int do_endif = FALSE;
4844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004845 // Don't set 'syntax' and 'filetype' again if the value is
4846 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004847 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004848#if defined(FEAT_SYN_HL)
4849 p->indir == PV_SYN ||
4850#endif
4851 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 {
4853 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4854 *(char_u **)(varp)) < 0
4855 || put_eol(fd) < 0)
4856 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004857 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 }
4859 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004860 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004862 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
4864 if (put_line(fd, "endif") == FAIL)
4865 return FAIL;
4866 }
4867 }
4868 }
4869 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 return OK;
4872}
4873
4874#if defined(FEAT_FOLDING) || defined(PROTO)
4875/*
4876 * Generate set commands for the local fold options only. Used when
4877 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4878 */
4879 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004880makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004882 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004884 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 == FAIL
4886# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004887 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004889 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 == FAIL
4891 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4892 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4893 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4894 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4895 )
4896 return FAIL;
4897
4898 return OK;
4899}
4900#endif
4901
4902 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004903put_setstring(
4904 FILE *fd,
4905 char *cmd,
4906 char *name,
4907 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004908 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909{
4910 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004911 char_u *buf = NULL;
4912 char_u *part = NULL;
4913 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914
4915 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4916 return FAIL;
4917 if (*valuep != NULL)
4918 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004919 // Output 'pastetoggle' as key names. For other
4920 // options some characters have to be escaped with
4921 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if (valuep == &p_pt)
4923 {
4924 s = *valuep;
4925 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01004926 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 return FAIL;
4928 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004929 // expand the option value, replace $HOME by ~
4930 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004932 int size = (int)STRLEN(*valuep) + 1;
4933
4934 // replace home directory in the whole option value into "buf"
4935 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004936 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004937 goto fail;
4938 home_replace(NULL, *valuep, buf, size, FALSE);
4939
4940 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004941 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004942 // can be expanded when read back.
4943 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4944 && vim_strchr(*valuep, ',') != NULL)
4945 {
4946 part = alloc(size);
4947 if (part == NULL)
4948 goto fail;
4949
4950 // write line break to clear the option, e.g. ':set rtp='
4951 if (put_eol(fd) == FAIL)
4952 goto fail;
4953
4954 p = buf;
4955 while (*p != NUL)
4956 {
4957 // for each comma separated option part, append value to
4958 // the option, :set rtp+=value
4959 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4960 goto fail;
4961 (void)copy_option_part(&p, part, size, ",");
4962 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4963 goto fail;
4964 }
4965 vim_free(buf);
4966 vim_free(part);
4967 return OK;
4968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004970 {
4971 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004973 }
4974 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976 else if (put_escstr(fd, *valuep, 2) == FAIL)
4977 return FAIL;
4978 }
4979 if (put_eol(fd) < 0)
4980 return FAIL;
4981 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004982fail:
4983 vim_free(buf);
4984 vim_free(part);
4985 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986}
4987
4988 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004989put_setnum(
4990 FILE *fd,
4991 char *cmd,
4992 char *name,
4993 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994{
4995 long wc;
4996
4997 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4998 return FAIL;
4999 if (wc_use_keyname((char_u *)valuep, &wc))
5000 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005001 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5003 return FAIL;
5004 }
5005 else if (fprintf(fd, "%ld", *valuep) < 0)
5006 return FAIL;
5007 if (put_eol(fd) < 0)
5008 return FAIL;
5009 return OK;
5010}
5011
5012 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005013put_setbool(
5014 FILE *fd,
5015 char *cmd,
5016 char *name,
5017 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005019 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005020 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5022 || put_eol(fd) < 0)
5023 return FAIL;
5024 return OK;
5025}
5026
5027/*
5028 * Clear all the terminal options.
5029 * If the option has been allocated, free the memory.
5030 * Terminal options are never hidden or indirect.
5031 */
5032 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005033clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 /*
5036 * Reset a few things before clearing the old options. This may cause
5037 * outputting a few things that the terminal doesn't understand, but the
5038 * screen will be cleared later, so this is OK.
5039 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005040 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005041 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005043 // When starting the GUI close the display opened for the clipboard.
5044 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (gui.starting)
5046 clear_xterm_clip();
5047#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005048 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005050 free_termoptions();
5051}
5052
5053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005054free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005055{
5056 struct vimoption *p;
5057
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005058 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 if (istermoption(p))
5060 {
5061 if (p->flags & P_ALLOCED)
5062 free_string_option(*(char_u **)(p->var));
5063 if (p->flags & P_DEF_ALLOCED)
5064 free_string_option(p->def_val[VI_DEFAULT]);
5065 *(char_u **)(p->var) = empty_option;
5066 p->def_val[VI_DEFAULT] = empty_option;
5067 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005068#ifdef FEAT_EVAL
5069 // remember where the option was cleared
5070 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073 clear_termcodes();
5074}
5075
5076/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005077 * Free the string for one term option, if it was allocated.
5078 * Set the string to empty_option and clear allocated flag.
5079 * "var" points to the option value.
5080 */
5081 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005082free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005083{
5084 struct vimoption *p;
5085
5086 for (p = &options[0]; p->fullname != NULL; p++)
5087 if (p->var == var)
5088 {
5089 if (p->flags & P_ALLOCED)
5090 free_string_option(*(char_u **)(p->var));
5091 *(char_u **)(p->var) = empty_option;
5092 p->flags &= ~P_ALLOCED;
5093 break;
5094 }
5095}
5096
5097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 * Set the terminal option defaults to the current value.
5099 * Used after setting the terminal name.
5100 */
5101 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005102set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103{
5104 struct vimoption *p;
5105
5106 for (p = &options[0]; p->fullname != NULL; p++)
5107 {
5108 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5109 {
5110 if (p->flags & P_DEF_ALLOCED)
5111 {
5112 free_string_option(p->def_val[VI_DEFAULT]);
5113 p->flags &= ~P_DEF_ALLOCED;
5114 }
5115 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5116 if (p->flags & P_ALLOCED)
5117 {
5118 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005119 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121 }
5122 }
5123}
5124
5125/*
5126 * return TRUE if 'p' starts with 't_'
5127 */
5128 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005129istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130{
5131 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5132}
5133
Bram Moolenaardac13472019-09-16 21:06:21 +02005134/*
5135 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5136 */
5137 int
5138istermoption_idx(int opt_idx)
5139{
5140 return istermoption(&options[opt_idx]);
5141}
5142
Bram Moolenaar113e1072019-01-20 15:30:40 +01005143#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005145 * Unset local option value, similar to ":set opt<".
5146 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005147 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005148unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005149{
5150 struct vimoption *p;
5151 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005152 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005153
5154 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005155 if (opt_idx < 0)
5156 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005157 p = &(options[opt_idx]);
5158
5159 switch ((int)p->indir)
5160 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005161 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005162 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005163 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005164 break;
5165 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005166 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005167 break;
5168 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005169 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005170 break;
5171 case PV_AR:
5172 buf->b_p_ar = -1;
5173 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005174 case PV_BKC:
5175 clear_string_option(&buf->b_p_bkc);
5176 buf->b_bkc_flags = 0;
5177 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005178 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005179 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005180 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005181 case PV_TC:
5182 clear_string_option(&buf->b_p_tc);
5183 buf->b_tc_flags = 0;
5184 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005185 case PV_SISO:
5186 curwin->w_p_siso = -1;
5187 break;
5188 case PV_SO:
5189 curwin->w_p_so = -1;
5190 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005191# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005192 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005193 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005194 break;
5195 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005196 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005197 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005198# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005199 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005200 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005201 break;
5202 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005203 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005204 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005205# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005206 case PV_TSRFU:
5207 clear_string_option(&buf->b_p_tsrfu);
5208 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005209# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005210 case PV_FP:
5211 clear_string_option(&buf->b_p_fp);
5212 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005213# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005214 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005215 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005216 break;
5217 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005218 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005219 break;
5220 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005221 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005222 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005223# endif
5224# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005225 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005226 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005227 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005228# endif
5229# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005230 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005231 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005232 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005233# endif
5234# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01005235 case PV_SBR:
5236 clear_string_option(&((win_T *)from)->w_p_sbr);
5237 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005238# endif
5239# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005240 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005241 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005242 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005243# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005244 case PV_UL:
5245 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5246 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005247 case PV_LW:
5248 clear_string_option(&buf->b_p_lw);
5249 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005250 case PV_MENC:
5251 clear_string_option(&buf->b_p_menc);
5252 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005253 case PV_LCS:
5254 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005255 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005256 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005257 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005258 case PV_FCS:
5259 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005260 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005261 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005262 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005263 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005264 clear_string_option(&((win_T *)from)->w_p_ve);
5265 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005266 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005267 }
5268}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005269#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005270
5271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005273 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 */
5275 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005276get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005278 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
5280 if (p->var == VAR_WIN)
5281 return (char_u *)GLOBAL_WO(get_varp(p));
5282 return p->var;
5283 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005284 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 {
5286 switch ((int)p->indir)
5287 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005288 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005290 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5291 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5292 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005294 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5295 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5296 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005297 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005298 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005299 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005300 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5301 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005303 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5304 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005306 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5307 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005308#ifdef FEAT_COMPL_FUNC
5309 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5310#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005311#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5312 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5313#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005314#if defined(FEAT_CRYPT)
5315 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5316#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005317#ifdef FEAT_LINEBREAK
5318 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5319#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005320#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005321 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005322#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005323 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005324 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005325 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005326 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005327 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005328 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005329 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005332 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 }
5334 return get_varp(p);
5335}
5336
5337/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005338 * Get pointer to option variable at 'opt_idx', depending on local or global
5339 * scope.
5340 */
5341 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005342get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005343{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005344 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005345}
5346
5347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 * Get pointer to option variable.
5349 */
5350 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005351get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005353 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 if (p->var == NULL)
5355 return NULL;
5356
5357 switch ((int)p->indir)
5358 {
5359 case PV_NONE: return p->var;
5360
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005361 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005362 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005364 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005366 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005368 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005370 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005372 case PV_TC: return *curbuf->b_p_tc != NUL
5373 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005374 case PV_BKC: return *curbuf->b_p_bkc != NUL
5375 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005376 case PV_SISO: return curwin->w_p_siso >= 0
5377 ? (char_u *)&(curwin->w_p_siso) : p->var;
5378 case PV_SO: return curwin->w_p_so >= 0
5379 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005381 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005383 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5385#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005386 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005388 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005390#ifdef FEAT_COMPL_FUNC
5391 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5392 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5393#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005394 case PV_FP: return *curbuf->b_p_fp != NUL
5395 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005397 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005399 case PV_GP: return *curbuf->b_p_gp != NUL
5400 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5401 case PV_MP: return *curbuf->b_p_mp != NUL
5402 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005404#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5405 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5406 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5407#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005408#if defined(FEAT_CRYPT)
5409 case PV_CM: return *curbuf->b_p_cm != NUL
5410 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5411#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005412#ifdef FEAT_LINEBREAK
5413 case PV_SBR: return *curwin->w_p_sbr != NUL
5414 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5415#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005416#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005417 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005418 ? (char_u *)&(curwin->w_p_stl) : p->var;
5419#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005420 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5421 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005422 case PV_LW: return *curbuf->b_p_lw != NUL
5423 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005424 case PV_MENC: return *curbuf->b_p_menc != NUL
5425 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426#ifdef FEAT_ARABIC
5427 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5428#endif
5429 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005430 case PV_LCS: return *curwin->w_p_lcs != NUL
5431 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005432 case PV_FCS: return *curwin->w_p_fcs != NUL
5433 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005434 case PV_VE: return *curwin->w_p_ve != NUL
5435 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005436#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005437 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5438#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005439#ifdef FEAT_SYN_HL
5440 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5441 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005442 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005443 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445#ifdef FEAT_DIFF
5446 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5447#endif
5448#ifdef FEAT_FOLDING
5449 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5450 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5451 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5452 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5453 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5454 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5455 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5456# ifdef FEAT_EVAL
5457 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5458 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5459# endif
5460 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5461#endif
5462 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005463 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005464#ifdef FEAT_LINEBREAK
5465 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005468 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005469#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5471#endif
5472#ifdef FEAT_RIGHTLEFT
5473 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5474 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5475#endif
5476 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01005477 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5479#ifdef FEAT_LINEBREAK
5480 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005481 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5482 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005484 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005486 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005487#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005488 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5489 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5490#endif
5491#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005492 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5493 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5494 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
5497 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5498 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5501 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5503 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5505 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5506 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01005507 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510#ifdef FEAT_FOLDING
5511 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005514#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005515 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005517#ifdef FEAT_COMPL_FUNC
5518 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005519 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005520#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005521#ifdef FEAT_EVAL
5522 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5523#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01005524 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005526 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005532 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5534 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5535 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5536 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5537#ifdef FEAT_FIND_ID
5538# ifdef FEAT_EVAL
5539 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5540# endif
5541#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01005542#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5544 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5545#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005546#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005547 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549#ifdef FEAT_CRYPT
5550 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01005553 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5555 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5556 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5557 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5558 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005560 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5567#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005568 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005569 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5570#endif
5571#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005572 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5573 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5574 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005575 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576#endif
5577 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5578 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5579 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5580 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005581#ifdef FEAT_PERSISTENT_UNDO
5582 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5585#ifdef FEAT_KEYMAP
5586 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5587#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005588#ifdef FEAT_SIGNS
5589 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5590#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005591#ifdef FEAT_VARTABS
5592 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5593 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5594#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005595 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005597 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 return (char_u *)&(curbuf->b_p_wm);
5599}
5600
5601/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005602 * Return a pointer to the variable for option at 'opt_idx'
5603 */
5604 char_u *
5605get_option_var(int opt_idx)
5606{
5607 return options[opt_idx].var;
5608}
5609
Dominique Pelle748b3082022-01-08 12:41:16 +00005610#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005611/*
5612 * Return the full name of the option at 'opt_idx'
5613 */
5614 char_u *
5615get_option_fullname(int opt_idx)
5616{
5617 return (char_u *)options[opt_idx].fullname;
5618}
Dominique Pelle748b3082022-01-08 12:41:16 +00005619#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005620
5621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 * Get the value of 'equalprg', either the buffer-local one or the global one.
5623 */
5624 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005625get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
5627 if (*curbuf->b_p_ep == NUL)
5628 return p_ep;
5629 return curbuf->b_p_ep;
5630}
5631
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632/*
5633 * Copy options from one window to another.
5634 * Used when splitting a window.
5635 */
5636 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005637win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
5639 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5640 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005641 after_copy_winopt(wp_to);
5642}
5643
5644/*
5645 * After copying window options: update variables depending on options.
5646 */
5647 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00005648after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005649{
5650#ifdef FEAT_LINEBREAK
5651 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005652#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005653#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005654 fill_culopt_flags(NULL, wp);
5655 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005656#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005657 set_chars_option(wp, &wp->w_p_lcs, TRUE);
5658 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005659}
5660
5661 static char_u *
5662copy_option_val(char_u *val)
5663{
5664 if (val == empty_option)
5665 return empty_option; // no need to allocate memory
5666 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
5669/*
5670 * Copy the options from one winopt_T to another.
5671 * Doesn't free the old option values in "to", use clear_winopt() for that.
5672 * The 'scroll' option is not copied, because it depends on the window height.
5673 * The 'previewwindow' option is reset, there can be only one preview window.
5674 */
5675 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005676copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
5678#ifdef FEAT_ARABIC
5679 to->wo_arab = from->wo_arab;
5680#endif
5681 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005682 to->wo_lcs = copy_option_val(from->wo_lcs);
5683 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005685 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005686 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02005687 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005688#ifdef FEAT_LINEBREAK
5689 to->wo_nuw = from->wo_nuw;
5690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691#ifdef FEAT_RIGHTLEFT
5692 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005693 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005695#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005696 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01005697#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005698#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005699 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005702#ifdef FEAT_DIFF
5703 to->wo_wrap_save = from->wo_wrap_save;
5704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705#ifdef FEAT_LINEBREAK
5706 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005707 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005708 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005710 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005712 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01005713 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005714 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005715 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005716#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005717 to->wo_spell = from->wo_spell;
5718#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005719#ifdef FEAT_SYN_HL
5720 to->wo_cuc = from->wo_cuc;
5721 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005722 to->wo_culopt = copy_option_val(from->wo_culopt);
5723 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725#ifdef FEAT_DIFF
5726 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005727 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005729#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005730 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005731 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005732#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005733#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005734 to->wo_twk = copy_option_val(from->wo_twk);
5735 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737#ifdef FEAT_FOLDING
5738 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005739 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005741 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005742 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 to->wo_fml = from->wo_fml;
5744 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005745 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005746 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005747 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005748 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 to->wo_fdn = from->wo_fdn;
5750# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005751 to->wo_fde = copy_option_val(from->wo_fde);
5752 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005754 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005756#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005757 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005758#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005759
5760#ifdef FEAT_EVAL
5761 // Copy the script context so that we know where the value was last set.
5762 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5763 sizeof(to->wo_script_ctx));
5764#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005765 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766}
5767
5768/*
5769 * Check string options in a window for a NULL value.
5770 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005771 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005772check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773{
5774 check_winopt(&win->w_onebuf_opt);
5775 check_winopt(&win->w_allbuf_opt);
5776}
5777
5778/*
5779 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5780 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005781 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005782check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783{
5784#ifdef FEAT_FOLDING
5785 check_string_option(&wop->wo_fdi);
5786 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005787 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788# ifdef FEAT_EVAL
5789 check_string_option(&wop->wo_fde);
5790 check_string_option(&wop->wo_fdt);
5791# endif
5792 check_string_option(&wop->wo_fmr);
5793#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005794#ifdef FEAT_SIGNS
5795 check_string_option(&wop->wo_scl);
5796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797#ifdef FEAT_RIGHTLEFT
5798 check_string_option(&wop->wo_rlc);
5799#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005800#ifdef FEAT_LINEBREAK
5801 check_string_option(&wop->wo_sbr);
5802#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005803#ifdef FEAT_STL_OPT
5804 check_string_option(&wop->wo_stl);
5805#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005806#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005807 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005808 check_string_option(&wop->wo_cc);
5809#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005810#ifdef FEAT_CONCEAL
5811 check_string_option(&wop->wo_cocu);
5812#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005813#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005814 check_string_option(&wop->wo_twk);
5815 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005816#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005817#ifdef FEAT_LINEBREAK
5818 check_string_option(&wop->wo_briopt);
5819#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005820 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005821 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005822 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005823 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824}
5825
5826/*
5827 * Free the allocated memory inside a winopt_T.
5828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005830clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
5832#ifdef FEAT_FOLDING
5833 clear_string_option(&wop->wo_fdi);
5834 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005835 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836# ifdef FEAT_EVAL
5837 clear_string_option(&wop->wo_fde);
5838 clear_string_option(&wop->wo_fdt);
5839# endif
5840 clear_string_option(&wop->wo_fmr);
5841#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005842#ifdef FEAT_SIGNS
5843 clear_string_option(&wop->wo_scl);
5844#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005845#ifdef FEAT_LINEBREAK
5846 clear_string_option(&wop->wo_briopt);
5847#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005848 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849#ifdef FEAT_RIGHTLEFT
5850 clear_string_option(&wop->wo_rlc);
5851#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005852#ifdef FEAT_LINEBREAK
5853 clear_string_option(&wop->wo_sbr);
5854#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005855#ifdef FEAT_STL_OPT
5856 clear_string_option(&wop->wo_stl);
5857#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005858#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005859 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005860 clear_string_option(&wop->wo_cc);
5861#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005862#ifdef FEAT_CONCEAL
5863 clear_string_option(&wop->wo_cocu);
5864#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005865#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005866 clear_string_option(&wop->wo_twk);
5867 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005868#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005869 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005870 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005871 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872}
5873
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005874#ifdef FEAT_EVAL
5875// Index into the options table for a buffer-local option enum.
5876static int buf_opt_idx[BV_COUNT];
5877# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5878
5879/*
5880 * Initialize buf_opt_idx[] if not done already.
5881 */
5882 static void
5883init_buf_opt_idx(void)
5884{
5885 static int did_init_buf_opt_idx = FALSE;
5886 int i;
5887
5888 if (did_init_buf_opt_idx)
5889 return;
5890 did_init_buf_opt_idx = TRUE;
5891 for (i = 0; !istermoption_idx(i); i++)
5892 if (options[i].indir & PV_BUF)
5893 buf_opt_idx[options[i].indir & PV_MASK] = i;
5894}
5895#else
5896# define COPY_OPT_SCTX(buf, bv)
5897#endif
5898
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899/*
5900 * Copy global option values to local options for one buffer.
5901 * Used when creating a new buffer and sometimes when entering a buffer.
5902 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005903 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5905 * appropriate.
5906 * BCO_NOHELP Don't copy the values to a help buffer.
5907 */
5908 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005909buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910{
5911 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005912 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 int dont_do_help;
5914 int did_isk = FALSE;
5915
5916 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 * Skip this when the option defaults have not been set yet. Happens when
5918 * main() allocates the first buffer.
5919 */
5920 if (p_cpo != NULL)
5921 {
5922 /*
5923 * Always copy when entering and 'cpo' contains 'S'.
5924 * Don't copy when already initialized.
5925 * Don't copy when 'cpo' contains 's' and not entering.
5926 * 'S' BCO_ENTER initialized 's' should_copy
5927 * yes yes X X TRUE
5928 * yes no yes X FALSE
5929 * no X yes X FALSE
5930 * X no no yes FALSE
5931 * X no no no TRUE
5932 * no yes no X TRUE
5933 */
5934 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5935 && (buf->b_p_initialized
5936 || (!(flags & BCO_ENTER)
5937 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5938 should_copy = FALSE;
5939
5940 if (should_copy || (flags & BCO_ALWAYS))
5941 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005942#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005943 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005944 init_buf_opt_idx();
5945#endif
5946 // Don't copy the options specific to a help buffer when
5947 // BCO_NOHELP is given or the options were initialized already
5948 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5950 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005951 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 {
5953 save_p_isk = buf->b_p_isk;
5954 buf->b_p_isk = NULL;
5955 }
5956 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005957 * Always free the allocated strings. If not already initialized,
5958 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 */
5960 if (!buf->b_p_initialized)
5961 {
5962 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005963 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005966 switch (*p_ffs)
5967 {
5968 case 'm':
5969 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5970 case 'd':
5971 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5972 case 'u':
5973 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5974 default:
5975 buf->b_p_ff = vim_strsave(p_ff);
5976 }
5977 if (buf->b_p_ff != NULL)
5978 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 buf->b_p_bh = empty_option;
5980 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 }
5982 else
5983 free_buf_options(buf, FALSE);
5984
5985 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005986 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 buf->b_p_ai_nopaste = p_ai_nopaste;
5988 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005989 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005991 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 buf->b_p_tw_nopaste = p_tw_nopaste;
5993 buf->b_p_tw_nobin = p_tw_nobin;
5994 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005995 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 buf->b_p_wm_nopaste = p_wm_nopaste;
5997 buf->b_p_wm_nobin = p_wm_nobin;
5998 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005999 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006000 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006001 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006002 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006003 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006005 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006007 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006009 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 buf->b_p_ml_nobin = p_ml_nobin;
6011 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006012 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006013 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006014 buf->b_p_swf = FALSE;
6015 else
6016 {
6017 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006018 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006021 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006022#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006023 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006024 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006026#ifdef FEAT_COMPL_FUNC
6027 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006028 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006029 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006030 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006031 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006032 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006033#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006034#ifdef FEAT_EVAL
6035 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006036 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006037 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006040 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006042#ifdef FEAT_VARTABS
6043 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006044 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006045 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006046 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006047 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006048 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006049 buf->b_p_vsts_nopaste = p_vsts_nopaste
6050 ? vim_strsave(p_vsts_nopaste) : NULL;
6051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006053 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006055 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056#ifdef FEAT_FOLDING
6057 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006058 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059#endif
6060 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006061 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006062 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006063 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006064 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6065 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006067 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006069 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006071 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006073 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006074
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006076 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006078 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006080 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006081 buf->b_p_cinsd = vim_strsave(p_cinsd);
6082 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006083 buf->b_p_lop = vim_strsave(p_lop);
6084 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006085
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006086 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006089 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006091 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006093 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006095 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006097 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006098 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006099 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006100#endif
6101#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006102 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006103 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006104 (void)compile_cap_prog(&buf->b_s);
6105 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006106 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006107 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006108 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006109 buf->b_s.b_p_spo = vim_strsave(p_spo);
6110 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006112#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006114 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006116 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006118 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006119#if defined(FEAT_EVAL)
6120 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006121 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123#ifdef FEAT_CRYPT
6124 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006125 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006128 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129#ifdef FEAT_KEYMAP
6130 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006131 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 buf->b_kmap_state |= KEYMAP_INIT;
6133#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006134#ifdef FEAT_TERMINAL
6135 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006136 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006137#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006138 // This isn't really an option, but copying the langmap and IME
6139 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006141 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006143 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006145 // options that are normally global but also have a local value
6146 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006148 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006149 buf->b_p_bkc = empty_option;
6150 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151#ifdef FEAT_QUICKFIX
6152 buf->b_p_gp = empty_option;
6153 buf->b_p_mp = empty_option;
6154 buf->b_p_efm = empty_option;
6155#endif
6156 buf->b_p_ep = empty_option;
6157 buf->b_p_kp = empty_option;
6158 buf->b_p_path = empty_option;
6159 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006160 buf->b_p_tc = empty_option;
6161 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162#ifdef FEAT_FIND_ID
6163 buf->b_p_def = empty_option;
6164 buf->b_p_inc = empty_option;
6165# ifdef FEAT_EVAL
6166 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006167 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168# endif
6169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 buf->b_p_dict = empty_option;
6171 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006172#ifdef FEAT_COMPL_FUNC
6173 buf->b_p_tsrfu = empty_option;
6174#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006175 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006176 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006177#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6178 buf->b_p_bexpr = empty_option;
6179#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006180#if defined(FEAT_CRYPT)
6181 buf->b_p_cm = empty_option;
6182#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006183#ifdef FEAT_PERSISTENT_UNDO
6184 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006185 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006186#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006187 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006188 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
6190 /*
6191 * Don't copy the options set by ex_help(), use the saved values,
6192 * when going from a help buffer to a non-help buffer.
6193 * Don't touch these at all when BCO_NOHELP is used and going from
6194 * or to a help buffer.
6195 */
6196 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006199#ifdef FEAT_VARTABS
6200 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006201 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006202 else
6203 buf->b_p_vts_array = NULL;
6204#endif
6205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 else
6207 {
6208 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006209 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 did_isk = TRUE;
6211 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006212 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006213#ifdef FEAT_VARTABS
6214 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006215 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006216 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006217 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006218 else
6219 buf->b_p_vts_array = NULL;
6220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (buf->b_p_bt[0] == 'h')
6223 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006225 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 }
6227 }
6228
6229 /*
6230 * When the options should be copied (ignoring BCO_ALWAYS), set the
6231 * flag that indicates that the options have been initialized.
6232 */
6233 if (should_copy)
6234 buf->b_p_initialized = TRUE;
6235 }
6236
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006237 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 if (did_isk)
6239 (void)buf_init_chartab(buf, FALSE);
6240}
6241
6242/*
6243 * Reset the 'modifiable' option and its default value.
6244 */
6245 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006246reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247{
6248 int opt_idx;
6249
6250 curbuf->b_p_ma = FALSE;
6251 p_ma = FALSE;
6252 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006253 if (opt_idx >= 0)
6254 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255}
6256
6257/*
6258 * Set the global value for 'iminsert' to the local value.
6259 */
6260 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006261set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 p_iminsert = curbuf->b_p_iminsert;
6264}
6265
6266/*
6267 * Set the global value for 'imsearch' to the local value.
6268 */
6269 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006270set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271{
6272 p_imsearch = curbuf->b_p_imsearch;
6273}
6274
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275static int expand_option_idx = -1;
6276static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6277static int expand_option_flags = 0;
6278
6279 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006280set_context_in_set_cmd(
6281 expand_T *xp,
6282 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006283 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284{
6285 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006286 long_u flags = 0; // init for GCC
6287 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 char_u *p;
6289 char_u *s;
6290 int is_term_option = FALSE;
6291 int key;
6292
6293 expand_option_flags = opt_flags;
6294
6295 xp->xp_context = EXPAND_SETTINGS;
6296 if (*arg == NUL)
6297 {
6298 xp->xp_pattern = arg;
6299 return;
6300 }
6301 p = arg + STRLEN(arg) - 1;
6302 if (*p == ' ' && *(p - 1) != '\\')
6303 {
6304 xp->xp_pattern = p + 1;
6305 return;
6306 }
6307 while (p > arg)
6308 {
6309 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006310 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (*p == ' ' || *p == ',')
6312 {
6313 while (s > arg && *(s - 1) == '\\')
6314 --s;
6315 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006316 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 if (*p == ' ' && ((p - s) & 1) == 0)
6318 {
6319 ++p;
6320 break;
6321 }
6322 --p;
6323 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006324 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 {
6326 xp->xp_context = EXPAND_BOOL_SETTINGS;
6327 p += 2;
6328 }
6329 if (STRNCMP(p, "inv", 3) == 0)
6330 {
6331 xp->xp_context = EXPAND_BOOL_SETTINGS;
6332 p += 3;
6333 }
6334 xp->xp_pattern = arg = p;
6335 if (*arg == '<')
6336 {
6337 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006338 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 return;
6340 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006341 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
6343 xp->xp_context = EXPAND_NOTHING;
6344 return;
6345 }
6346 nextchar = *++p;
6347 is_term_option = TRUE;
6348 expand_option_name[2] = KEY2TERMCAP0(key);
6349 expand_option_name[3] = KEY2TERMCAP1(key);
6350 }
6351 else
6352 {
6353 if (p[0] == 't' && p[1] == '_')
6354 {
6355 p += 2;
6356 if (*p != NUL)
6357 ++p;
6358 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006359 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 nextchar = *++p;
6361 is_term_option = TRUE;
6362 expand_option_name[2] = p[-2];
6363 expand_option_name[3] = p[-1];
6364 }
6365 else
6366 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006367 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6369 p++;
6370 if (*p == NUL)
6371 return;
6372 nextchar = *p;
6373 *p = NUL;
6374 opt_idx = findoption(arg);
6375 *p = nextchar;
6376 if (opt_idx == -1 || options[opt_idx].var == NULL)
6377 {
6378 xp->xp_context = EXPAND_NOTHING;
6379 return;
6380 }
6381 flags = options[opt_idx].flags;
6382 if (flags & P_BOOL)
6383 {
6384 xp->xp_context = EXPAND_NOTHING;
6385 return;
6386 }
6387 }
6388 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006389 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6391 {
6392 ++p;
6393 nextchar = '=';
6394 }
6395 if ((nextchar != '=' && nextchar != ':')
6396 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6397 {
6398 xp->xp_context = EXPAND_UNSUCCESSFUL;
6399 return;
6400 }
6401 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6402 {
6403 xp->xp_context = EXPAND_OLD_SETTING;
6404 if (is_term_option)
6405 expand_option_idx = -1;
6406 else
6407 expand_option_idx = opt_idx;
6408 xp->xp_pattern = p + 1;
6409 return;
6410 }
6411 xp->xp_context = EXPAND_NOTHING;
6412 if (is_term_option || (flags & P_NUM))
6413 return;
6414
6415 xp->xp_pattern = p + 1;
6416
6417 if (flags & P_EXPAND)
6418 {
6419 p = options[opt_idx].var;
6420 if (p == (char_u *)&p_bdir
6421 || p == (char_u *)&p_dir
6422 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006423 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426#ifdef FEAT_SESSION
6427 || p == (char_u *)&p_vdir
6428#endif
6429 )
6430 {
6431 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006432 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 xp->xp_backslash = XP_BS_THREE;
6434 else
6435 xp->xp_backslash = XP_BS_ONE;
6436 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006437 else if (p == (char_u *)&p_ft)
6438 {
6439 xp->xp_context = EXPAND_FILETYPE;
6440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 else
6442 {
6443 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006444 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 if (p == (char_u *)&p_tags)
6446 xp->xp_backslash = XP_BS_THREE;
6447 else
6448 xp->xp_backslash = XP_BS_ONE;
6449 }
6450 }
6451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006452 // For an option that is a list of file names, find the start of the
6453 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6455 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006456 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 if (*p == ' ' || *p == ',')
6458 {
6459 s = p;
6460 while (s > xp->xp_pattern && *(s - 1) == '\\')
6461 --s;
6462 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6463 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6464 {
6465 xp->xp_pattern = p + 1;
6466 break;
6467 }
6468 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006469
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006470#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006471 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006472 if (options[opt_idx].var == (char_u *)&p_sps
6473 && STRNCMP(p, "file:", 5) == 0)
6474 {
6475 xp->xp_pattern = p + 5;
6476 break;
6477 }
6478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480}
6481
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006482/*
6483 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
6484 *
6485 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
6486 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
6487 *
6488 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
6489 * regular expression 'regmatch', then stores the match in matches[idx] and
6490 * returns TRUE.
6491 *
6492 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6493 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
6494 *
6495 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6496 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
6497 */
6498 static int
6499match_str(
6500 char_u *str,
6501 regmatch_T *regmatch,
6502 char_u **matches,
6503 int idx,
6504 int test_only,
6505 int fuzzy,
6506 char_u *fuzzystr,
6507 fuzmatch_str_T *fuzmatch)
6508{
6509 if (!fuzzy)
6510 {
6511 if (vim_regexec(regmatch, str, (colnr_T)0))
6512 {
6513 if (!test_only)
6514 matches[idx] = vim_strsave(str);
6515 return TRUE;
6516 }
6517 }
6518 else
6519 {
6520 int score;
6521
6522 score = fuzzy_match_str(str, fuzzystr);
6523 if (score != 0)
6524 {
6525 if (!test_only)
6526 {
6527 fuzmatch[idx].idx = idx;
6528 fuzmatch[idx].str = vim_strsave(str);
6529 fuzmatch[idx].score = score;
6530 }
6531
6532 return TRUE;
6533 }
6534 }
6535
6536 return FALSE;
6537}
6538
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006540ExpandSettings(
6541 expand_T *xp,
6542 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006543 char_u *fuzzystr,
6544 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01006545 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006546 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006548 int num_normal = 0; // Nr of matching non-term-code settings
6549 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 int opt_idx;
6551 int match;
6552 int count = 0;
6553 char_u *str;
6554 int loop;
6555 int is_term_opt;
6556 char_u name_buf[MAX_KEY_NAME_LEN];
6557 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006558 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006559 int fuzzy;
6560 fuzmatch_str_T *fuzmatch = NULL;
6561
Christian Brabandtcb747892022-05-08 21:10:56 +01006562 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006564 // do this loop twice:
6565 // loop == 0: count the number of matching options
6566 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 for (loop = 0; loop <= 1; ++loop)
6568 {
6569 regmatch->rm_ic = ic;
6570 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6571 {
K.Takataeeec2542021-06-02 13:28:16 +02006572 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006573 {
6574 if (match_str((char_u *)names[match], regmatch, *matches,
6575 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 {
6577 if (loop == 0)
6578 num_normal++;
6579 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006580 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 }
6584 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6585 opt_idx++)
6586 {
6587 if (options[opt_idx].var == NULL)
6588 continue;
6589 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6590 && !(options[opt_idx].flags & P_BOOL))
6591 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006592 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 if (is_term_opt && num_normal > 0)
6594 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006595
6596 if (match_str(str, regmatch, *matches, count, (loop == 0),
6597 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 {
6599 if (loop == 0)
6600 {
6601 if (is_term_opt)
6602 num_term++;
6603 else
6604 num_normal++;
6605 }
6606 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006607 count++;
6608 }
6609 else if (!fuzzy && options[opt_idx].shortname != NULL
6610 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006611 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006612 {
6613 // Compare against the abbreviated option name (for regular
6614 // expression match). Fuzzy matching (previous if) already
6615 // matches against both the expanded and abbreviated names.
6616 if (loop == 0)
6617 {
6618 if (is_term_opt)
6619 num_term++;
6620 else
6621 num_normal++;
6622 }
6623 else
6624 (*matches)[count++] = vim_strsave(str);
6625 }
6626 else if (is_term_opt)
6627 {
6628 name_buf[0] = '<';
6629 name_buf[1] = 't';
6630 name_buf[2] = '_';
6631 name_buf[3] = str[2];
6632 name_buf[4] = str[3];
6633 name_buf[5] = '>';
6634 name_buf[6] = NUL;
6635
6636 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6637 fuzzy, fuzzystr, fuzmatch))
6638 {
6639 if (loop == 0)
6640 num_term++;
6641 else
6642 count++;
6643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 }
6645 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006646
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 /*
6648 * Check terminal key codes, these are not in the option table
6649 */
6650 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6651 {
6652 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6653 {
6654 if (!isprint(str[0]) || !isprint(str[1]))
6655 continue;
6656
6657 name_buf[0] = 't';
6658 name_buf[1] = '_';
6659 name_buf[2] = str[0];
6660 name_buf[3] = str[1];
6661 name_buf[4] = NUL;
6662
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006663 if (match_str(name_buf, regmatch, *matches, count,
6664 (loop == 0), fuzzy, fuzzystr, fuzmatch))
6665 {
6666 if (loop == 0)
6667 num_term++;
6668 else
6669 count++;
6670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 else
6672 {
6673 name_buf[0] = '<';
6674 name_buf[1] = 't';
6675 name_buf[2] = '_';
6676 name_buf[3] = str[0];
6677 name_buf[4] = str[1];
6678 name_buf[5] = '>';
6679 name_buf[6] = NUL;
6680
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006681 if (match_str(name_buf, regmatch, *matches, count,
6682 (loop == 0), fuzzy, fuzzystr,
6683 fuzmatch))
6684 {
6685 if (loop == 0)
6686 num_term++;
6687 else
6688 count++;
6689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 }
6691 }
6692
6693 /*
6694 * Check special key names.
6695 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006696 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6698 {
6699 name_buf[0] = '<';
6700 STRCPY(name_buf + 1, str);
6701 STRCAT(name_buf, ">");
6702
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006703 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6704 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 {
6706 if (loop == 0)
6707 num_term++;
6708 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006709 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 }
6711 }
6712 }
6713 if (loop == 0)
6714 {
6715 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006716 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006718 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 else
6720 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006721 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006723 *matches = ALLOC_MULT(char_u *, *numMatches);
6724 if (*matches == NULL)
6725 {
6726 *matches = (char_u **)"";
6727 return FAIL;
6728 }
6729 }
6730 else
6731 {
6732 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
6733 if (fuzmatch == NULL)
6734 {
6735 *matches = (char_u **)"";
6736 return FAIL;
6737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 }
6739 }
6740 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006741
6742 if (fuzzy &&
6743 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
6744 return FAIL;
6745
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 return OK;
6747}
6748
6749 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00006750ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006752 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 char_u *buf;
6754
zeertzjqb0d45ec2023-01-25 15:04:22 +00006755 *numMatches = 0;
6756 *matches = ALLOC_ONE(char_u *);
6757 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 return FAIL;
6759
6760 /*
6761 * For a terminal key code expand_option_idx is < 0.
6762 */
6763 if (expand_option_idx < 0)
6764 {
6765 var = find_termcode(expand_option_name + 2);
6766 if (var == NULL)
6767 expand_option_idx = findoption(expand_option_name);
6768 }
6769
6770 if (expand_option_idx >= 0)
6771 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006772 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 option_value2string(&options[expand_option_idx], expand_option_flags);
6774 var = NameBuff;
6775 }
6776 else if (var == NULL)
6777 var = (char_u *)"";
6778
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006779 // A backslash is required before some characters. This is the reverse of
6780 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 buf = vim_strsave_escaped(var, escape_chars);
6782
6783 if (buf == NULL)
6784 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00006785 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 return FAIL;
6787 }
6788
6789#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006790 // For MS-Windows et al. we don't double backslashes at the start and
6791 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006792 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 if (var[0] == '\\' && var[1] == '\\'
6794 && expand_option_idx >= 0
6795 && (options[expand_option_idx].flags & P_EXPAND)
6796 && vim_isfilec(var[2])
6797 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006798 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799#endif
6800
zeertzjqb0d45ec2023-01-25 15:04:22 +00006801 *matches[0] = buf;
6802 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 return OK;
6804}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
6806/*
6807 * Get the value for the numeric or string option *opp in a nice format into
6808 * NameBuff[]. Must not be called with a hidden option!
6809 */
6810 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006811option_value2string(
6812 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006813 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814{
6815 char_u *varp;
6816
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006817 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
6819 if (opp->flags & P_NUM)
6820 {
6821 long wc = 0;
6822
6823 if (wc_use_keyname(varp, &wc))
6824 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6825 else if (wc != 0)
6826 STRCPY(NameBuff, transchar((int)wc));
6827 else
6828 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6829 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006830 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 {
6832 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006833 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 NameBuff[0] = NUL;
6835#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006836 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006837 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 STRCPY(NameBuff, "*****");
6839#endif
6840 else if (opp->flags & P_EXPAND)
6841 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006842 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 else if ((char_u **)opp->var == &p_pt)
6844 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6845 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006846 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 }
6848}
6849
6850/*
6851 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6852 * printed as a keyname.
6853 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6854 */
6855 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006856wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
6858 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6859 {
6860 *wcp = *(long *)varp;
6861 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6862 return TRUE;
6863 }
6864 return FALSE;
6865}
6866
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 * Return TRUE if "x" is present in 'shortmess' option, or
6869 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6870 */
6871 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006872shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006874 return p_shm != NULL &&
6875 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 || (vim_strchr(p_shm, 'a') != NULL
6877 && vim_strchr((char_u *)SHM_A, x) != NULL));
6878}
6879
6880/*
6881 * paste_option_changed() - Called after p_paste was set or reset.
6882 */
6883 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006884paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
6886 static int old_p_paste = FALSE;
6887 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006888 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890#ifdef FEAT_RIGHTLEFT
6891 static int save_ri = 0;
6892 static int save_hkmap = 0;
6893#endif
6894 buf_T *buf;
6895
6896 if (p_paste)
6897 {
6898 /*
6899 * Paste switched from off to on.
6900 * Save the current values, so they can be restored later.
6901 */
6902 if (!old_p_paste)
6903 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006904 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006905 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {
6907 buf->b_p_tw_nopaste = buf->b_p_tw;
6908 buf->b_p_wm_nopaste = buf->b_p_wm;
6909 buf->b_p_sts_nopaste = buf->b_p_sts;
6910 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006911 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006912#ifdef FEAT_VARTABS
6913 if (buf->b_p_vsts_nopaste)
6914 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006915 buf->b_p_vsts_nopaste =
6916 buf->b_p_vsts && buf->b_p_vsts != empty_option
6917 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 }
6920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006921 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006923 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925#ifdef FEAT_RIGHTLEFT
6926 save_ri = p_ri;
6927 save_hkmap = p_hkmap;
6928#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006929 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006930 p_ai_nopaste = p_ai;
6931 p_et_nopaste = p_et;
6932 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 p_tw_nopaste = p_tw;
6934 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006935#ifdef FEAT_VARTABS
6936 if (p_vsts_nopaste)
6937 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006938 p_vsts_nopaste = p_vsts && p_vsts != empty_option
6939 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 }
6942
6943 /*
6944 * Always set the option values, also when 'paste' is set when it is
6945 * already on.
6946 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006947 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006948 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006950 buf->b_p_tw = 0; // textwidth is 0
6951 buf->b_p_wm = 0; // wrapmargin is 0
6952 buf->b_p_sts = 0; // softtabstop is 0
6953 buf->b_p_ai = 0; // no auto-indent
6954 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006955#ifdef FEAT_VARTABS
6956 if (buf->b_p_vsts)
6957 free_string_option(buf->b_p_vsts);
6958 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006959 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 }
6962
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006963 // set global options
6964 p_sm = 0; // no showmatch
6965 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006967 status_redraw_all(); // redraw to remove the ruler
6968 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006970 p_ri = 0; // no reverse insert
6971 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006973 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 p_tw = 0;
6975 p_wm = 0;
6976 p_sts = 0;
6977 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006978#ifdef FEAT_VARTABS
6979 if (p_vsts)
6980 free_string_option(p_vsts);
6981 p_vsts = empty_option;
6982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 }
6984
6985 /*
6986 * Paste switched from on to off: Restore saved values.
6987 */
6988 else if (old_p_paste)
6989 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006990 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006991 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {
6993 buf->b_p_tw = buf->b_p_tw_nopaste;
6994 buf->b_p_wm = buf->b_p_wm_nopaste;
6995 buf->b_p_sts = buf->b_p_sts_nopaste;
6996 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006997 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006998#ifdef FEAT_VARTABS
6999 if (buf->b_p_vsts)
7000 free_string_option(buf->b_p_vsts);
7001 buf->b_p_vsts = buf->b_p_vsts_nopaste
7002 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007003 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007004 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007005 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007006 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007007 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 }
7010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007011 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007013 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007015 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017#ifdef FEAT_RIGHTLEFT
7018 p_ri = save_ri;
7019 p_hkmap = save_hkmap;
7020#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007021 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007022 p_ai = p_ai_nopaste;
7023 p_et = p_et_nopaste;
7024 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 p_tw = p_tw_nopaste;
7026 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007027#ifdef FEAT_VARTABS
7028 if (p_vsts)
7029 free_string_option(p_vsts);
7030 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
7033
7034 old_p_paste = p_paste;
7035}
7036
7037/*
7038 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7039 *
7040 * Reset 'compatible' and set the values for options that didn't get set yet
7041 * to the Vim defaults.
7042 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007043 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 */
7045 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007046vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007048 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007049 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007050 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
7052 if (!option_was_set((char_u *)"cp"))
7053 {
7054 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007055 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7057 set_option_default(opt_idx, OPT_FREE, FALSE);
7058 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007059 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007061
7062 if (fname != NULL)
7063 {
7064 p = vim_getenv(envname, &dofree);
7065 if (p == NULL)
7066 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007067 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007068 p = FullName_save(fname, FALSE);
7069 if (p != NULL)
7070 {
7071 vim_setenv(envname, p);
7072 vim_free(p);
7073 }
7074 }
7075 else if (dofree)
7076 vim_free(p);
7077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078}
7079
7080/*
7081 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7082 */
7083 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007084change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007086 int opt_idx;
7087
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 if (p_cp != on)
7089 {
7090 p_cp = on;
7091 compatible_set();
7092 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007093 opt_idx = findoption((char_u *)"cp");
7094 if (opt_idx >= 0)
7095 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096}
7097
7098/*
7099 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007100 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 */
7102 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007103option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104{
7105 int idx;
7106
7107 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007108 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 return FALSE;
7110 if (options[idx].flags & P_WAS_SET)
7111 return TRUE;
7112 return FALSE;
7113}
7114
7115/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007116 * Reset the flag indicating option "name" was set.
7117 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007118 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007119reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007120{
7121 int idx = findoption(name);
7122
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007123 if (idx < 0)
7124 return FAIL;
7125
7126 options[idx].flags &= ~P_WAS_SET;
7127 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007128}
7129
7130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 * compatible_set() - Called when 'compatible' has been set or unset.
7132 *
7133 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7134 * flag) to a Vi compatible value.
7135 * When 'compatible' is unset: Set all options that have a different default
7136 * for Vim (without the P_VI_DEF flag) to that default.
7137 */
7138 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007139compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140{
7141 int opt_idx;
7142
Bram Moolenaardac13472019-09-16 21:06:21 +02007143 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7145 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7146 set_option_default(opt_idx, OPT_FREE, p_cp);
7147 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007148 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149}
7150
Bram Moolenaardac13472019-09-16 21:06:21 +02007151#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153/*
7154 * fill_breakat_flags() -- called when 'breakat' changes value.
7155 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007156 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007157fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007159 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 int i;
7161
7162 for (i = 0; i < 256; i++)
7163 breakat_flags[i] = FALSE;
7164
7165 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007166 for (p = p_breakat; *p; p++)
7167 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169#endif
7170
7171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 * Check if backspacing over something is allowed.
7173 */
7174 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007175can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007176 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007178#ifdef FEAT_JOB_CHANNEL
7179 if (what == BS_START && bt_prompt(curbuf))
7180 return FALSE;
7181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 switch (*p_bs)
7183 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007184 case '3': return TRUE;
7185 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 case '1': return (what != BS_START);
7187 case '0': return FALSE;
7188 }
7189 return vim_strchr(p_bs, what) != NULL;
7190}
7191
7192/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007193 * Return the effective 'scrolloff' value for the current window, using the
7194 * global value when appropriate.
7195 */
7196 long
7197get_scrolloff_value(void)
7198{
7199 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7200}
7201
7202/*
7203 * Return the effective 'sidescrolloff' value for the current window, using the
7204 * global value when appropriate.
7205 */
7206 long
7207get_sidescrolloff_value(void)
7208{
7209 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7210}
7211
7212/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007213 * Get the local or global value of 'backupcopy'.
7214 */
7215 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007216get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007217{
7218 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7219}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007220
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007221#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007222/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007223 * Get the local or global value of 'formatlistpat'.
7224 */
7225 char_u *
7226get_flp_value(buf_T *buf)
7227{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007228 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7229 return p_flp;
7230 return buf->b_p_flp;
7231}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007232#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007233
7234/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007235 * Get the local or global value of the 'virtualedit' flags.
7236 */
7237 unsigned int
7238get_ve_flags(void)
7239{
Gary Johnson51ad8502021-08-03 18:33:08 +02007240 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7241 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007242}
7243
Bram Moolenaaree857022019-11-09 23:26:40 +01007244#if defined(FEAT_LINEBREAK) || defined(PROTO)
7245/*
7246 * Get the local or global value of 'showbreak'.
7247 */
7248 char_u *
7249get_showbreak_value(win_T *win)
7250{
7251 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7252 return p_sbr;
7253 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7254 return empty_option;
7255 return win->w_p_sbr;
7256}
7257#endif
7258
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007259#if defined(FEAT_EVAL) || defined(PROTO)
7260/*
7261 * Get window or buffer local options.
7262 */
7263 dict_T *
7264get_winbuf_options(int bufopt)
7265{
7266 dict_T *d;
7267 int opt_idx;
7268
7269 d = dict_alloc();
7270 if (d == NULL)
7271 return NULL;
7272
Bram Moolenaardac13472019-09-16 21:06:21 +02007273 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007274 {
7275 struct vimoption *opt = &options[opt_idx];
7276
7277 if ((bufopt && (opt->indir & PV_BUF))
7278 || (!bufopt && (opt->indir & PV_WIN)))
7279 {
7280 char_u *varp = get_varp(opt);
7281
7282 if (varp != NULL)
7283 {
7284 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007285 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007286 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007287 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007288 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007289 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007290 }
7291 }
7292 }
7293
7294 return d;
7295}
7296#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007297
Bram Moolenaardac13472019-09-16 21:06:21 +02007298#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007299/*
7300 * This is called when 'culopt' is changed
7301 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007302 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007303fill_culopt_flags(char_u *val, win_T *wp)
7304{
7305 char_u *p;
7306 char_u culopt_flags_new = 0;
7307
7308 if (val == NULL)
7309 p = wp->w_p_culopt;
7310 else
7311 p = val;
7312 while (*p != NUL)
7313 {
7314 if (STRNCMP(p, "line", 4) == 0)
7315 {
7316 p += 4;
7317 culopt_flags_new |= CULOPT_LINE;
7318 }
7319 else if (STRNCMP(p, "both", 4) == 0)
7320 {
7321 p += 4;
7322 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7323 }
7324 else if (STRNCMP(p, "number", 6) == 0)
7325 {
7326 p += 6;
7327 culopt_flags_new |= CULOPT_NBR;
7328 }
7329 else if (STRNCMP(p, "screenline", 10) == 0)
7330 {
7331 p += 10;
7332 culopt_flags_new |= CULOPT_SCRLINE;
7333 }
7334
7335 if (*p != ',' && *p != NUL)
7336 return FAIL;
7337 if (*p == ',')
7338 ++p;
7339 }
7340
7341 // Can't have both "line" and "screenline".
7342 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7343 return FAIL;
7344 wp->w_p_culopt_flags = culopt_flags_new;
7345
7346 return OK;
7347}
7348#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007349
7350/*
7351 * Get the value of 'magic' adjusted for Vim9 script.
7352 */
7353 int
7354magic_isset(void)
7355{
7356 switch (magic_overruled)
7357 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007358 case OPTION_MAGIC_ON: return TRUE;
7359 case OPTION_MAGIC_OFF: return FALSE;
7360 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007361 }
7362#ifdef FEAT_EVAL
7363 if (in_vim9script())
7364 return TRUE;
7365#endif
7366 return p_magic;
7367}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007368
7369/*
7370 * Set the callback function value for an option that accepts a function name,
7371 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7372 * Returns OK if the option is successfully set to a function, otherwise
7373 * returns FAIL.
7374 */
7375 int
7376option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7377{
7378#ifdef FEAT_EVAL
7379 typval_T *tv;
7380 callback_T cb;
7381
7382 if (optval == NULL || *optval == NUL)
7383 {
7384 free_callback(optcb);
7385 return OK;
7386 }
7387
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007388 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007389 || (STRNCMP(optval, "function(", 9) == 0)
7390 || (STRNCMP(optval, "funcref(", 8) == 0))
7391 // Lambda expression or a funcref
7392 tv = eval_expr(optval, NULL);
7393 else
7394 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007395 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007396 if (tv == NULL)
7397 return FAIL;
7398
7399 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007400 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007401 {
7402 free_tv(tv);
7403 return FAIL;
7404 }
7405
7406 free_callback(optcb);
7407 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00007408 if (cb.cb_free_name)
7409 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007410 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007411
7412 // when using Vim9 style "import.funcname" it needs to be expanded to
7413 // "import#funcname".
7414 expand_autload_callback(optcb);
7415
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007416 return OK;
7417#else
7418 return FAIL;
7419#endif
7420}