blob: 9ff3084adc0b9af936535c50c3b8e027af0fcd41 [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:
13 * - Put it in the options array below (copy an existing entry).
14 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
16 * - Add a PV_XX entry to the enum below.
17 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
19 * - For a buffer option, add some code to buf_copy_options().
20 * - For a buffer string option, add code to check_buf_options().
21 * - If it's a numeric option, add any necessary bounds checks to do_set().
22 * - If it's a list of flags, add some code in do_set(), search for WW_ALL.
23 * - When adding an option with expansion (P_EXPAND), but with a different
24 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020025 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 * options.txt, and any other related places.
27 * - Add an entry in runtime/optwin.vim.
28 * When making changes:
29 * - Adjust the help for the option in doc/option.txt.
30 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
31 * comment at the help for the 'compatible' option.
32 */
33
34#define IN_OPTION_C
35#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020036#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010039static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020040static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010041static char_u *option_expand(int opt_idx, char_u *val);
42static void didset_options(void);
43static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000044#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000046#else
47# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
48#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010049static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
50static 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 +020051static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020053static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010054static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010055static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
57static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020058static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000059static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020061static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000062static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010063static void check_winopt(winopt_T *wop);
64static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010065static void paste_option_changed(void);
66static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/*
69 * Initialize the options, first part.
70 *
71 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010072 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000073 */
74 void
Bram Moolenaar07268702018-03-01 21:57:32 +010075set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
77 char_u *p;
78 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000079 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81#ifdef FEAT_LANGMAP
82 langmap_init();
83#endif
84
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010085 // Be Vi compatible by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 p_cp = TRUE;
87
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010088 // Use POSIX compatibility when $VIM_POSIX is set.
Bram Moolenaar4399ef42005-02-12 14:29:27 +000089 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +000090 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +000091 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020092 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000093 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000094
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 /*
96 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +000097 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 */
Bram Moolenaar7c626922005-02-07 22:01:03 +000099 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100100#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100102 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000104 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200105#if defined(MSWIN)
106 {
107 // For MS-Windows put the path in quotes instead of escaping spaces.
108 char_u *cmd;
109 size_t len;
110
111 if (vim_strchr(p, ' ') != NULL)
112 {
113 len = STRLEN(p) + 3; // two quotes and a trailing NUL
114 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200115 if (cmd != NULL)
116 {
117 vim_snprintf((char *)cmd, len, "\"%s\"", p);
118 set_string_default("sh", cmd);
119 vim_free(cmd);
120 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200121 }
122 else
123 set_string_default("sh", p);
124 }
125#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100126 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_WILDIGN
130 /*
131 * Set the default for 'backupskip' to include environment variables for
132 * temp files.
133 */
134 {
135# ifdef UNIX
136 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
137# else
138 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
139# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000140 int len;
141 garray_T ga;
142 int mustfree;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200143 char_u *item;
144
145 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
147 ga_init2(&ga, 1, 100);
K.Takataeeec2542021-06-02 13:28:16 +0200148 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000150 mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# ifdef UNIX
152 if (*names[n] == NUL)
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200153# ifdef MACOS_X
154 p = (char_u *)"/private/tmp";
155# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 p = (char_u *)"/tmp";
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 else
159# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000160 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (p != NULL && *p != NUL)
162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100163 // First time count the NUL, otherwise count the ','.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000164 len = (int)STRLEN(p) + 3;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200165 item = alloc(len);
166 STRCPY(item, p);
167 add_pathsep(item);
168 STRCAT(item, "*");
169 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
170 == NULL
171 && ga_grow(&ga, len) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 if (ga.ga_len > 0)
174 STRCAT(ga.ga_data, ",");
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200175 STRCAT(ga.ga_data, item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 ga.ga_len += len;
177 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200178 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 if (mustfree)
181 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 }
183 if (ga.ga_data != NULL)
184 {
185 set_string_default("bsk", ga.ga_data);
186 vim_free(ga.ga_data);
187 }
188 }
189#endif
190
191 /*
192 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
193 */
194 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000195 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000197#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
198 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
199#endif
200 {
ichizok02560422022-04-05 14:18:44 +0100201#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100202 // Use amount of memory available at this moment.
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200203 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100204#elif defined(HAVE_TOTAL_MEM)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100205 // Use amount of memory available to Vim.
Bram Moolenaar914572a2007-05-01 11:37:47 +0000206 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100207#else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000208 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000211 opt_idx = findoption((char_u *)"maxmem");
212 if (opt_idx >= 0)
213 {
214#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100215 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
216 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000217#endif
218 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
219 }
220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 }
222
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#ifdef FEAT_SEARCHPATH
224 {
225 char_u *cdpath;
226 char_u *buf;
227 int i;
228 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000229 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100231 // Initialize the 'cdpath' option's default value.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000232 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 if (cdpath != NULL)
234 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200235 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 if (buf != NULL)
237 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100238 buf[0] = ','; // start with ",", current dir first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 j = 1;
240 for (i = 0; cdpath[i] != NUL; ++i)
241 {
242 if (vim_ispathlistsep(cdpath[i]))
243 buf[j++] = ',';
244 else
245 {
246 if (cdpath[i] == ' ' || cdpath[i] == ',')
247 buf[j++] = '\\';
248 buf[j++] = cdpath[i];
249 }
250 }
251 buf[j] = NUL;
252 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000253 if (opt_idx >= 0)
254 {
255 options[opt_idx].def_val[VI_DEFAULT] = buf;
256 options[opt_idx].flags |= P_DEF_ALLOCED;
257 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200258 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100259 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000261 if (mustfree)
262 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 }
264 }
265#endif
266
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#ifdef FEAT_CINDENT
662 parse_cino(curbuf);
663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664}
665
666/*
667 * Set the Vi-default value of a string option.
668 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100669 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100671 static void
672set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
674 char_u *p;
675 int opt_idx;
676
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100677 if (escape && vim_strchr(val, ' ') != NULL)
678 p = vim_strsave_escaped(val, (char_u *)" ");
679 else
680 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100681 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
683 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000684 if (opt_idx >= 0)
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;
690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
692}
693
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100694 void
695set_string_default(char *name, char_u *val)
696{
697 set_string_default_esc(name, val, FALSE);
698}
699
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200701 * For an option value that contains comma separated items, find "newval" in
702 * "origval". Return NULL if not found.
703 */
704 static char_u *
705find_dup_item(char_u *origval, char_u *newval, long_u flags)
706{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200707 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200708 size_t newlen;
709 char_u *s;
710
711 if (origval == NULL)
712 return NULL;
713
714 newlen = STRLEN(newval);
715 for (s = origval; *s != NUL; ++s)
716 {
717 if ((!(flags & P_COMMA)
718 || s == origval
719 || (s[-1] == ',' && !(bs & 1)))
720 && STRNCMP(s, newval, newlen) == 0
721 && (!(flags & P_COMMA)
722 || s[newlen] == ','
723 || s[newlen] == NUL))
724 return s;
725 // Count backslashes. Only a comma with an even number of backslashes
726 // or a single backslash preceded by a comma before it is recognized as
727 // a separator.
728 if ((s > origval + 1
729 && s[-1] == '\\'
730 && s[-2] != ',')
731 || (s == origval + 1
732 && s[-1] == '\\'))
733 ++bs;
734 else
735 bs = 0;
736 }
737 return NULL;
738}
739
740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 * Set the Vi-default value of a number option.
742 * Used for 'lines' and 'columns'.
743 */
744 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100745set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000747 int opt_idx;
748
749 opt_idx = findoption((char_u *)name);
750 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000751 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752}
753
Dominique Pelle748b3082022-01-08 12:41:16 +0000754#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200755/*
756 * Set all window-local and buffer-local options to the Vim default.
757 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200758 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200759 */
760 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200761set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200762{
763 win_T *save_curwin = curwin;
764 int i;
765
766 curwin = wp;
767 curbuf = curwin->w_buffer;
768 block_autocmds();
769
Bram Moolenaardac13472019-09-16 21:06:21 +0200770 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200771 {
772 struct vimoption *p = &(options[i]);
773 char_u *varp = get_varp_scope(p, OPT_LOCAL);
774
775 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200776 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200777 && !(options[i].flags & P_NODEFAULT)
778 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200779 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200780 }
781
782 unblock_autocmds();
783 curwin = save_curwin;
784 curbuf = curwin->w_buffer;
785}
Dominique Pelle748b3082022-01-08 12:41:16 +0000786#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200787
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000788#if defined(EXITFREE) || defined(PROTO)
789/*
790 * Free all options.
791 */
792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100793free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000794{
795 int i;
796
Bram Moolenaardac13472019-09-16 21:06:21 +0200797 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000798 {
799 if (options[i].indir == PV_NONE)
800 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100801 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100802 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000803 free_string_option(*(char_u **)options[i].var);
804 if (options[i].flags & P_DEF_ALLOCED)
805 free_string_option(options[i].def_val[VI_DEFAULT]);
806 }
807 else if (options[i].var != VAR_WIN
808 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100809 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200810 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000811 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000812 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000813 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000814}
815#endif
816
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818/*
819 * Initialize the options, part two: After getting Rows and Columns and
820 * setting 'term'.
821 */
822 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100823set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000825 int idx;
826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100828 * 'scroll' defaults to half the window height. The stored default is zero,
829 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000831 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000832 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000833 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 comp_col();
835
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000836 /*
837 * 'window' is only for backwards compatibility with Vi.
838 * Default is Rows - 1.
839 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000840 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000841 p_window = Rows - 1;
842 set_number_default("window", Rows - 1);
843
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100844 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100845#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000846 /*
847 * If 'background' wasn't set by the user, try guessing the value,
848 * depending on the terminal name. Only need to check for terminals
849 * with a dark background, that can handle color.
850 */
851 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000852 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
853 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000855 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 // don't mark it as set, when starting the GUI it may be
857 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000858 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
860#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000861
862#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100863 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000864#endif
865#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100866 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000867#endif
868#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100869 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871}
872
873/*
874 * Initialize the options, part three: After reading the .vimrc
875 */
876 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100877set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100879#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880/*
881 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
882 * This is done after other initializations, where 'shell' might have been
883 * set, but only if they have not been set before.
884 */
885 char_u *p;
886 int idx_srr;
887 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100888# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 int idx_sp;
890 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
893 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000894 if (idx_srr < 0)
895 do_srr = FALSE;
896 else
897 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100898# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000900 if (idx_sp < 0)
901 do_sp = FALSE;
902 else
903 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100904# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200905 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (p != NULL)
907 {
908 /*
909 * Default for p_sp is "| tee", for p_srr is ">".
910 * For known shells it is changed here to include stderr.
911 */
912 if ( fnamecmp(p, "csh") == 0
913 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100914# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 || fnamecmp(p, "csh.exe") == 0
916 || fnamecmp(p, "tcsh.exe") == 0
917# endif
918 )
919 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100920# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (do_sp)
922 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100923# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100925# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
929 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100930# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (do_srr)
932 {
933 p_srr = (char_u *)">&";
934 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
935 }
936 }
Mike Williams12795022021-06-28 20:53:58 +0200937# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200938 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
939 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200940 else if ( fnamecmp(p, "powershell") == 0
941 || fnamecmp(p, "powershell.exe") == 0
942 )
943 {
944# if defined(FEAT_QUICKFIX)
945 if (do_sp)
946 {
947 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
948 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
949 }
950# endif
951 if (do_srr)
952 {
953 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
954 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
955 }
956 }
957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 else
Natanael Copa56318362021-05-06 18:46:35 +0200959 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 if ( fnamecmp(p, "sh") == 0
961 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200962 || fnamecmp(p, "mksh") == 0
963 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000965 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200967 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200968 || fnamecmp(p, "ash") == 0
969 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200970 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100971# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 || fnamecmp(p, "cmd") == 0
973 || fnamecmp(p, "sh.exe") == 0
974 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200975 || fnamecmp(p, "mksh.exe") == 0
976 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000978 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 || fnamecmp(p, "bash.exe") == 0
980 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200981 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200982 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100984 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100986# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if (do_sp)
988 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100989# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100991# else
Mike Williamsa3d1b292021-06-30 20:56:00 +0200992 if (fnamecmp(p, "pwsh") == 0)
993 p_sp = (char_u *)">%s 2>&1";
994 else
995 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
998 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if (do_srr)
1001 {
1002 p_srr = (char_u *)">%s 2>&1";
1003 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1004 }
1005 }
1006 vim_free(p);
1007 }
1008#endif
1009
Bram Moolenaar4f974752019-02-17 17:44:42 +01001010#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001012 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1013 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001015 * set, but only if they have not been set before.
1016 * Default values depend on shell (cmd.exe is default shell):
1017 *
1018 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001019 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001020 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001021 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001022 * "sh" like shells - "-c" "\""
1023 *
1024 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 */
Mike Williams12795022021-06-28 20:53:58 +02001026 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1027 {
1028 int idx_opt;
1029
1030 idx_opt = findoption((char_u *)"shcf");
1031 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1032 {
1033 p_shcf = (char_u*)"-Command";
1034 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1035 }
1036
1037 idx_opt = findoption((char_u *)"sxq");
1038 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1039 {
1040 p_sxq = (char_u*)"\"";
1041 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1042 }
1043 }
1044 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
1046 int idx3;
1047
1048 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001049 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
1051 p_shcf = (char_u *)"-c";
1052 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1053 }
1054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001055 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001057 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 {
1059 p_sxq = (char_u *)"\"";
1060 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001063 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1064 {
1065 int idx3;
1066
1067 /*
1068 * cmd.exe on Windows will strip the first and last double quote given
1069 * on the command line, e.g. most of the time things like:
1070 * cmd /c "my path/to/echo" "my args to echo"
1071 * become:
1072 * my path/to/echo" "my args to echo
1073 * when executed.
1074 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001075 * To avoid this, set shellxquote to surround the command in
1076 * parenthesis. This appears to make most commands work, without
1077 * breaking commands that worked previously, such as
1078 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001079 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001080 idx3 = findoption((char_u *)"sxq");
1081 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1082 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001083 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001084 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1085 }
1086
Bram Moolenaara64ba222012-02-12 23:23:31 +01001087 idx3 = findoption((char_u *)"shcf");
1088 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1089 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001090 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001091 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1092 }
1093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094#endif
1095
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001096 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001097 {
1098 int idx_ffs = findoption((char_u *)"ffs");
1099
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001100 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001101 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1102 set_fileformat(default_fileformat(), OPT_LOCAL);
1103 }
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106}
1107
1108#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1109/*
1110 * When 'helplang' is still at its default value, set it to "lang".
1111 * Only the first two characters of "lang" are used.
1112 */
1113 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001114set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
1116 int idx;
1117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001118 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 return;
1120 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001121 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
1123 if (options[idx].flags & P_ALLOCED)
1124 free_string_option(p_hlg);
1125 p_hlg = vim_strsave(lang);
1126 if (p_hlg == NULL)
1127 p_hlg = empty_option;
1128 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001129 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001130 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001131 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1132 {
1133 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1134 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1135 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001136 // any C like setting, such as C.UTF-8, becomes "en"
1137 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1138 {
1139 p_hlg[0] = 'e';
1140 p_hlg[1] = 'n';
1141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 options[idx].flags |= P_ALLOCED;
1145 }
1146}
1147#endif
1148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149/*
1150 * 'title' and 'icon' only default to true if they have not been set or reset
1151 * in .vimrc and we can read the old value.
1152 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1153 * they can be reset. This reduces startup time when using X on a remote
1154 * machine.
1155 */
1156 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001157set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
1159 int idx1;
1160 long val;
1161
1162 /*
1163 * If GUI is (going to be) used, we can always set the window title and
1164 * icon name. Saves a bit of time, because the X11 display server does
1165 * not need to be contacted.
1166 */
1167 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001168 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
1170#ifdef FEAT_GUI
1171 if (gui.starting || gui.in_use)
1172 val = TRUE;
1173 else
1174#endif
1175 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001176 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 p_title = val;
1178 }
1179 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001180 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
1182#ifdef FEAT_GUI
1183 if (gui.starting || gui.in_use)
1184 val = TRUE;
1185 else
1186#endif
1187 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001188 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 p_icon = val;
1190 }
1191}
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 Moolenaar071d4272004-06-13 20:20:40 +00001214/*
1215 * Parse 'arg' for option settings.
1216 *
1217 * 'arg' may be IObuff, but only when no errors can be present and option
1218 * does not need to be expanded with option_expand().
1219 * "opt_flags":
1220 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001221 * OPT_GLOBAL for ":setglobal"
1222 * OPT_LOCAL for ":setlocal" and a modeline
1223 * OPT_MODELINE for a modeline
1224 * OPT_WINONLY to only set window-local options
1225 * OPT_NOWIN to skip setting window-local options
1226 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 *
1228 * returns FAIL if an error is detected, OK otherwise
1229 */
1230 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001231do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02001232 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001233 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234{
Bram Moolenaar1594f312021-07-08 16:40:13 +02001235 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001237 char *errmsg;
1238 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1241 int nextchar; // next non-white char after option name
1242 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 int len;
1244 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001245 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001247 long_u flags; // flags for current option
1248 char_u *varp = NULL; // pointer to variable for current option
1249 int did_show = FALSE; // already showed one value
1250 int adding; // "opt+=arg"
1251 int prepending; // "opt^=arg"
1252 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 int cp_val = 0;
1254 char_u key_name[2];
1255
1256 if (*arg == NUL)
1257 {
1258 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001259 did_show = TRUE;
1260 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001263 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
1265 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001266 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001268 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1269 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 {
1271 /*
1272 * ":set all" show all options.
1273 * ":set all&" set all options to their default value.
1274 */
1275 arg += 3;
1276 if (*arg == '&')
1277 {
1278 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001279 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001281 didset_options();
1282 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001283 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
1285 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001288 did_show = TRUE;
1289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001291 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {
1293 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001294 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001295 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 arg += 7;
1297 }
1298 else
1299 {
1300 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001301 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
1303 prefix = 0;
1304 arg += 2;
1305 }
1306 else if (STRNCMP(arg, "inv", 3) == 0)
1307 {
1308 prefix = 2;
1309 arg += 3;
1310 }
1311
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001312 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 key = 0;
1314 if (*arg == '<')
1315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001317 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1319 len = 5;
1320 else
1321 {
1322 len = 1;
1323 while (arg[len] != NUL && arg[len] != '>')
1324 ++len;
1325 }
1326 if (arg[len] != '>')
1327 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001328 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 goto skip;
1330 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001331 arg[len] = NUL; // put NUL after name
1332 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001334 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001336 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338 else
1339 {
1340 len = 0;
1341 /*
1342 * The two characters after "t_" may not be alphanumeric.
1343 */
1344 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1345 len = 4;
1346 else
1347 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1348 ++len;
1349 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001350 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001352 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001354 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001357 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 afterchar = arg[len];
1359
Bram Moolenaardeb108b2021-07-08 17:35:36 +02001360 if (in_vim9script())
1361 {
1362 char_u *p = skipwhite(arg + len);
1363
1364 // disallow white space before =val, +=val, -=val, ^=val
1365 if (p > arg + len && (p[0] == '='
1366 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1367 && p[1] == '=')))
1368 {
1369 errmsg = e_no_white_space_allowed_between_option_and;
1370 arg = p;
1371 startarg = p;
1372 goto skip;
1373 }
1374 }
1375 else
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001376 // skip white space, allow ":set ai ?", ":set hlsearch !"
1377 while (VIM_ISWHITE(arg[len]))
1378 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
1380 adding = FALSE;
1381 prepending = FALSE;
1382 removing = FALSE;
1383 if (arg[len] != NUL && arg[len + 1] == '=')
1384 {
1385 if (arg[len] == '+')
1386 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001387 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 ++len;
1389 }
1390 else if (arg[len] == '^')
1391 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001392 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 ++len;
1394 }
1395 else if (arg[len] == '-')
1396 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001397 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 ++len;
1399 }
1400 }
1401 nextchar = arg[len];
1402
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001403 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 {
Bram Moolenaar1594f312021-07-08 16:40:13 +02001405 if (in_vim9script() && arg > arg_start
1406 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1407 errmsg = e_no_white_space_allowed_between_option_and;
1408 else
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001409 errmsg = e_unknown_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 goto skip;
1411 }
1412
1413 if (opt_idx >= 0)
1414 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001415 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001417 // Only give an error message when requesting the value of
1418 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1420 && (!(options[opt_idx].flags & P_BOOL)
1421 || nextchar == '?'))
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001422 errmsg = e_option_not_supported;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 goto skip;
1424 }
1425
1426 flags = options[opt_idx].flags;
1427 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1428 }
1429 else
1430 {
1431 flags = P_STRING;
1432 if (key < 0)
1433 {
1434 key_name[0] = KEY2TERMCAP0(key);
1435 key_name[1] = KEY2TERMCAP1(key);
1436 }
1437 else
1438 {
1439 key_name[0] = KS_KEY;
1440 key_name[1] = (key & 0xff);
1441 }
1442 }
1443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001444 // Skip all options that are not window-local (used when showing
1445 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001446 if ((opt_flags & OPT_WINONLY)
1447 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1448 goto skip;
1449
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001450 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001451 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1452 && options[opt_idx].var == VAR_WIN)
1453 goto skip;
1454
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001455 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001456 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001458 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001459 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001460 errmsg = e_not_allowed_in_modeline;
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001461 goto skip;
1462 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001463 if ((flags & P_MLE) && !p_mle)
1464 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001465 errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
Bram Moolenaar110289e2019-05-23 15:38:06 +02001466 goto skip;
1467 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001468#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001469 // In diff mode some options are overruled. This avoids that
1470 // 'foldmethod' becomes "marker" instead of "diff" and that
1471 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001472 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001473 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001474 && (
1475#ifdef FEAT_FOLDING
1476 options[opt_idx].indir == PV_FDM ||
1477#endif
1478 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001479 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 }
1482
1483#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001484 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001485 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001487 errmsg = e_not_allowed_in_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 goto skip;
1489 }
1490#endif
1491
1492 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1493 {
1494 arg += len;
1495 cp_val = p_cp;
1496 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1497 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001498 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
1500 cp_val = FALSE;
1501 arg += 3;
1502 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001503 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 {
1505 cp_val = TRUE;
1506 arg += 2;
1507 }
1508 }
1509 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001510 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001512 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 goto skip;
1514 }
1515 }
1516
1517 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001518 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001519 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 */
1521 if (nextchar == '?'
1522 || (prefix == 1
1523 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1524 && !(flags & P_BOOL)))
1525 {
1526 /*
1527 * print value
1528 */
1529 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001530 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 else
1532 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001533 gotocmdline(TRUE); // cursor at status line
1534 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
1536 if (opt_idx >= 0)
1537 {
1538 showoneopt(&options[opt_idx], opt_flags);
1539#ifdef FEAT_EVAL
1540 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001541 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001542 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001543 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001544 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001545 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001546 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001547 (int)options[opt_idx].indir & PV_MASK]);
1548 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001549 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001550 (int)options[opt_idx].indir & PV_MASK]);
1551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552#endif
1553 }
1554 else
1555 {
1556 char_u *p;
1557
1558 p = find_termcode(key_name);
1559 if (p == NULL)
1560 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001561 errmsg = e_key_code_not_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 goto skip;
1563 }
1564 else
1565 (void)show_one_termcode(key_name, p, TRUE);
1566 }
1567 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001568 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001569 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 }
1571 else
1572 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001573 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001574 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001575
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001576 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 {
1578 if (nextchar == '=' || nextchar == ':')
1579 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001580 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 goto skip;
1582 }
1583
1584 /*
1585 * ":set opt!": invert
1586 * ":set opt&": reset to default value
1587 * ":set opt<": reset to global value
1588 */
1589 if (nextchar == '!')
1590 value = *(int *)(varp) ^ 1;
1591 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001592 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 ((flags & P_VI_DEF) || cp_val)
1594 ? VI_DEFAULT : VIM_DEFAULT];
1595 else if (nextchar == '<')
1596 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001597 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if ((int *)varp == &curbuf->b_p_ar
1599 && opt_flags == OPT_LOCAL)
1600 value = -1;
1601 else
1602 value = *(int *)get_varp_scope(&(options[opt_idx]),
1603 OPT_GLOBAL);
1604 }
1605 else
1606 {
1607 /*
1608 * ":set invopt": invert
1609 * ":set opt" or ":set noopt": set or reset
1610 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001611 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001613 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 goto skip;
1615 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001616 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 value = *(int *)(varp) ^ 1;
1618 else
1619 value = prefix;
1620 }
1621
1622 errmsg = set_bool_option(opt_idx, varp, (int)value,
1623 opt_flags);
1624 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001625 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
1627 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1628 || prefix != 1)
1629 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001630 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 goto skip;
1632 }
1633
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001634 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
1636 /*
1637 * Different ways to set a number option:
1638 * & set to default value
1639 * < set to global value
1640 * <xx> accept special key codes for 'wildchar'
1641 * c accept any non-digit for 'wildchar'
1642 * [-]0-9 set number
1643 * other error
1644 */
1645 ++arg;
1646 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001647 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 ((flags & P_VI_DEF) || cp_val)
1649 ? VI_DEFAULT : VIM_DEFAULT];
1650 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001651 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001652 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1653 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001654 if ((long *)varp == &curbuf->b_p_ul
1655 && opt_flags == OPT_LOCAL)
1656 value = NO_LOCAL_UNDOLEVEL;
1657 else
1658 value = *(long *)get_varp_scope(
1659 &(options[opt_idx]), OPT_GLOBAL);
1660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 else if (((long *)varp == &p_wc
1662 || (long *)varp == &p_wcm)
1663 && (*arg == '<'
1664 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001665 || (*arg != NUL
1666 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 && !VIM_ISDIGIT(*arg))))
1668 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001669 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if (value == 0 && (long *)varp != &p_wcm)
1671 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001672 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 goto skip;
1674 }
1675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1677 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001678 // Allow negative (for 'undolevels'), octal and
1679 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001680 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001681 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001682 if (i == 0 || (arg[i] != NUL
1683 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001685 errmsg = e_number_required_after_equal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 goto skip;
1687 }
1688 }
1689 else
1690 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001691 errmsg = e_number_required_after_equal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 goto skip;
1693 }
1694
1695 if (adding)
1696 value = *(long *)varp + value;
1697 if (prepending)
1698 value = *(long *)varp * value;
1699 if (removing)
1700 value = *(long *)varp - value;
1701 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001702 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001704 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001706 char_u *save_arg = NULL;
1707 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001708 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001709 char_u *newval;
1710 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001711 char_u *origval_l = NULL;
1712 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001713#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001714 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001715 char_u *saved_origval_l = NULL;
1716 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001717 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001718#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001719 unsigned newlen;
1720 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001721 int new_value_alloced; // new string option
1722 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001724 // When using ":set opt=val" for a global option
1725 // with a local value the local value will be
1726 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001728 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 varp = options[opt_idx].var;
1730
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001731 // The old value is kept until we are sure that the
1732 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001734
Bram Moolenaard7c96872019-06-15 17:12:48 +02001735 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1736 {
1737 origval_l = *(char_u **)get_varp_scope(
1738 &(options[opt_idx]), OPT_LOCAL);
1739 origval_g = *(char_u **)get_varp_scope(
1740 &(options[opt_idx]), OPT_GLOBAL);
1741
1742 // A global-local string option might have an empty
1743 // option as value to indicate that the global
1744 // value should be used.
1745 if (((int)options[opt_idx].indir & PV_BOTH)
1746 && origval_l == empty_option)
1747 origval_l = origval_g;
1748 }
1749
1750 // When setting the local value of a global
1751 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001752 if (((int)options[opt_idx].indir & PV_BOTH)
1753 && (opt_flags & OPT_LOCAL))
1754 origval = *(char_u **)get_varp(
1755 &options[opt_idx]);
1756 else
1757 origval = oldval;
1758
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001759 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 {
1761 newval = options[opt_idx].def_val[
1762 ((flags & P_VI_DEF) || cp_val)
1763 ? VI_DEFAULT : VIM_DEFAULT];
1764 if ((char_u **)varp == &p_bg)
1765 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001766 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#ifdef FEAT_GUI
1768 if (gui.in_use)
1769 newval = gui_bg_default();
1770 else
1771#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001772 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001774 else if ((char_u **)varp == &p_fencs && enc_utf8)
1775 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001777 // expand environment variables and ~ since the
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001778 // default value was already expanded, only
1779 // required when an environment variable was set
1780 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 if (newval == NULL)
1782 newval = empty_option;
1783 else
1784 {
1785 s = option_expand(opt_idx, newval);
1786 if (s == NULL)
1787 s = newval;
1788 newval = vim_strsave(s);
1789 }
1790 new_value_alloced = TRUE;
1791 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001792 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 {
1794 newval = vim_strsave(*(char_u **)get_varp_scope(
1795 &(options[opt_idx]), OPT_GLOBAL));
1796 new_value_alloced = TRUE;
1797 }
1798 else
1799 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001800 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 /*
1803 * Set 'keywordprg' to ":help" if an empty
1804 * value was passed to :set by the user.
1805 * Misuse errbuf[] for the resulting string.
1806 */
1807 if (varp == (char_u *)&p_kp
1808 && (*arg == NUL || *arg == ' '))
1809 {
1810 STRCPY(errbuf, ":help");
1811 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001812 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
1814 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001815 * Convert 'backspace' number to string, for
1816 * adding, prepending and removing string.
1817 */
1818 else if (varp == (char_u *)&p_bs
1819 && VIM_ISDIGIT(**(char_u **)varp))
1820 {
1821 i = getdigits((char_u **)varp);
1822 switch (i)
1823 {
1824 case 0:
1825 *(char_u **)varp = empty_option;
1826 break;
1827 case 1:
1828 *(char_u **)varp = vim_strsave(
1829 (char_u *)"indent,eol");
1830 break;
1831 case 2:
1832 *(char_u **)varp = vim_strsave(
1833 (char_u *)"indent,eol,start");
1834 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001835 case 3:
1836 *(char_u **)varp = vim_strsave(
1837 (char_u *)"indent,eol,nostop");
1838 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001839 }
1840 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001841 if (origval == oldval)
1842 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001843 if (origval_l == oldval)
1844 origval_l = *(char_u **)varp;
1845 if (origval_g == oldval)
1846 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001847 oldval = *(char_u **)varp;
1848 }
1849 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 * Convert 'whichwrap' number to string, for
1851 * backwards compatibility with Vim 3.0.
1852 * Misuse errbuf[] for the resulting string.
1853 */
1854 else if (varp == (char_u *)&p_ww
1855 && VIM_ISDIGIT(*arg))
1856 {
1857 *errbuf = NUL;
1858 i = getdigits(&arg);
1859 if (i & 1)
1860 STRCAT(errbuf, "b,");
1861 if (i & 2)
1862 STRCAT(errbuf, "s,");
1863 if (i & 4)
1864 STRCAT(errbuf, "h,l,");
1865 if (i & 8)
1866 STRCAT(errbuf, "<,>,");
1867 if (i & 16)
1868 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001869 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 errbuf[STRLEN(errbuf) - 1] = NUL;
1871 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001872 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 }
1874 /*
1875 * Remove '>' before 'dir' and 'bdir', for
1876 * backwards compatibility with version 3.0
1877 */
1878 else if ( *arg == '>'
1879 && (varp == (char_u *)&p_dir
1880 || varp == (char_u *)&p_bdir))
1881 {
1882 ++arg;
1883 }
1884
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 /*
1886 * Copy the new string into allocated memory.
1887 * Can't use set_string_option_direct(), because
1888 * we need to remove the backslashes.
1889 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001890 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 newlen = (unsigned)STRLEN(arg) + 1;
1892 if (adding || prepending || removing)
1893 newlen += (unsigned)STRLEN(origval) + 1;
1894 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001895 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 break;
1897 s = newval;
1898
1899 /*
1900 * Copy the string, skip over escaped chars.
1901 * For MS-DOS and WIN32 backslashes before normal
1902 * file name characters are not removed, and keep
1903 * backslash at start, for "\\machine\path", but
1904 * do remove it for "\\\\machine\\path".
1905 * The reverse is found in ExpandOldSetting().
1906 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001907 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
1909 if (*arg == '\\' && arg[1] != NUL
1910#ifdef BACKSLASH_IN_FILENAME
1911 && !((flags & P_EXPAND)
1912 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001913 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 && (arg[1] != '\\'
1915 || (s == newval
1916 && arg[2] != '\\')))
1917#endif
1918 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001919 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001921 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001923 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 mch_memmove(s, arg, (size_t)i);
1925 arg += i;
1926 s += i;
1927 }
1928 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 *s++ = *arg++;
1930 }
1931 *s = NUL;
1932
1933 /*
1934 * Expand environment variables and ~.
1935 * Don't do it when adding without inserting a
1936 * comma.
1937 */
1938 if (!(adding || prepending || removing)
1939 || (flags & P_COMMA))
1940 {
1941 s = option_expand(opt_idx, newval);
1942 if (s != NULL)
1943 {
1944 vim_free(newval);
1945 newlen = (unsigned)STRLEN(s) + 1;
1946 if (adding || prepending || removing)
1947 newlen += (unsigned)STRLEN(origval) + 1;
1948 newval = alloc(newlen);
1949 if (newval == NULL)
1950 break;
1951 STRCPY(newval, s);
1952 }
1953 }
1954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001955 // locate newval[] in origval[] when removing it
1956 // and when adding to avoid duplicates
1957 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if (removing || (flags & P_NODUP))
1959 {
1960 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001961 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001963 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001964 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 {
1966 prepending = FALSE;
1967 adding = FALSE;
1968 STRCPY(newval, origval);
1969 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001970
1971 // if no duplicate, move pointer to end of
1972 // original value
1973 if (s == NULL)
1974 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
1976
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001977 // concatenate the two strings; add a ',' if
1978 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 if (adding || prepending)
1980 {
1981 comma = ((flags & P_COMMA) && *origval != NUL
1982 && *newval != NUL);
1983 if (adding)
1984 {
1985 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001986 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001987 if (comma && i > 1
1988 && (flags & P_ONECOMMA) == P_ONECOMMA
1989 && origval[i - 1] == ','
1990 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001991 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 mch_memmove(newval + i + comma, newval,
1993 STRLEN(newval) + 1);
1994 mch_memmove(newval, origval, (size_t)i);
1995 }
1996 else
1997 {
1998 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001999 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 }
2001 if (comma)
2002 newval[i] = ',';
2003 }
2004
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002005 // Remove newval[] from origval[]. (Note: "i" has
2006 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 if (removing)
2008 {
2009 STRCPY(newval, origval);
2010 if (*s)
2011 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002012 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if (flags & P_COMMA)
2014 {
2015 if (s == origval)
2016 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002017 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 if (s[i] == ',')
2019 ++i;
2020 }
2021 else
2022 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002023 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 --s;
2025 ++i;
2026 }
2027 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002028 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
2030 }
2031
2032 if (flags & P_FLAGLIST)
2033 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002034 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002035 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002036 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002037 // if options have P_FLAGLIST and
2038 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002039 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002041 if (*s != ',' && *(s + 1) == ','
2042 && vim_strchr(s + 2, *s) != NULL)
2043 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002044 // Remove the duplicated value and
2045 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002046 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002047 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002050 else
2051 {
2052 if ((!(flags & P_COMMA) || *s != ',')
2053 && vim_strchr(s + 1, *s) != NULL)
2054 {
2055 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002056 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002057 }
2058 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002059 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 }
2062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002063 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 arg = save_arg;
2065 new_value_alloced = TRUE;
2066 }
2067
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002068 /*
2069 * Set the new value.
2070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 *(char_u **)(varp) = newval;
2072
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002073#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002074 if (!starting
2075# ifdef FEAT_CRYPT
2076 && options[opt_idx].indir != PV_KEY
2077# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002078 && origval != NULL && newval != NULL)
2079 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002080 // origval may be freed by
2081 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002082 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002083 // newval (and varp) may become invalid if the
2084 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002085 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002086 if (origval_l != NULL)
2087 saved_origval_l = vim_strsave(origval_l);
2088 if (origval_g != NULL)
2089 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002090 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002091#endif
2092
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002093 {
2094 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002095 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002096
2097 // When an option is set in the sandbox, from a
2098 // modeline or in secure mode, then deal with side
2099 // effects in secure mode. Also when the value was
2100 // set with the P_INSECURE flag and is not
2101 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002102 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002103#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002104 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002105#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002106 || (!value_is_replaced && (*p & P_INSECURE)))
2107 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002108
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002109 // Handle side effects, and set the global value
2110 // for ":set" on local options. Note: when setting
2111 // 'syntax' or 'filetype' autocommands may be
2112 // triggered that can cause havoc.
2113 errmsg = did_set_string_option(
2114 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002115 new_value_alloced, oldval, errbuf,
2116 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002117
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002118 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002121#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002122 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002123 trigger_optionsset_string(
2124 opt_idx, opt_flags, saved_origval,
2125 saved_origval_l, saved_origval_g,
2126 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002127 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002128 vim_free(saved_origval_l);
2129 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002130 vim_free(saved_newval);
2131#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002132 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if (errmsg != NULL)
2134 goto skip;
2135 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002136 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
2138 char_u *p;
2139
2140 if (nextchar == '&')
2141 {
2142 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002143 errmsg = e_not_found_in_termcap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 }
2145 else
2146 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002147 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002148 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 if (*p == '\\' && p[1] != NUL)
2150 ++p;
2151 nextchar = *p;
2152 *p = NUL;
2153 add_termcode(key_name, arg, FALSE);
2154 *p = nextchar;
2155 }
2156 if (full_screen)
2157 ttest(FALSE);
2158 redraw_all_later(CLEAR);
2159 }
2160 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002161
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002163 did_set_option(
2164 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166
2167skip:
2168 /*
2169 * Advance to next argument.
2170 * - skip until a blank found, taking care of backslashes
2171 * - skip blanks
2172 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2173 */
2174 for (i = 0; i < 2 ; ++i)
2175 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002176 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 if (*arg++ == '\\' && *arg != NUL)
2178 ++arg;
2179 arg = skipwhite(arg);
2180 if (*arg != '=')
2181 break;
2182 }
2183 }
2184
2185 if (errmsg != NULL)
2186 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002187 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002188 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 if (i + (arg - startarg) < IOSIZE)
2190 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002191 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 STRCAT(IObuff, ": ");
2193 mch_memmove(IObuff + i, startarg, (arg - startarg));
2194 IObuff[i + (arg - startarg)] = NUL;
2195 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002196 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 trans_characters(IObuff, IOSIZE);
2198
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002199 ++no_wait_return; // wait_return done later
2200 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 --no_wait_return;
2202
2203 return FAIL;
2204 }
2205
2206 arg = skipwhite(arg);
2207 }
2208
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002209theend:
2210 if (silent_mode && did_show)
2211 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002212 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002213 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002214 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002215 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002216 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002217 out_flush();
2218 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002219 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002220 }
2221
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 return OK;
2223}
2224
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002225/*
2226 * Call this when an option has been given a new value through a user command.
2227 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2228 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002229 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002230did_set_option(
2231 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002232 int opt_flags, // possibly with OPT_MODELINE
2233 int new_value, // value was replaced completely
2234 int value_checked) // value was checked to be safe, no need to set the
2235 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002236{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002237 long_u *p;
2238
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002239 options[opt_idx].flags |= P_WAS_SET;
2240
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002241 // When an option is set in the sandbox, from a modeline or in secure mode
2242 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2243 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002244 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002245 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002246#ifdef HAVE_SANDBOX
2247 || sandbox != 0
2248#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002249 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002250 *p = *p | P_INSECURE;
2251 else if (new_value)
2252 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002253}
2254
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255/*
2256 * Convert a key name or string into a key value.
2257 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002258 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002260 int
2261string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262{
2263 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002264 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (*arg == '^')
2266 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002267 if (multi_byte)
2268 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 return *arg;
2270}
2271
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272/*
2273 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2274 * maketitle() to create and display it.
2275 * When switching the title or icon off, call mch_restore_title() to get
2276 * the old value back.
2277 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002278 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002279did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
2281 if (starting != NO_SCREEN
2282#ifdef FEAT_GUI
2283 && !gui.starting
2284#endif
2285 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
2289/*
2290 * set_options_bin - called when 'bin' changes value.
2291 */
2292 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002293set_options_bin(
2294 int oldval,
2295 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002296 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
2298 /*
2299 * The option values that are changed when 'bin' changes are
2300 * copied when 'bin is set and restored when 'bin' is reset.
2301 */
2302 if (newval)
2303 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002304 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 if (!(opt_flags & OPT_GLOBAL))
2307 {
2308 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2309 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2310 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2311 curbuf->b_p_et_nobin = curbuf->b_p_et;
2312 }
2313 if (!(opt_flags & OPT_LOCAL))
2314 {
2315 p_tw_nobin = p_tw;
2316 p_wm_nobin = p_wm;
2317 p_ml_nobin = p_ml;
2318 p_et_nobin = p_et;
2319 }
2320 }
2321
2322 if (!(opt_flags & OPT_GLOBAL))
2323 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002324 curbuf->b_p_tw = 0; // no automatic line wrap
2325 curbuf->b_p_wm = 0; // no automatic line wrap
2326 curbuf->b_p_ml = 0; // no modelines
2327 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329 if (!(opt_flags & OPT_LOCAL))
2330 {
2331 p_tw = 0;
2332 p_wm = 0;
2333 p_ml = FALSE;
2334 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002335 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
2337 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002338 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
2340 if (!(opt_flags & OPT_GLOBAL))
2341 {
2342 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2343 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2344 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2345 curbuf->b_p_et = curbuf->b_p_et_nobin;
2346 }
2347 if (!(opt_flags & OPT_LOCAL))
2348 {
2349 p_tw = p_tw_nobin;
2350 p_wm = p_wm_nobin;
2351 p_ml = p_ml_nobin;
2352 p_et = p_et_nobin;
2353 }
2354 }
2355}
2356
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357/*
2358 * Expand environment variables for some string options.
2359 * These string options cannot be indirect!
2360 * If "val" is NULL expand the current value of the option.
2361 * Return pointer to NameBuff, or NULL when not expanded.
2362 */
2363 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002364option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002366 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2368 return NULL;
2369
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002370 // If val is longer than MAXPATHL no meaningful expansion can be done,
2371 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (val != NULL && STRLEN(val) > MAXPATHL)
2373 return NULL;
2374
2375 if (val == NULL)
2376 val = *(char_u **)options[opt_idx].var;
2377
2378 /*
2379 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2380 * Escape spaces when expanding 'tags', they are used to separate file
2381 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002382 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 */
2384 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002385 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002386#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002387 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2388#endif
2389 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002390 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 return NULL;
2392
2393 return NameBuff;
2394}
2395
2396/*
2397 * After setting various option values: recompute variables that depend on
2398 * option values.
2399 */
2400 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002401didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002403 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 (void)init_chartab();
2405
Bram Moolenaardac13472019-09-16 21:06:21 +02002406 didset_string_options();
2407
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002408#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002409 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002410 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002411 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002412 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002415 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 (void)check_cedit();
2417#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002418#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002419 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002420 fill_breakat_flags();
2421#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002422 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002423}
2424
2425/*
2426 * More side effects of setting options.
2427 */
2428 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002429didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002430{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002431 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002432 (void)highlight_changed();
2433
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002434 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002435 check_opt_wim();
2436
Bram Moolenaareed9d462021-02-15 20:38:25 +01002437 // Parse default for 'listchars'.
2438 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2439
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002440 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002441 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002442
2443#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002444 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002445 (void)check_clipboard_option();
2446#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002447#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002448 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002449 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002450 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002451 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453}
2454
2455/*
2456 * Check for string options that are NULL (normally only termcap options).
2457 */
2458 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002459check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
2461 int opt_idx;
2462
2463 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2464 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2465 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2466}
2467
2468/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002469 * Return the option index found by a pointer into term_strings[].
2470 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002472 int
2473get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002475 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
2477 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2478 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002479 return opt_idx;
2480 return -1; // cannot happen: didn't find it!
2481}
2482
2483/*
2484 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2485 * Return the option index or -1 if not found.
2486 */
2487 int
2488set_term_option_alloced(char_u **p)
2489{
2490 int opt_idx = get_term_opt_idx(p);
2491
2492 if (opt_idx >= 0)
2493 options[opt_idx].flags |= P_ALLOCED;
2494 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495}
2496
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002497#if defined(FEAT_EVAL) || defined(PROTO)
2498/*
2499 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2500 * Return FALSE when it wasn't.
2501 * Return -1 for an unknown option.
2502 */
2503 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002504was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002505{
2506 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002507 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002508
2509 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002510 {
2511 flagp = insecure_flag(idx, opt_flags);
2512 return (*flagp & P_INSECURE) != 0;
2513 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002514 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002515 return -1;
2516}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002517
2518/*
2519 * Get a pointer to the flags used for the P_INSECURE flag of option
2520 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002521 * NOTE: Caller must make sure that "curwin" is set to the window from which
2522 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002523 */
2524 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002525insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002526{
2527 if (opt_flags & OPT_LOCAL)
2528 switch ((int)options[opt_idx].indir)
2529 {
2530#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002531 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002532#endif
2533#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002534# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002535 case PV_FDE: return &curwin->w_p_fde_flags;
2536 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002537# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002538# ifdef FEAT_BEVAL
2539 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2540# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002541# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002542 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002543# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002544 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002545# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002546 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002547# endif
2548#endif
2549 }
2550
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002551 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002552 return &options[opt_idx].flags;
2553}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002554#endif
2555
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002556/*
2557 * Redraw the window title and/or tab page text later.
2558 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002559void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002560{
2561 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002562 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002563}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002564
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002566 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2567 * characters or characters in "allowed".
2568 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002569 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002570valid_name(char_u *val, char *allowed)
2571{
2572 char_u *s;
2573
2574 for (s = val; *s != NUL; ++s)
2575 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2576 return FALSE;
2577 return TRUE;
2578}
2579
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002580#if defined(FEAT_EVAL) || defined(PROTO)
2581/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002582 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002583 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002584 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002585 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002586set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002587{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002588 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2589 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002590 sctx_T new_script_ctx = script_ctx;
2591
Bram Moolenaar51258742020-05-03 17:19:33 +02002592 // Modeline already has the line number set.
2593 if (!(opt_flags & OPT_MODELINE))
2594 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002596 // Remember where the option was set. For local options need to do that
2597 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002598 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002599 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002600 if (both || (opt_flags & OPT_LOCAL))
2601 {
2602 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002603 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002604 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002605 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002606 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002607 if (both)
2608 // also setting the "all buffers" value
2609 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2610 new_script_ctx;
2611 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002612 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002613}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002614
2615/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002616 * Get the script context of global option "name".
2617 *
2618 */
2619 sctx_T *
2620get_option_sctx(char *name)
2621{
2622 int idx = findoption((char_u *)name);
2623
2624 if (idx >= 0)
2625 return &options[idx].script_ctx;
2626 siemsg("no such option: %s", name);
2627 return NULL;
2628}
2629
2630/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002631 * Set the script_ctx for a termcap option.
2632 * "name" must be the two character code, e.g. "RV".
2633 * When "name" is NULL use "opt_idx".
2634 */
2635 void
2636set_term_option_sctx_idx(char *name, int opt_idx)
2637{
2638 char_u buf[5];
2639 int idx;
2640
2641 if (name == NULL)
2642 idx = opt_idx;
2643 else
2644 {
2645 buf[0] = 't';
2646 buf[1] = '_';
2647 buf[2] = name[0];
2648 buf[3] = name[1];
2649 buf[4] = 0;
2650 idx = findoption(buf);
2651 }
2652 if (idx >= 0)
2653 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2654}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002655#endif
2656
Bram Moolenaarf4140482020-02-15 23:06:45 +01002657#if defined(FEAT_EVAL)
2658/*
2659 * Apply the OptionSet autocommand.
2660 */
2661 static void
2662apply_optionset_autocmd(
2663 int opt_idx,
2664 long opt_flags,
2665 long oldval,
2666 long oldval_g,
2667 long newval,
2668 char *errmsg)
2669{
2670 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2671
2672 // Don't do this while starting up, failure or recursively.
2673 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2674 return;
2675
2676 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2677 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2678 oldval_g);
2679 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2680 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2681 (opt_flags & OPT_LOCAL) ? "local" : "global");
2682 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2683 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2684 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2685 if (opt_flags & OPT_LOCAL)
2686 {
2687 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2688 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2689 }
2690 if (opt_flags & OPT_GLOBAL)
2691 {
2692 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2693 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2694 }
2695 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2696 {
2697 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2698 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2699 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2700 }
2701 if (opt_flags & OPT_MODELINE)
2702 {
2703 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2704 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2705 }
2706 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2707 NULL, FALSE, NULL);
2708 reset_v_option_vars();
2709}
2710#endif
2711
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712/*
2713 * Set the value of a boolean option, and take care of side effects.
2714 * Returns NULL for success, or an error message for an error.
2715 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002716 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002717set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002718 int opt_idx, // index in options[] table
2719 char_u *varp, // pointer to the option variable
2720 int value, // new value
2721 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
2723 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002724#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002725 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002726#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002727 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002729 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 if ((secure
2731#ifdef HAVE_SANDBOX
2732 || sandbox != 0
2733#endif
2734 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002735 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002737#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002738 // Save the global value before changing anything. This is needed as for
2739 // a global-only option setting the "local value" in fact sets the global
2740 // value (since there is only one value).
2741 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2742 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2743 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002744#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002748 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002749 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750#endif
2751
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002752#ifdef FEAT_GUI
2753 need_mouse_correct = TRUE;
2754#endif
2755
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2758 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2759
2760 /*
2761 * Handle side effects of changing a bool option.
2762 */
2763
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
Bram Moolenaar920694c2016-08-21 17:45:02 +02002768#ifdef FEAT_LANGMAP
2769 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002770 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002771 p_lnr = !p_lrm;
2772 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002774 p_lrm = !p_lnr;
2775#endif
2776
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002777#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002778 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002779 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2780 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002781 // Only take action when the option was set. When reset we do not
2782 // delete the undo file, the option may be set again without making
2783 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002784 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002785 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002786 char_u hash[UNDO_HASH_SIZE];
2787 buf_T *save_curbuf = curbuf;
2788
Bram Moolenaar29323592016-07-24 22:04:11 +02002789 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002790 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002791 // When 'undofile' is set globally: for every buffer, otherwise
2792 // only for the current buffer: Try to read in the undofile,
2793 // if one exists, the buffer wasn't changed and the buffer was
2794 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002795 if ((curbuf == save_curbuf
2796 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2797 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2798 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002799#ifdef FEAT_CRYPT
2800 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2801 continue;
2802#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002803 u_compute_hash(hash);
2804 u_read_undo(NULL, hash, curbuf->b_fname);
2805 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002806 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002807 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002808 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002809 }
2810#endif
2811
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 else if ((int *)varp == &curbuf->b_p_ro)
2813 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002814 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2816 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002817
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002818 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002819 if (curbuf->b_p_ro)
2820 curbuf->b_did_warn = FALSE;
2821
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002822 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 }
2824
Bram Moolenaar9c449722010-07-20 18:44:27 +02002825#ifdef FEAT_GUI
2826 else if ((int *)varp == &p_mh)
2827 {
2828 if (!p_mh)
2829 gui_mch_mousehide(FALSE);
2830 }
2831#endif
2832
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002833 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002835 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002836# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002837 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002838 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2839 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002840 {
2841 curbuf->b_p_ma = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002842 return e_cannot_make_terminal_with_running_job_modifiable;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002843 }
2844# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002845 redraw_titles();
2846 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002847 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002849 {
2850 redraw_titles();
2851 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002852 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002853 else if ((int *)varp == &curbuf->b_p_fixeol)
2854 {
2855 redraw_titles();
2856 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002857 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002858 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002859 {
2860 redraw_titles();
2861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002863 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 else if ((int *)varp == &curbuf->b_p_bin)
2865 {
2866 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002867 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2872 {
2873 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2874 NULL, NULL, TRUE, curbuf);
2875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 else if ((int *)varp == &curbuf->b_p_swf)
2879 {
2880 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002883 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2884 // buf->b_p_swf
2885 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 }
2887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002888 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 else if ((int *)varp == &p_terse)
2890 {
2891 char_u *p;
2892
2893 p = vim_strchr(p_shm, SHM_SEARCH);
2894
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002895 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 if (p_terse && p == NULL)
2897 {
2898 STRCPY(IObuff, p_shm);
2899 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002900 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002902 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002904 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002907 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 else if ((int *)varp == &p_paste)
2909 {
2910 paste_option_changed();
2911 }
2912
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002913 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 else if ((int *)varp == &p_im)
2915 {
2916 if (p_im)
2917 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002918 if ((State & MODE_INSERT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 need_start_insertmode = TRUE;
2920 stop_insert_mode = FALSE;
2921 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002922 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002923 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 {
2925 need_start_insertmode = FALSE;
2926 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002927 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 restart_edit = 0;
2930 }
2931 }
2932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002933 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 else if ((int *)varp == &p_ic && p_hls)
2935 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002936 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 }
2938
2939#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002940 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 else if ((int *)varp == &p_hls)
2942 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002943 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 }
2945#endif
2946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002947 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2948 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 else if ((int *)varp == &curwin->w_p_scb)
2950 {
2951 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002954 curwin->w_scbind_pos = curwin->w_topline;
2955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
Bram Moolenaar4033c552017-09-16 20:54:51 +02002958#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002959 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 else if ((int *)varp == &curwin->w_p_pvw)
2961 {
2962 if (curwin->w_p_pvw)
2963 {
2964 win_T *win;
2965
Bram Moolenaar29323592016-07-24 22:04:11 +02002966 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 if (win->w_p_pvw && win != curwin)
2968 {
2969 curwin->w_p_pvw = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002970 return e_preview_window_already_exists;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 }
2972 }
2973 }
2974#endif
2975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002976 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 else if ((int *)varp == &curbuf->b_p_tx)
2978 {
2979 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2980 }
2981
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 set_string_option_direct((char_u *)"ffs", -1,
2986 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002987 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
2990 /*
2991 * When 'lisp' option changes include/exclude '-' in
2992 * keyword characters.
2993 */
2994#ifdef FEAT_LISP
2995 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2996 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002997 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 }
2999#endif
3000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003001 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02003002 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003004 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
3007 else if ((int *)varp == &curbuf->b_changed)
3008 {
3009 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003010 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003011 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
3014
3015#ifdef BACKSLASH_IN_FILENAME
3016 else if ((int *)varp == &p_ssl)
3017 {
3018 if (p_ssl)
3019 {
3020 psepc = '/';
3021 psepcN = '\\';
3022 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 }
3024 else
3025 {
3026 psepc = '\\';
3027 psepcN = '/';
3028 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 }
3030
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003031 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 buflist_slash_adjust();
3033 alist_slash_adjust();
3034# ifdef FEAT_EVAL
3035 scriptnames_slash_adjust();
3036# endif
3037 }
3038#endif
3039
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003040 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 else if ((int *)varp == &curwin->w_p_wrap)
3042 {
3043 if (curwin->w_p_wrap)
3044 curwin->w_leftcol = 0;
3045 }
3046
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 else if ((int *)varp == &p_ea)
3048 {
3049 if (p_ea && !old_value)
3050 win_equal(curwin, FALSE, 0);
3051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
3053 else if ((int *)varp == &p_wiv)
3054 {
3055 /*
3056 * When 'weirdinvert' changed, set/reset 't_xs'.
3057 * Then set 'weirdinvert' according to value of 't_xs'.
3058 */
3059 if (p_wiv && !old_value)
3060 T_XS = (char_u *)"y";
3061 else if (!p_wiv && old_value)
3062 T_XS = empty_option;
3063 p_wiv = (*T_XS != NUL);
3064 }
3065
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003066#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 else if ((int *)varp == &p_beval)
3068 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003069 if (!balloonEvalForTerm)
3070 {
3071 if (p_beval && !old_value)
3072 gui_mch_enable_beval_area(balloonEval);
3073 else if (!p_beval && old_value)
3074 gui_mch_disable_beval_area(balloonEval);
3075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003077#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003078#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003079 else if ((int *)varp == &p_bevalterm)
3080 {
3081 mch_bevalterm_changed();
3082 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003085#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 else if ((int *)varp == &p_acd)
3087 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003088 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003089 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 }
3091#endif
3092
3093#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 else if ((int *)varp == &curwin->w_p_diff)
3096 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003097 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003098 diff_buf_adjust(curwin);
3099# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (foldmethodIsDiff(curwin))
3101 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
3104#endif
3105
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003106#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003107 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 else if ((int *)varp == &p_imdisable)
3109 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003110 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 if (p_imdisable)
3112 im_set_active(FALSE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003113 else if (State & MODE_INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003114 // When the option is set from an autocommand, it may need to take
3115 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003116 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 }
3118#endif
3119
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003120#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003121 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003122 else if ((int *)varp == &curwin->w_p_spell)
3123 {
3124 if (curwin->w_p_spell)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003125 errmsg = did_set_spelllang(curwin);
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003126 }
3127#endif
3128
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129#ifdef FEAT_ARABIC
3130 if ((int *)varp == &curwin->w_p_arab)
3131 {
3132 if (curwin->w_p_arab)
3133 {
3134 /*
3135 * 'arabic' is set, handle various sub-settings.
3136 */
3137 if (!p_tbidi)
3138 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003139 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 if (!curwin->w_p_rl)
3141 {
3142 curwin->w_p_rl = TRUE;
3143 changed_window_setting();
3144 }
3145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003146 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 if (!p_arshape)
3148 {
3149 p_arshape = TRUE;
3150 redraw_later_clear();
3151 }
3152 }
3153
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003154 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003155 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003157 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003158 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3159
Bram Moolenaar8820b482017-03-16 17:23:31 +01003160 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003161 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003162#ifdef FEAT_EVAL
3163 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3164#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003167 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003171 // Force-set the necessary keymap for arabic
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003172 errmsg = set_option_value((char_u *)"keymap",
3173 0L, (char_u *)"arabic", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 }
3176 else
3177 {
3178 /*
3179 * 'arabic' is reset, handle various sub-settings.
3180 */
3181 if (!p_tbidi)
3182 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003183 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 if (curwin->w_p_rl)
3185 {
3186 curwin->w_p_rl = FALSE;
3187 changed_window_setting();
3188 }
3189
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003190 // 'arabicshape' isn't reset, it is a global option and
3191 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
3193
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003194 // 'delcombine' isn't reset, it is a global option and another
3195 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003198 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 curbuf->b_p_iminsert = B_IMODE_NONE;
3200 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3201# endif
3202 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003203 }
3204
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003205#endif
3206
Bram Moolenaar44826212019-08-22 21:23:20 +02003207#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3208 else if (((int *)varp == &curwin->w_p_nu
3209 || (int *)varp == &curwin->w_p_rnu)
3210 && gui.in_use
3211 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3212 && curbuf->b_signlist != NULL)
3213 {
3214 // If the 'number' or 'relativenumber' options are modified and
3215 // 'signcolumn' is set to 'number', then clear the screen for a full
3216 // refresh. Otherwise the sign icons are not displayed properly in the
3217 // number column. If the 'number' option is set and only the
3218 // 'relativenumber' option is toggled, then don't refresh the screen
3219 // (optimization).
3220 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3221 redraw_all_later(CLEAR);
3222 }
3223#endif
3224
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003225#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003226 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003227 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003228 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003229# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003230 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003231 if (
3232# ifdef VIMDLL
3233 !gui.in_use && !gui.starting &&
3234# endif
3235 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003236 {
3237 p_tgc = 0;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003238 return e_24_bit_colors_are_not_supported_on_this_environment;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003239 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003240 if (is_term_win32())
3241 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003242# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003243# ifdef FEAT_GUI
3244 if (!gui.in_use && !gui.starting)
3245# endif
3246 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003247# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003248 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003249 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003250 {
3251 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003252 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003253 init_highlight(TRUE, FALSE);
3254 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003255# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003256# ifdef FEAT_TERMINAL
3257 term_update_colors_all();
LemonBoyb2b3acb2022-05-20 10:10:34 +01003258 term_update_palette_all();
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003259 term_update_wincolor_all();
3260# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003261 }
3262#endif
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 /*
3265 * End of handling side effects for bool options.
3266 */
3267
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003268 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 options[opt_idx].flags |= P_WAS_SET;
3271
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003272#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003273 apply_optionset_autocmd(opt_idx, opt_flags,
3274 (long)(old_value ? TRUE : FALSE),
3275 (long)(old_global_value ? TRUE : FALSE),
3276 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003277#endif
3278
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003279 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003280 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003281 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003282 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003283
3284 if ((opt_flags & OPT_NO_REDRAW) == 0)
3285 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003287 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288}
3289
3290/*
3291 * Set the value of a number option, and take care of side effects.
3292 * Returns NULL for success, or an error message for an error.
3293 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003294 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003295set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003296 int opt_idx, // index in options[] table
3297 char_u *varp, // pointer to the option variable
3298 long value, // new value
3299 char *errbuf, // buffer for error messages
3300 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003301 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3302 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003304 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003306#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003307 long old_global_value = 0; // only used when setting a local and
3308 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003309#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003310 long old_Rows = Rows; // remember old Rows
3311 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 long *pp = (long *)varp;
3313
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003314 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003315 if ((secure
3316#ifdef HAVE_SANDBOX
3317 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003319 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003320 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003322#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003323 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003324 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003325 // value (since there is only one value).
3326 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003327 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3328 OPT_GLOBAL);
3329#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003330
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 *pp = value;
3332#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003333 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003334 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003336#ifdef FEAT_GUI
3337 need_mouse_correct = TRUE;
3338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
Bram Moolenaar14f24742012-08-08 18:01:05 +02003340 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003342 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003343#ifdef FEAT_VARTABS
3344 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3345 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003346 ? tabstop_first(curbuf->b_p_vts_array)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003347 : curbuf->b_p_ts;
3348#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 }
3352
3353 /*
3354 * Number options that need some action when changed
3355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 if (pp == &p_wh || pp == &p_hh)
3357 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003358 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (p_wh < 1)
3360 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003361 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 p_wh = 1;
3363 }
3364 if (p_wmh > p_wh)
3365 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003366 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 p_wh = p_wmh;
3368 }
3369 if (p_hh < 0)
3370 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003371 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 p_hh = 0;
3373 }
3374
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003375 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003376 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
3378 if (pp == &p_wh && curwin->w_height < p_wh)
3379 win_setheight((int)p_wh);
3380 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3381 win_setheight((int)p_hh);
3382 }
3383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 else if (pp == &p_wmh)
3385 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003386 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 if (p_wmh < 0)
3388 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003389 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 p_wmh = 0;
3391 }
3392 if (p_wmh > p_wh)
3393 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003394 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 p_wmh = p_wh;
3396 }
3397 win_setminheight();
3398 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003399 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003401 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (p_wiw < 1)
3403 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003404 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 p_wiw = 1;
3406 }
3407 if (p_wmw > p_wiw)
3408 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003409 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 p_wiw = p_wmw;
3411 }
3412
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003413 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003414 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 win_setwidth((int)p_wiw);
3416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 else if (pp == &p_wmw)
3418 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003419 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 if (p_wmw < 0)
3421 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003422 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 p_wmw = 0;
3424 }
3425 if (p_wmw > p_wiw)
3426 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003427 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 p_wmw = p_wiw;
3429 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003430 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003433 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 else if (pp == &p_ls)
3435 {
3436 last_status(FALSE);
3437 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003439 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003440 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003441 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003442 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
3445#ifdef FEAT_GUI
3446 else if (pp == &p_linespace)
3447 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003448 // Recompute gui.char_height and resize the Vim window to keep the
3449 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003450 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003451 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
3453#endif
3454
3455#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003456 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 else if (pp == &curwin->w_p_fdl)
3458 {
3459 if (curwin->w_p_fdl < 0)
3460 curwin->w_p_fdl = 0;
3461 newFoldLevel();
3462 }
3463
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003464 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 else if (pp == &curwin->w_p_fml)
3466 {
3467 foldUpdateAll(curwin);
3468 }
3469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003470 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 else if (pp == &curwin->w_p_fdn)
3472 {
3473 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3474 foldUpdateAll(curwin);
3475 }
3476
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003477 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 else if (pp == &curwin->w_p_fdc)
3479 {
3480 if (curwin->w_p_fdc < 0)
3481 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003482 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 curwin->w_p_fdc = 0;
3484 }
3485 else if (curwin->w_p_fdc > 12)
3486 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003487 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 curwin->w_p_fdc = 12;
3489 }
3490 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003491#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003493#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003494 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3496 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003497# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 if (foldmethodIsIndent(curwin))
3499 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003500# endif
3501# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003502 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3503 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003504 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3505 parse_cino(curbuf);
3506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003510 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003511 else if (pp == &p_mco)
3512 {
3513 if (p_mco > MAX_MCO)
3514 p_mco = MAX_MCO;
3515 else if (p_mco < 0)
3516 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003517 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003518 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 else if (pp == &curbuf->b_p_iminsert)
3521 {
3522 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3523 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003524 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 curbuf->b_p_iminsert = B_IMODE_NONE;
3526 }
3527 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003528 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003530#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003531 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 status_redraw_curbuf();
3533#endif
3534 }
3535
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003536#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003537 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003538 else if (pp == &p_imst)
3539 {
3540 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003541 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003542 }
3543#endif
3544
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003545 else if (pp == &p_window)
3546 {
3547 if (p_window < 1)
3548 p_window = 1;
3549 else if (p_window >= Rows)
3550 p_window = Rows - 1;
3551 }
3552
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 else if (pp == &curbuf->b_p_imsearch)
3554 {
3555 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3556 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003557 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 curbuf->b_p_imsearch = B_IMODE_NONE;
3559 }
3560 p_imsearch = curbuf->b_p_imsearch;
3561 }
3562
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003563 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 else if (pp == &p_titlelen)
3565 {
3566 if (p_titlelen < 0)
3567 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003568 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 p_titlelen = 85;
3570 }
3571 if (starting != NO_SCREEN && old_value != p_titlelen)
3572 need_maketitle = TRUE;
3573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003575 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 else if (pp == &p_ch)
3577 {
3578 if (p_ch < 1)
3579 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003580 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 p_ch = 1;
3582 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003583 if (p_ch > Rows - min_rows() + 1)
3584 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003586 // Only compute the new window layout when startup has been
3587 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 if (p_ch != old_value && full_screen
3589#ifdef FEAT_GUI
3590 && !gui.starting
3591#endif
3592 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003593 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003596 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 else if (pp == &p_uc)
3598 {
3599 if (p_uc < 0)
3600 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003601 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 p_uc = 100;
3603 }
3604 if (p_uc && !old_value)
3605 ml_open_files();
3606 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003607#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003608 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003609 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003610 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003611 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003612 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003613 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003614 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003615 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003616 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003617 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003618 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003619 }
3620 }
3621#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003622#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003623 else if (pp == &p_mzq)
3624 mzvim_reset_timer();
3625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003627#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003628 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003629 else if (pp == &p_pyx)
3630 {
3631 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003632 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003633 }
3634#endif
3635
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003636 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 else if (pp == &p_ul)
3638 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003639 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003641 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 p_ul = value;
3643 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003644 else if (pp == &curbuf->b_p_ul)
3645 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003646 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003647 curbuf->b_p_ul = old_value;
3648 u_sync(TRUE);
3649 curbuf->b_p_ul = value;
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003652#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003653 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003654 else if (pp == &curwin->w_p_nuw)
3655 {
3656 if (curwin->w_p_nuw < 1)
3657 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003658 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003659 curwin->w_p_nuw = 1;
3660 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003661 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003662 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003663 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003664 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003665 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003666 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003667 }
3668#endif
3669
Bram Moolenaar1a384422010-07-14 19:53:30 +02003670 else if (pp == &curbuf->b_p_tw)
3671 {
3672 if (curbuf->b_p_tw < 0)
3673 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003674 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003675 curbuf->b_p_tw = 0;
3676 }
3677#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003678 {
3679 win_T *wp;
3680 tabpage_T *tp;
3681
3682 FOR_ALL_TAB_WINDOWS(tp, wp)
3683 check_colorcolumn(wp);
3684 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003685#endif
3686 }
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 /*
3689 * Check the bounds for numeric options here
3690 */
3691 if (Rows < min_rows() && full_screen)
3692 {
3693 if (errbuf != NULL)
3694 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003695 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003696 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 errmsg = errbuf;
3698 }
3699 Rows = min_rows();
3700 }
3701 if (Columns < MIN_COLUMNS && full_screen)
3702 {
3703 if (errbuf != NULL)
3704 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003705 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003706 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 errmsg = errbuf;
3708 }
3709 Columns = MIN_COLUMNS;
3710 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003711 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 /*
3714 * If the screen (shell) height has been changed, assume it is the
3715 * physical screenheight.
3716 */
3717 if (old_Rows != Rows || old_Columns != Columns)
3718 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003719 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 if (updating_screen)
3721 *pp = old_value;
3722 else if (full_screen
3723#ifdef FEAT_GUI
3724 && !gui.starting
3725#endif
3726 )
3727 set_shellsize((int)Columns, (int)Rows, TRUE);
3728 else
3729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003730 // Postpone the resizing; check the size and cmdline position for
3731 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 check_shellsize();
3733 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3734 cmdline_row = Rows - p_ch;
3735 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003736 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003737 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
3739
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (curbuf->b_p_ts <= 0)
3741 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003742 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 curbuf->b_p_ts = 8;
3744 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003745 else if (curbuf->b_p_ts > TABSTOP_MAX)
3746 {
3747 errmsg = e_invalid_argument;
3748 curbuf->b_p_ts = 8;
3749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 if (p_tm < 0)
3751 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003752 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 p_tm = 0;
3754 }
3755 if ((curwin->w_p_scr <= 0
3756 || (curwin->w_p_scr > curwin->w_height
3757 && curwin->w_height > 0))
3758 && full_screen)
3759 {
3760 if (pp == &(curwin->w_p_scr))
3761 {
3762 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003763 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 win_comp_scroll(curwin);
3765 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003766 // If 'scroll' became invalid because of a side effect silently adjust
3767 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 else if (curwin->w_p_scr <= 0)
3769 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003770 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 curwin->w_p_scr = curwin->w_height;
3772 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003773 if (p_hi < 0)
3774 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003775 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003776 p_hi = 0;
3777 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003778 else if (p_hi > 10000)
3779 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003780 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003781 p_hi = 10000;
3782 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003783 if (p_re < 0 || p_re > 2)
3784 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003785 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003786 p_re = 0;
3787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (p_report < 0)
3789 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003790 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 p_report = 1;
3792 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003793 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003795 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 p_sj = Rows / 2;
3797 else
3798 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003799 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 p_sj = 1;
3801 }
3802 }
3803 if (p_so < 0 && full_screen)
3804 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003805 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 p_so = 0;
3807 }
3808 if (p_siso < 0 && full_screen)
3809 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003810 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 p_siso = 0;
3812 }
3813#ifdef FEAT_CMDWIN
3814 if (p_cwh < 1)
3815 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003816 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 p_cwh = 1;
3818 }
3819#endif
3820 if (p_ut < 0)
3821 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003822 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 p_ut = 2000;
3824 }
3825 if (p_ss < 0)
3826 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003827 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 p_ss = 0;
3829 }
3830
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003831 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3833 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3834
3835 options[opt_idx].flags |= P_WAS_SET;
3836
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003837#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003838 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3839 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003840#endif
3841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003842 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003843 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003844 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003845 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003846 if ((opt_flags & OPT_NO_REDRAW) == 0)
3847 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848
3849 return errmsg;
3850}
3851
3852/*
3853 * Called after an option changed: check if something needs to be redrawn.
3854 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003855 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003856check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003858 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003859 int doclear = (flags & P_RCLR) == P_RCLR;
3860 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003862 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
3865 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3866 changed_window_setting();
3867 if (flags & P_RBUF)
3868 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003869 if (flags & P_RWINONLY)
3870 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003871 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 redraw_all_later(CLEAR);
3873 else if (all)
3874 redraw_all_later(NOT_VALID);
3875}
3876
3877/*
3878 * Find index for option 'arg'.
3879 * Return -1 if not found.
3880 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003881 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003882findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883{
3884 int opt_idx;
3885 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003886 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 int is_term_opt;
3888
3889 /*
3890 * For first call: Initialize the quick-access table.
3891 * It contains the index for the first option that starts with a certain
3892 * letter. There are 26 letters, plus the first "t_" option.
3893 */
3894 if (quick_tab[1] == 0)
3895 {
3896 p = options[0].fullname;
3897 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3898 {
3899 if (s[0] != p[0])
3900 {
3901 if (s[0] == 't' && s[1] == '_')
3902 quick_tab[26] = opt_idx;
3903 else
3904 quick_tab[CharOrdLow(s[0])] = opt_idx;
3905 }
3906 p = s;
3907 }
3908 }
3909
3910 /*
3911 * Check for name starting with an illegal character.
3912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 return -1;
3915
3916 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3917 if (is_term_opt)
3918 opt_idx = quick_tab[26];
3919 else
3920 opt_idx = quick_tab[CharOrdLow(arg[0])];
3921 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3922 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003923 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 break;
3925 }
3926 if (s == NULL && !is_term_opt)
3927 {
3928 opt_idx = quick_tab[CharOrdLow(arg[0])];
3929 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3930 {
3931 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003932 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 break;
3934 s = NULL;
3935 }
3936 }
3937 if (s == NULL)
3938 opt_idx = -1;
3939 return opt_idx;
3940}
3941
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003942#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943/*
3944 * Get the value for an option.
3945 *
3946 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003947 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003948 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003949 * String option: gov_string, *stringval gets allocated string.
3950 * Hidden Number option: gov_hidden_number.
3951 * Hidden Toggle option: gov_hidden_bool.
3952 * Hidden String option: gov_hidden_string.
3953 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003954 *
3955 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003957 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003958get_option_value(
3959 char_u *name,
3960 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003961 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003962 int *flagsp,
3963 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
3965 int opt_idx;
3966 char_u *varp;
3967
3968 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003969 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003970 {
3971 int key;
3972
3973 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003974 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003975 {
3976 char_u key_name[2];
3977 char_u *p;
3978
Bram Moolenaar10c75c42021-12-28 20:53:30 +00003979 if (flagsp != NULL)
3980 *flagsp = 0; // terminal option has no flags
3981
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003982 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003983 if (key < 0)
3984 {
3985 key_name[0] = KEY2TERMCAP0(key);
3986 key_name[1] = KEY2TERMCAP1(key);
3987 }
3988 else
3989 {
3990 key_name[0] = KS_KEY;
3991 key_name[1] = (key & 0xff);
3992 }
3993 p = find_termcode(key_name);
3994 if (p != NULL)
3995 {
3996 if (stringval != NULL)
3997 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003998 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003999 }
4000 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004001 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004004 varp = get_varp_scope(&(options[opt_idx]), scope);
4005
4006 if (flagsp != NULL)
4007 // Return the P_xxxx option flags.
4008 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 if (options[opt_idx].flags & P_STRING)
4011 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004012 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004013 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 if (stringval != NULL)
4015 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004016 if ((char_u **)varp == &p_pt) // 'pastetoggle'
4017 *stringval = str2special_save(*(char_u **)(varp), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004019 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004020 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004021 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004024 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 *stringval = vim_strsave(*(char_u **)(varp));
4026 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004027 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004030 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004031 return (options[opt_idx].flags & P_NUM)
4032 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 if (options[opt_idx].flags & P_NUM)
4034 *numval = *(long *)varp;
4035 else
4036 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004037 // Special case: 'modified' is b_changed, but we also want to consider
4038 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if ((int *)varp == &curbuf->b_changed)
4040 *numval = curbufIsChanged();
4041 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004042 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004044 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045}
4046#endif
4047
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004048#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004049/*
4050 * Returns the option attributes and its value. Unlike the above function it
4051 * will return either global value or local value of the option depending on
4052 * what was requested, but it will never return global value if it was
4053 * requested to return local one and vice versa. Neither it will return
4054 * buffer-local value if it was requested to return window-local one.
4055 *
4056 * Pretends that option is absent if it is not present in the requested scope
4057 * (i.e. has no global, window-local or buffer-local value depending on
4058 * opt_type). Uses
4059 *
4060 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004061 * 0 hidden or unknown option, also option that does not have requested
4062 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004063 * see SOPT_* in vim.h for other flags
4064 *
4065 * Possible opt_type values: see SREQ_* in vim.h
4066 */
4067 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004068get_option_value_strict(
4069 char_u *name,
4070 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004071 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004072 int opt_type,
4073 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004074{
4075 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004076 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004077 struct vimoption *p;
4078 int r = 0;
4079
4080 opt_idx = findoption(name);
4081 if (opt_idx < 0)
4082 return 0;
4083
4084 p = &(options[opt_idx]);
4085
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004086 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004087 if (p->var == NULL)
4088 return 0;
4089
4090 if (p->flags & P_BOOL)
4091 r |= SOPT_BOOL;
4092 else if (p->flags & P_NUM)
4093 r |= SOPT_NUM;
4094 else if (p->flags & P_STRING)
4095 r |= SOPT_STRING;
4096
4097 if (p->indir == PV_NONE)
4098 {
4099 if (opt_type == SREQ_GLOBAL)
4100 r |= SOPT_GLOBAL;
4101 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004102 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004103 }
4104 else
4105 {
4106 if (p->indir & PV_BOTH)
4107 r |= SOPT_GLOBAL;
4108 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004109 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004110
4111 if (p->indir & PV_WIN)
4112 {
4113 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004114 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004115 else
4116 r |= SOPT_WIN;
4117 }
4118 else if (p->indir & PV_BUF)
4119 {
4120 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004121 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004122 else
4123 r |= SOPT_BUF;
4124 }
4125 }
4126
4127 if (stringval == NULL)
4128 return r;
4129
4130 if (opt_type == SREQ_GLOBAL)
4131 varp = p->var;
4132 else
4133 {
4134 if (opt_type == SREQ_BUF)
4135 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004136 // Special case: 'modified' is b_changed, but we also want to
4137 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004138 if (p->indir == PV_MOD)
4139 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004140 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004141 varp = NULL;
4142 }
4143#ifdef FEAT_CRYPT
4144 else if (p->indir == PV_KEY)
4145 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004146 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004147 *stringval = NULL;
4148 varp = NULL;
4149 }
4150#endif
4151 else
4152 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004153 buf_T *save_curbuf = curbuf;
4154
4155 // only getting a pointer, no need to use aucmd_prepbuf()
4156 curbuf = (buf_T *)from;
4157 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004158 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004159 curbuf = save_curbuf;
4160 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004161 }
4162 }
4163 else if (opt_type == SREQ_WIN)
4164 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004165 win_T *save_curwin = curwin;
4166
4167 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004168 curbuf = curwin->w_buffer;
4169 varp = get_varp(p);
4170 curwin = save_curwin;
4171 curbuf = curwin->w_buffer;
4172 }
4173 if (varp == p->var)
4174 return (r | SOPT_UNSET);
4175 }
4176
4177 if (varp != NULL)
4178 {
4179 if (p->flags & P_STRING)
4180 *stringval = vim_strsave(*(char_u **)(varp));
4181 else if (p->flags & P_NUM)
4182 *numval = *(long *) varp;
4183 else
4184 *numval = *(int *)varp;
4185 }
4186
4187 return r;
4188}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004189
4190/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004191 * Iterate over options. First argument is a pointer to a pointer to a
4192 * structure inside options[] array, second is option type like in the above
4193 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004194 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004195 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004196 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004197 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004198 * first argument to NULL.
4199 *
4200 * Returns full option name for current option on each call.
4201 */
4202 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004203option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004204{
4205 struct vimoption *ret = NULL;
4206 do
4207 {
4208 if (*option == NULL)
4209 *option = (void *) options;
4210 else if (((struct vimoption *) (*option))->fullname == NULL)
4211 {
4212 *option = NULL;
4213 return NULL;
4214 }
4215 else
4216 *option = (void *) (((struct vimoption *) (*option)) + 1);
4217
4218 ret = ((struct vimoption *) (*option));
4219
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004220 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004221 if (ret->var == NULL)
4222 {
4223 ret = NULL;
4224 continue;
4225 }
4226
4227 switch (opt_type)
4228 {
4229 case SREQ_GLOBAL:
4230 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4231 ret = NULL;
4232 break;
4233 case SREQ_BUF:
4234 if (!(ret->indir & PV_BUF))
4235 ret = NULL;
4236 break;
4237 case SREQ_WIN:
4238 if (!(ret->indir & PV_WIN))
4239 ret = NULL;
4240 break;
4241 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004242 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004243 return NULL;
4244 }
4245 }
4246 while (ret == NULL);
4247
4248 return (char_u *)ret->fullname;
4249}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004250#endif
4251
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004253 * Return the flags for the option at 'opt_idx'.
4254 */
4255 long_u
4256get_option_flags(int opt_idx)
4257{
4258 return options[opt_idx].flags;
4259}
4260
4261/*
4262 * Set a flag for the option at 'opt_idx'.
4263 */
4264 void
4265set_option_flag(int opt_idx, long_u flag)
4266{
4267 options[opt_idx].flags |= flag;
4268}
4269
4270/*
4271 * Clear a flag for the option at 'opt_idx'.
4272 */
4273 void
4274clear_option_flag(int opt_idx, long_u flag)
4275{
4276 options[opt_idx].flags &= ~flag;
4277}
4278
4279/*
4280 * Returns TRUE if the option at 'opt_idx' is a global option
4281 */
4282 int
4283is_global_option(int opt_idx)
4284{
4285 return options[opt_idx].indir == PV_NONE;
4286}
4287
4288/*
4289 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4290 * local value.
4291 */
4292 int
4293is_global_local_option(int opt_idx)
4294{
4295 return options[opt_idx].indir & PV_BOTH;
4296}
4297
4298/*
4299 * Returns TRUE if the option at 'opt_idx' is a window-local option
4300 */
4301 int
4302is_window_local_option(int opt_idx)
4303{
4304 return options[opt_idx].var == VAR_WIN;
4305}
4306
4307/*
4308 * Returns TRUE if the option at 'opt_idx' is a hidden option
4309 */
4310 int
4311is_hidden_option(int opt_idx)
4312{
4313 return options[opt_idx].var == NULL;
4314}
4315
4316#if defined(FEAT_CRYPT) || defined(PROTO)
4317/*
4318 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4319 */
4320 int
4321is_crypt_key_option(int opt_idx)
4322{
4323 return options[opt_idx].indir == PV_KEY;
4324}
4325#endif
4326
4327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 * Set the value of option "name".
4329 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004330 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004331 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004333 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004334set_option_value(
4335 char_u *name,
4336 long number,
4337 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004338 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339{
4340 int opt_idx;
4341 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004342 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343
4344 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004345 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004346 {
4347 int key;
4348
4349 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004350 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004351 {
4352 char_u key_name[2];
4353
4354 if (key < 0)
4355 {
4356 key_name[0] = KEY2TERMCAP0(key);
4357 key_name[1] = KEY2TERMCAP1(key);
4358 }
4359 else
4360 {
4361 key_name[0] = KS_KEY;
4362 key_name[1] = (key & 0xff);
4363 }
4364 add_termcode(key_name, string, FALSE);
4365 if (full_screen)
4366 ttest(FALSE);
4367 redraw_all_later(CLEAR);
4368 return NULL;
4369 }
4370
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004371 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 else
4374 {
4375 flags = options[opt_idx].flags;
4376#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004377 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004379 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004380 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004381 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004384 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004385 return set_string_option(opt_idx, string, opt_flags);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004386
4387 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
4388 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004390 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004392 int idx;
4393
4394 // Either we are given a string or we are setting option
4395 // to zero.
4396 for (idx = 0; string[idx] == '0'; ++idx)
4397 ;
4398 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004399 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004400 // There's another character after zeros or the string
4401 // is empty. In both cases, we are trying to set a
4402 // num option using a string.
4403 semsg(_(e_number_required_after_str_equal_str),
4404 name, string);
4405 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004406
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004409 if (flags & P_NUM)
4410 return set_num_option(opt_idx, varp, number,
4411 NULL, 0, opt_flags);
4412 else
4413 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004415
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004417 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418}
4419
4420/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004421 * Call set_option_value() and when an error is returned report it.
4422 */
4423 void
4424set_option_value_give_err(
4425 char_u *name,
4426 long number,
4427 char_u *string,
4428 int opt_flags) // OPT_LOCAL or 0 (both)
4429{
4430 char *errmsg = set_option_value(name, number, string, opt_flags);
4431
4432 if (errmsg != NULL)
4433 emsg(_(errmsg));
4434}
4435
4436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 * Get the terminal code for a terminal option.
4438 * Returns NULL when not found.
4439 */
4440 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004441get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442{
4443 int opt_idx;
4444 char_u *varp;
4445
4446 if (tname[0] != 't' || tname[1] != '_' ||
4447 tname[2] == NUL || tname[3] == NUL)
4448 return NULL;
4449 if ((opt_idx = findoption(tname)) >= 0)
4450 {
4451 varp = get_varp(&(options[opt_idx]));
4452 if (varp != NULL)
4453 varp = *(char_u **)(varp);
4454 return varp;
4455 }
4456 return find_termcode(tname + 2);
4457}
4458
4459 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004460get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461{
4462 int i;
4463
4464 i = findoption((char_u *)"hl");
4465 if (i >= 0)
4466 return options[i].def_val[VI_DEFAULT];
4467 return (char_u *)NULL;
4468}
4469
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004470 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004471get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004472{
4473 int i;
4474
4475 i = findoption((char_u *)"enc");
4476 if (i >= 0)
4477 return options[i].def_val[VI_DEFAULT];
4478 return (char_u *)NULL;
4479}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004480
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004481#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004482 int
4483is_option_allocated(char *name)
4484{
4485 int idx = findoption((char_u *)name);
4486
4487 return idx >= 0 && (options[idx].flags & P_ALLOCED);
4488}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004489#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004490
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491/*
4492 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004493 * When "has_lt" is true there is a '<' before "*arg_arg".
4494 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 */
4496 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004497find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004499 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004501 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 /*
4504 * Don't use get_special_key_code() for t_xx, we don't want it to call
4505 * add_termcap_entry().
4506 */
4507 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4508 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004509 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004511 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004513 key = find_special_key(&arg, &modifiers,
4514 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004515 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 key = 0;
4517 }
4518 return key;
4519}
4520
4521/*
4522 * if 'all' == 0: show changed options
4523 * if 'all' == 1: show all normal options
4524 * if 'all' == 2: show all terminal options
4525 */
4526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004527showoptions(
4528 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004529 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530{
4531 struct vimoption *p;
4532 int col;
4533 int isterm;
4534 char_u *varp;
4535 struct vimoption **items;
4536 int item_count;
4537 int run;
4538 int row, rows;
4539 int cols;
4540 int i;
4541 int len;
4542
4543#define INC 20
4544#define GAP 3
4545
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004546 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (items == NULL)
4548 return;
4549
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004550 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004552 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004554 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004556 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004558 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
4560 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004561 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 * 1. display the short items
4563 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004564 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 */
4566 for (run = 1; run <= 2 && !got_int; ++run)
4567 {
4568 /*
4569 * collect the items in items[]
4570 */
4571 item_count = 0;
4572 for (p = &options[0]; p->fullname != NULL; p++)
4573 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004574 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004575 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004576 continue;
4577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 varp = NULL;
4579 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004580 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
4582 if (p->indir != PV_NONE && !isterm)
4583 varp = get_varp_scope(p, opt_flags);
4584 }
4585 else
4586 varp = get_varp(p);
4587 if (varp != NULL
4588 && ((all == 2 && isterm)
4589 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004590 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004592 if (opt_flags & OPT_ONECOLUMN)
4593 len = Columns;
4594 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004595 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 else
4597 {
4598 option_value2string(p, opt_flags);
4599 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4600 }
4601 if ((len <= INC - GAP && run == 1) ||
4602 (len > INC - GAP && run == 2))
4603 items[item_count++] = p;
4604 }
4605 }
4606
4607 /*
4608 * display the items
4609 */
4610 if (run == 1)
4611 {
4612 cols = (Columns + GAP - 3) / INC;
4613 if (cols == 0)
4614 cols = 1;
4615 rows = (item_count + cols - 1) / cols;
4616 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004617 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 rows = item_count;
4619 for (row = 0; row < rows && !got_int; ++row)
4620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004621 msg_putchar('\n'); // go to next line
4622 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 break;
4624 col = 0;
4625 for (i = row; i < item_count; i += rows)
4626 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004627 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 showoneopt(items[i], opt_flags);
4629 col += INC;
4630 }
4631 out_flush();
4632 ui_breakcheck();
4633 }
4634 }
4635 vim_free(items);
4636}
4637
4638/*
4639 * Return TRUE if option "p" has its default value.
4640 */
4641 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004642optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643{
4644 int dvi;
4645
4646 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004647 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004648 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004650 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004652 // the cast to long is required for Manx C, long_i is
4653 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004654 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004655 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4657}
4658
4659/*
4660 * showoneopt: show the value of one option
4661 * must not be called with a hidden option!
4662 */
4663 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004664showoneopt(
4665 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004666 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004668 char_u *varp;
4669 int save_silent = silent_mode;
4670
4671 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004672 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
4674 varp = get_varp_scope(p, opt_flags);
4675
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004676 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4678 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004679 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004681 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004683 msg_puts(" ");
4684 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (!(p->flags & P_BOOL))
4686 {
4687 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004688 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 option_value2string(p, opt_flags);
4690 msg_outtrans(NameBuff);
4691 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004692
4693 silent_mode = save_silent;
4694 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695}
4696
4697/*
4698 * Write modified options as ":set" commands to a file.
4699 *
4700 * There are three values for "opt_flags":
4701 * OPT_GLOBAL: Write global option values and fresh values of
4702 * buffer-local options (used for start of a session
4703 * file).
4704 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4705 * curwin (used for a vimrc file).
4706 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4707 * and local values for window-local options of
4708 * curwin. Local values are also written when at the
4709 * default value, because a modeline or autocommand
4710 * may have set them when doing ":edit file" and the
4711 * user has set them back at the default or fresh
4712 * value.
4713 * When "local_only" is TRUE, don't write fresh
4714 * values, only local values (for ":mkview").
4715 * (fresh value = value used for a new buffer or window for a local option).
4716 *
4717 * Return FAIL on error, OK otherwise.
4718 */
4719 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004720makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721{
4722 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004723 char_u *varp; // currently used value
4724 char_u *varp_fresh; // local value
4725 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 char *cmd;
4727 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004728 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
4730 /*
4731 * The options that don't have a default (terminal name, columns, lines)
4732 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004733 * Do the loop over "options[]" twice: once for options with the
4734 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004736 for (pri = 1; pri >= 0; --pri)
4737 {
4738 for (p = &options[0]; !istermoption(p); p++)
4739 if (!(p->flags & P_NO_MKRC)
4740 && !istermoption(p)
4741 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004743 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4745 continue;
4746
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004747 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4748 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4750 continue;
4751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004752 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004754 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 continue;
4756
Bram Moolenaard23b7142021-04-17 21:04:34 +02004757 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4758 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004759 continue;
4760
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 round = 2;
4762 if (p->indir != PV_NONE)
4763 {
4764 if (p->var == VAR_WIN)
4765 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004766 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (!(opt_flags & OPT_LOCAL))
4768 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004769 // When fresh value of window-local option is not at the
4770 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4772 {
4773 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004774 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
4776 round = 1;
4777 varp_local = varp;
4778 varp = varp_fresh;
4779 }
4780 }
4781 }
4782 }
4783
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004784 // Round 1: fresh value for window-local options.
4785 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 for ( ; round <= 2; varp = varp_local, ++round)
4787 {
4788 if (round == 1 || (opt_flags & OPT_GLOBAL))
4789 cmd = "set";
4790 else
4791 cmd = "setlocal";
4792
4793 if (p->flags & P_BOOL)
4794 {
4795 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4796 return FAIL;
4797 }
4798 else if (p->flags & P_NUM)
4799 {
4800 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4801 return FAIL;
4802 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004803 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004805 int do_endif = FALSE;
4806
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004807 // Don't set 'syntax' and 'filetype' again if the value is
4808 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004809 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004810#if defined(FEAT_SYN_HL)
4811 p->indir == PV_SYN ||
4812#endif
4813 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
4815 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4816 *(char_u **)(varp)) < 0
4817 || put_eol(fd) < 0)
4818 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004819 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 }
4821 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004822 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004824 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 {
4826 if (put_line(fd, "endif") == FAIL)
4827 return FAIL;
4828 }
4829 }
4830 }
4831 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 return OK;
4834}
4835
4836#if defined(FEAT_FOLDING) || defined(PROTO)
4837/*
4838 * Generate set commands for the local fold options only. Used when
4839 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4840 */
4841 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004842makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004844 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004846 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 == FAIL
4848# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004849 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004851 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 == FAIL
4853 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4854 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4855 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4856 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4857 )
4858 return FAIL;
4859
4860 return OK;
4861}
4862#endif
4863
4864 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004865put_setstring(
4866 FILE *fd,
4867 char *cmd,
4868 char *name,
4869 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004870 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871{
4872 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004873 char_u *buf = NULL;
4874 char_u *part = NULL;
4875 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
4877 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4878 return FAIL;
4879 if (*valuep != NULL)
4880 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004881 // Output 'pastetoggle' as key names. For other
4882 // options some characters have to be escaped with
4883 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 if (valuep == &p_pt)
4885 {
4886 s = *valuep;
4887 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004888 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 return FAIL;
4890 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004891 // expand the option value, replace $HOME by ~
4892 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004894 int size = (int)STRLEN(*valuep) + 1;
4895
4896 // replace home directory in the whole option value into "buf"
4897 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004898 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004899 goto fail;
4900 home_replace(NULL, *valuep, buf, size, FALSE);
4901
4902 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004903 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004904 // can be expanded when read back.
4905 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4906 && vim_strchr(*valuep, ',') != NULL)
4907 {
4908 part = alloc(size);
4909 if (part == NULL)
4910 goto fail;
4911
4912 // write line break to clear the option, e.g. ':set rtp='
4913 if (put_eol(fd) == FAIL)
4914 goto fail;
4915
4916 p = buf;
4917 while (*p != NUL)
4918 {
4919 // for each comma separated option part, append value to
4920 // the option, :set rtp+=value
4921 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4922 goto fail;
4923 (void)copy_option_part(&p, part, size, ",");
4924 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4925 goto fail;
4926 }
4927 vim_free(buf);
4928 vim_free(part);
4929 return OK;
4930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004932 {
4933 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004935 }
4936 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938 else if (put_escstr(fd, *valuep, 2) == FAIL)
4939 return FAIL;
4940 }
4941 if (put_eol(fd) < 0)
4942 return FAIL;
4943 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004944fail:
4945 vim_free(buf);
4946 vim_free(part);
4947 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948}
4949
4950 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004951put_setnum(
4952 FILE *fd,
4953 char *cmd,
4954 char *name,
4955 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956{
4957 long wc;
4958
4959 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4960 return FAIL;
4961 if (wc_use_keyname((char_u *)valuep, &wc))
4962 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004963 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4965 return FAIL;
4966 }
4967 else if (fprintf(fd, "%ld", *valuep) < 0)
4968 return FAIL;
4969 if (put_eol(fd) < 0)
4970 return FAIL;
4971 return OK;
4972}
4973
4974 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004975put_setbool(
4976 FILE *fd,
4977 char *cmd,
4978 char *name,
4979 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004981 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004982 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4984 || put_eol(fd) < 0)
4985 return FAIL;
4986 return OK;
4987}
4988
4989/*
4990 * Clear all the terminal options.
4991 * If the option has been allocated, free the memory.
4992 * Terminal options are never hidden or indirect.
4993 */
4994 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004995clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 /*
4998 * Reset a few things before clearing the old options. This may cause
4999 * outputting a few things that the terminal doesn't understand, but the
5000 * screen will be cleared later, so this is OK.
5001 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005002 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005003 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005005 // When starting the GUI close the display opened for the clipboard.
5006 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 if (gui.starting)
5008 clear_xterm_clip();
5009#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005010 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005012 free_termoptions();
5013}
5014
5015 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005016free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005017{
5018 struct vimoption *p;
5019
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005020 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 if (istermoption(p))
5022 {
5023 if (p->flags & P_ALLOCED)
5024 free_string_option(*(char_u **)(p->var));
5025 if (p->flags & P_DEF_ALLOCED)
5026 free_string_option(p->def_val[VI_DEFAULT]);
5027 *(char_u **)(p->var) = empty_option;
5028 p->def_val[VI_DEFAULT] = empty_option;
5029 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005030#ifdef FEAT_EVAL
5031 // remember where the option was cleared
5032 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
5035 clear_termcodes();
5036}
5037
5038/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005039 * Free the string for one term option, if it was allocated.
5040 * Set the string to empty_option and clear allocated flag.
5041 * "var" points to the option value.
5042 */
5043 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005044free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005045{
5046 struct vimoption *p;
5047
5048 for (p = &options[0]; p->fullname != NULL; p++)
5049 if (p->var == var)
5050 {
5051 if (p->flags & P_ALLOCED)
5052 free_string_option(*(char_u **)(p->var));
5053 *(char_u **)(p->var) = empty_option;
5054 p->flags &= ~P_ALLOCED;
5055 break;
5056 }
5057}
5058
5059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 * Set the terminal option defaults to the current value.
5061 * Used after setting the terminal name.
5062 */
5063 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005064set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
5066 struct vimoption *p;
5067
5068 for (p = &options[0]; p->fullname != NULL; p++)
5069 {
5070 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5071 {
5072 if (p->flags & P_DEF_ALLOCED)
5073 {
5074 free_string_option(p->def_val[VI_DEFAULT]);
5075 p->flags &= ~P_DEF_ALLOCED;
5076 }
5077 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5078 if (p->flags & P_ALLOCED)
5079 {
5080 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005081 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
5083 }
5084 }
5085}
5086
5087/*
5088 * return TRUE if 'p' starts with 't_'
5089 */
5090 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005091istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092{
5093 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5094}
5095
Bram Moolenaardac13472019-09-16 21:06:21 +02005096/*
5097 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5098 */
5099 int
5100istermoption_idx(int opt_idx)
5101{
5102 return istermoption(&options[opt_idx]);
5103}
5104
Bram Moolenaar113e1072019-01-20 15:30:40 +01005105#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005107 * Unset local option value, similar to ":set opt<".
5108 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005109 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005110unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005111{
5112 struct vimoption *p;
5113 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005114 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005115
5116 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005117 if (opt_idx < 0)
5118 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005119 p = &(options[opt_idx]);
5120
5121 switch ((int)p->indir)
5122 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005123 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005124 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005125 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005126 break;
5127 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005128 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005129 break;
5130 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005131 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005132 break;
5133 case PV_AR:
5134 buf->b_p_ar = -1;
5135 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005136 case PV_BKC:
5137 clear_string_option(&buf->b_p_bkc);
5138 buf->b_bkc_flags = 0;
5139 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005140 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005141 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005142 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005143 case PV_TC:
5144 clear_string_option(&buf->b_p_tc);
5145 buf->b_tc_flags = 0;
5146 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005147 case PV_SISO:
5148 curwin->w_p_siso = -1;
5149 break;
5150 case PV_SO:
5151 curwin->w_p_so = -1;
5152 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005153#ifdef FEAT_FIND_ID
5154 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005155 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005156 break;
5157 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005158 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005159 break;
5160#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005161 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005162 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005163 break;
5164 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005165 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005166 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005167#ifdef FEAT_COMPL_FUNC
5168 case PV_TSRFU:
5169 clear_string_option(&buf->b_p_tsrfu);
5170 break;
5171#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005172 case PV_FP:
5173 clear_string_option(&buf->b_p_fp);
5174 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005175#ifdef FEAT_QUICKFIX
5176 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005177 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005178 break;
5179 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005180 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005181 break;
5182 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005183 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005184 break;
5185#endif
5186#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5187 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005188 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005189 break;
5190#endif
5191#if defined(FEAT_CRYPT)
5192 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005193 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005194 break;
5195#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005196#ifdef FEAT_LINEBREAK
5197 case PV_SBR:
5198 clear_string_option(&((win_T *)from)->w_p_sbr);
5199 break;
5200#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005201#ifdef FEAT_STL_OPT
5202 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005203 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005204 break;
5205#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005206 case PV_UL:
5207 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5208 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005209#ifdef FEAT_LISP
5210 case PV_LW:
5211 clear_string_option(&buf->b_p_lw);
5212 break;
5213#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005214 case PV_MENC:
5215 clear_string_option(&buf->b_p_menc);
5216 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005217 case PV_LCS:
5218 clear_string_option(&((win_T *)from)->w_p_lcs);
5219 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5220 redraw_later(NOT_VALID);
5221 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005222 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005223 clear_string_option(&((win_T *)from)->w_p_ve);
5224 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005225 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005226 }
5227}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005228#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005229
5230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005232 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 */
5234 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005235get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005237 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
5239 if (p->var == VAR_WIN)
5240 return (char_u *)GLOBAL_WO(get_varp(p));
5241 return p->var;
5242 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005243 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 {
5245 switch ((int)p->indir)
5246 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005247 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005249 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5250 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5251 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005253 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5254 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5255 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005256 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005257 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005258 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005259 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5260 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005262 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5263 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005265 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5266 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005267#ifdef FEAT_COMPL_FUNC
5268 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5269#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005270#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5271 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5272#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005273#if defined(FEAT_CRYPT)
5274 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5275#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005276#ifdef FEAT_LINEBREAK
5277 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5278#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005279#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005280 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005281#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005282 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005283#ifdef FEAT_LISP
5284 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5285#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005286 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005287 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005288 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005289 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005290
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005292 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
5294 return get_varp(p);
5295}
5296
5297/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005298 * Get pointer to option variable at 'opt_idx', depending on local or global
5299 * scope.
5300 */
5301 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005302get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005303{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005304 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005305}
5306
5307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 * Get pointer to option variable.
5309 */
5310 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005311get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005313 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 if (p->var == NULL)
5315 return NULL;
5316
5317 switch ((int)p->indir)
5318 {
5319 case PV_NONE: return p->var;
5320
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005321 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005322 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005324 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005326 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005328 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005330 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005332 case PV_TC: return *curbuf->b_p_tc != NUL
5333 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005334 case PV_BKC: return *curbuf->b_p_bkc != NUL
5335 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005336 case PV_SISO: return curwin->w_p_siso >= 0
5337 ? (char_u *)&(curwin->w_p_siso) : p->var;
5338 case PV_SO: return curwin->w_p_so >= 0
5339 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005341 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005343 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5345#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005346 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005348 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005350#ifdef FEAT_COMPL_FUNC
5351 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5352 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5353#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005354 case PV_FP: return *curbuf->b_p_fp != NUL
5355 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005357 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005359 case PV_GP: return *curbuf->b_p_gp != NUL
5360 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5361 case PV_MP: return *curbuf->b_p_mp != NUL
5362 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005364#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5365 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5366 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5367#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005368#if defined(FEAT_CRYPT)
5369 case PV_CM: return *curbuf->b_p_cm != NUL
5370 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5371#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005372#ifdef FEAT_LINEBREAK
5373 case PV_SBR: return *curwin->w_p_sbr != NUL
5374 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5375#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005376#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005377 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005378 ? (char_u *)&(curwin->w_p_stl) : p->var;
5379#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005380 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5381 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005382#ifdef FEAT_LISP
5383 case PV_LW: return *curbuf->b_p_lw != NUL
5384 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5385#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005386 case PV_MENC: return *curbuf->b_p_menc != NUL
5387 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388#ifdef FEAT_ARABIC
5389 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5390#endif
5391 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005392 case PV_LCS: return *curwin->w_p_lcs != NUL
5393 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005394 case PV_VE: return *curwin->w_p_ve != NUL
5395 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005396#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005397 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5398#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005399#ifdef FEAT_SYN_HL
5400 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5401 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005402 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005403 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405#ifdef FEAT_DIFF
5406 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5407#endif
5408#ifdef FEAT_FOLDING
5409 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5410 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5411 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5412 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5413 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5414 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5415 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5416# ifdef FEAT_EVAL
5417 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5418 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5419# endif
5420 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5421#endif
5422 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005423 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005424#ifdef FEAT_LINEBREAK
5425 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005428 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005429#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5431#endif
5432#ifdef FEAT_RIGHTLEFT
5433 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5434 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5435#endif
5436 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5437 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5438#ifdef FEAT_LINEBREAK
5439 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005440 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5441 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005443 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005445 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005446#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005447 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5448 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5449#endif
5450#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005451 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5452 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5453 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455
5456 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5457 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5460 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5462 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5463#ifdef FEAT_CINDENT
5464 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5465 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5466 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01005467 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468#endif
5469#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5470 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473#ifdef FEAT_FOLDING
5474 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005477#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005478 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005480#ifdef FEAT_COMPL_FUNC
5481 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005482 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005483#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005484#ifdef FEAT_EVAL
5485 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005488 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005494 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5496 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5497 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5498 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5499#ifdef FEAT_FIND_ID
5500# ifdef FEAT_EVAL
5501 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5502# endif
5503#endif
5504#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5505 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5506 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5507#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005508#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005509 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511#ifdef FEAT_CRYPT
5512 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5513#endif
5514#ifdef FEAT_LISP
5515 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5516#endif
5517 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5518 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5519 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5520 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5521 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005523#ifdef FEAT_TEXTOBJ
5524 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5527#ifdef FEAT_SMARTINDENT
5528 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5532#ifdef FEAT_SEARCHPATH
5533 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5534#endif
5535 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5536#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005537 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005538 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5539#endif
5540#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005541 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5542 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5543 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005544 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545#endif
5546 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5547 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5548 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5549 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005550#ifdef FEAT_PERSISTENT_UNDO
5551 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5554#ifdef FEAT_KEYMAP
5555 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5556#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005557#ifdef FEAT_SIGNS
5558 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5559#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005560#ifdef FEAT_VARTABS
5561 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5562 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5563#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005564 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005566 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 return (char_u *)&(curbuf->b_p_wm);
5568}
5569
5570/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005571 * Return a pointer to the variable for option at 'opt_idx'
5572 */
5573 char_u *
5574get_option_var(int opt_idx)
5575{
5576 return options[opt_idx].var;
5577}
5578
Dominique Pelle748b3082022-01-08 12:41:16 +00005579#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005580/*
5581 * Return the full name of the option at 'opt_idx'
5582 */
5583 char_u *
5584get_option_fullname(int opt_idx)
5585{
5586 return (char_u *)options[opt_idx].fullname;
5587}
Dominique Pelle748b3082022-01-08 12:41:16 +00005588#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005589
5590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 * Get the value of 'equalprg', either the buffer-local one or the global one.
5592 */
5593 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005594get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595{
5596 if (*curbuf->b_p_ep == NUL)
5597 return p_ep;
5598 return curbuf->b_p_ep;
5599}
5600
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601/*
5602 * Copy options from one window to another.
5603 * Used when splitting a window.
5604 */
5605 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005606win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
5608 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5609 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005610 after_copy_winopt(wp_to);
5611}
5612
5613/*
5614 * After copying window options: update variables depending on options.
5615 */
5616 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00005617after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005618{
5619#ifdef FEAT_LINEBREAK
5620 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005621#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005622#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005623 fill_culopt_flags(NULL, wp);
5624 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005625#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005626 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628
5629/*
5630 * Copy the options from one winopt_T to another.
5631 * Doesn't free the old option values in "to", use clear_winopt() for that.
5632 * The 'scroll' option is not copied, because it depends on the window height.
5633 * The 'previewwindow' option is reset, there can be only one preview window.
5634 */
5635 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005636copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638#ifdef FEAT_ARABIC
5639 to->wo_arab = from->wo_arab;
5640#endif
5641 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005642 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005644 to->wo_rnu = from->wo_rnu;
Gary Johnson51ad8502021-08-03 18:33:08 +02005645 to->wo_ve = vim_strsave(from->wo_ve);
5646 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005647#ifdef FEAT_LINEBREAK
5648 to->wo_nuw = from->wo_nuw;
5649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650#ifdef FEAT_RIGHTLEFT
5651 to->wo_rl = from->wo_rl;
5652 to->wo_rlc = vim_strsave(from->wo_rlc);
5653#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005654#ifdef FEAT_LINEBREAK
5655 to->wo_sbr = vim_strsave(from->wo_sbr);
5656#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005657#ifdef FEAT_STL_OPT
5658 to->wo_stl = vim_strsave(from->wo_stl);
5659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005661#ifdef FEAT_DIFF
5662 to->wo_wrap_save = from->wo_wrap_save;
5663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664#ifdef FEAT_LINEBREAK
5665 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005666 to->wo_bri = from->wo_bri;
5667 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005669 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005671 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005672 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005673 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005674#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005675 to->wo_spell = from->wo_spell;
5676#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005677#ifdef FEAT_SYN_HL
5678 to->wo_cuc = from->wo_cuc;
5679 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005680 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005681 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683#ifdef FEAT_DIFF
5684 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005685 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005687#ifdef FEAT_CONCEAL
5688 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005689 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005690#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005691#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005692 to->wo_twk = vim_strsave(from->wo_twk);
5693 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695#ifdef FEAT_FOLDING
5696 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005697 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005699 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 to->wo_fdi = vim_strsave(from->wo_fdi);
5701 to->wo_fml = from->wo_fml;
5702 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005703 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005705 to->wo_fdm_save = from->wo_diff_saved
5706 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 to->wo_fdn = from->wo_fdn;
5708# ifdef FEAT_EVAL
5709 to->wo_fde = vim_strsave(from->wo_fde);
5710 to->wo_fdt = vim_strsave(from->wo_fdt);
5711# endif
5712 to->wo_fmr = vim_strsave(from->wo_fmr);
5713#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005714#ifdef FEAT_SIGNS
5715 to->wo_scl = vim_strsave(from->wo_scl);
5716#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005717
5718#ifdef FEAT_EVAL
5719 // Copy the script context so that we know where the value was last set.
5720 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5721 sizeof(to->wo_script_ctx));
5722#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005723 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724}
5725
5726/*
5727 * Check string options in a window for a NULL value.
5728 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005729 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005730check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 check_winopt(&win->w_onebuf_opt);
5733 check_winopt(&win->w_allbuf_opt);
5734}
5735
5736/*
5737 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5738 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005739 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005740check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742#ifdef FEAT_FOLDING
5743 check_string_option(&wop->wo_fdi);
5744 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005745 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746# ifdef FEAT_EVAL
5747 check_string_option(&wop->wo_fde);
5748 check_string_option(&wop->wo_fdt);
5749# endif
5750 check_string_option(&wop->wo_fmr);
5751#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005752#ifdef FEAT_SIGNS
5753 check_string_option(&wop->wo_scl);
5754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755#ifdef FEAT_RIGHTLEFT
5756 check_string_option(&wop->wo_rlc);
5757#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005758#ifdef FEAT_LINEBREAK
5759 check_string_option(&wop->wo_sbr);
5760#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005761#ifdef FEAT_STL_OPT
5762 check_string_option(&wop->wo_stl);
5763#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005764#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005765 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005766 check_string_option(&wop->wo_cc);
5767#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005768#ifdef FEAT_CONCEAL
5769 check_string_option(&wop->wo_cocu);
5770#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005771#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005772 check_string_option(&wop->wo_twk);
5773 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005774#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005775#ifdef FEAT_LINEBREAK
5776 check_string_option(&wop->wo_briopt);
5777#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005778 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005779 check_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005780 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781}
5782
5783/*
5784 * Free the allocated memory inside a winopt_T.
5785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005787clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788{
5789#ifdef FEAT_FOLDING
5790 clear_string_option(&wop->wo_fdi);
5791 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005792 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793# ifdef FEAT_EVAL
5794 clear_string_option(&wop->wo_fde);
5795 clear_string_option(&wop->wo_fdt);
5796# endif
5797 clear_string_option(&wop->wo_fmr);
5798#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005799#ifdef FEAT_SIGNS
5800 clear_string_option(&wop->wo_scl);
5801#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005802#ifdef FEAT_LINEBREAK
5803 clear_string_option(&wop->wo_briopt);
5804#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005805 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806#ifdef FEAT_RIGHTLEFT
5807 clear_string_option(&wop->wo_rlc);
5808#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005809#ifdef FEAT_LINEBREAK
5810 clear_string_option(&wop->wo_sbr);
5811#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005812#ifdef FEAT_STL_OPT
5813 clear_string_option(&wop->wo_stl);
5814#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005815#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005816 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005817 clear_string_option(&wop->wo_cc);
5818#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005819#ifdef FEAT_CONCEAL
5820 clear_string_option(&wop->wo_cocu);
5821#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005822#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005823 clear_string_option(&wop->wo_twk);
5824 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005825#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005826 clear_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005827 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828}
5829
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005830#ifdef FEAT_EVAL
5831// Index into the options table for a buffer-local option enum.
5832static int buf_opt_idx[BV_COUNT];
5833# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5834
5835/*
5836 * Initialize buf_opt_idx[] if not done already.
5837 */
5838 static void
5839init_buf_opt_idx(void)
5840{
5841 static int did_init_buf_opt_idx = FALSE;
5842 int i;
5843
5844 if (did_init_buf_opt_idx)
5845 return;
5846 did_init_buf_opt_idx = TRUE;
5847 for (i = 0; !istermoption_idx(i); i++)
5848 if (options[i].indir & PV_BUF)
5849 buf_opt_idx[options[i].indir & PV_MASK] = i;
5850}
5851#else
5852# define COPY_OPT_SCTX(buf, bv)
5853#endif
5854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855/*
5856 * Copy global option values to local options for one buffer.
5857 * Used when creating a new buffer and sometimes when entering a buffer.
5858 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005859 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5861 * appropriate.
5862 * BCO_NOHELP Don't copy the values to a help buffer.
5863 */
5864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005865buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
5867 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005868 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 int dont_do_help;
5870 int did_isk = FALSE;
5871
5872 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 * Skip this when the option defaults have not been set yet. Happens when
5874 * main() allocates the first buffer.
5875 */
5876 if (p_cpo != NULL)
5877 {
5878 /*
5879 * Always copy when entering and 'cpo' contains 'S'.
5880 * Don't copy when already initialized.
5881 * Don't copy when 'cpo' contains 's' and not entering.
5882 * 'S' BCO_ENTER initialized 's' should_copy
5883 * yes yes X X TRUE
5884 * yes no yes X FALSE
5885 * no X yes X FALSE
5886 * X no no yes FALSE
5887 * X no no no TRUE
5888 * no yes no X TRUE
5889 */
5890 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5891 && (buf->b_p_initialized
5892 || (!(flags & BCO_ENTER)
5893 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5894 should_copy = FALSE;
5895
5896 if (should_copy || (flags & BCO_ALWAYS))
5897 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005898#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005899 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005900 init_buf_opt_idx();
5901#endif
5902 // Don't copy the options specific to a help buffer when
5903 // BCO_NOHELP is given or the options were initialized already
5904 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5906 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005907 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 {
5909 save_p_isk = buf->b_p_isk;
5910 buf->b_p_isk = NULL;
5911 }
5912 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005913 * Always free the allocated strings. If not already initialized,
5914 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 */
5916 if (!buf->b_p_initialized)
5917 {
5918 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005919 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005922 switch (*p_ffs)
5923 {
5924 case 'm':
5925 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5926 case 'd':
5927 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5928 case 'u':
5929 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5930 default:
5931 buf->b_p_ff = vim_strsave(p_ff);
5932 }
5933 if (buf->b_p_ff != NULL)
5934 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 buf->b_p_bh = empty_option;
5936 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 }
5938 else
5939 free_buf_options(buf, FALSE);
5940
5941 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005942 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 buf->b_p_ai_nopaste = p_ai_nopaste;
5944 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005945 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005947 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 buf->b_p_tw_nopaste = p_tw_nopaste;
5949 buf->b_p_tw_nobin = p_tw_nobin;
5950 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005951 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 buf->b_p_wm_nopaste = p_wm_nopaste;
5953 buf->b_p_wm_nobin = p_wm_nobin;
5954 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005955 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005956 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005957 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005958 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005959 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005961 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005963 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005965 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 buf->b_p_ml_nobin = p_ml_nobin;
5967 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005968 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005969 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005970 buf->b_p_swf = FALSE;
5971 else
5972 {
5973 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00005974 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005977 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005978#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005979 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005980 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005982#ifdef FEAT_COMPL_FUNC
5983 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005984 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005985 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005986 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005987 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005988 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005989#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005990#ifdef FEAT_EVAL
5991 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005992 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005993 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005996 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005998#ifdef FEAT_VARTABS
5999 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006000 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006001 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006002 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006003 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006004 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006005 buf->b_p_vsts_nopaste = p_vsts_nopaste
6006 ? vim_strsave(p_vsts_nopaste) : NULL;
6007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006009 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006011 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012#ifdef FEAT_FOLDING
6013 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006014 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015#endif
6016 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006017 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006018 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006019 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006020 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6021 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006023 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006025 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026#ifdef FEAT_SMARTINDENT
6027 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006028 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029#endif
6030 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006031 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032#ifdef FEAT_CINDENT
6033 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006034 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006036 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006038 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006039 buf->b_p_cinsd = vim_strsave(p_cinsd);
6040 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006042 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006045 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
6047 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006048 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049#endif
6050#ifdef FEAT_LISP
6051 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006052 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053#endif
6054#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006055 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006057 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006058 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006059 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006060#endif
6061#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006062 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006063 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006064 (void)compile_cap_prog(&buf->b_s);
6065 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006066 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006067 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006068 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006069 buf->b_s.b_p_spo = vim_strsave(p_spo);
6070 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071#endif
6072#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
6073 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006074 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006076 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006078 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006079#if defined(FEAT_EVAL)
6080 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006081 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083#ifdef FEAT_CRYPT
6084 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006085 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086#endif
6087#ifdef FEAT_SEARCHPATH
6088 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006089 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090#endif
6091#ifdef FEAT_KEYMAP
6092 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006093 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 buf->b_kmap_state |= KEYMAP_INIT;
6095#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006096#ifdef FEAT_TERMINAL
6097 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006098 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006099#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006100 // This isn't really an option, but copying the langmap and IME
6101 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006103 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006105 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006107 // options that are normally global but also have a local value
6108 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006110 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006111 buf->b_p_bkc = empty_option;
6112 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113#ifdef FEAT_QUICKFIX
6114 buf->b_p_gp = empty_option;
6115 buf->b_p_mp = empty_option;
6116 buf->b_p_efm = empty_option;
6117#endif
6118 buf->b_p_ep = empty_option;
6119 buf->b_p_kp = empty_option;
6120 buf->b_p_path = empty_option;
6121 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006122 buf->b_p_tc = empty_option;
6123 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124#ifdef FEAT_FIND_ID
6125 buf->b_p_def = empty_option;
6126 buf->b_p_inc = empty_option;
6127# ifdef FEAT_EVAL
6128 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006129 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130# endif
6131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 buf->b_p_dict = empty_option;
6133 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006134#ifdef FEAT_COMPL_FUNC
6135 buf->b_p_tsrfu = empty_option;
6136#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006137#ifdef FEAT_TEXTOBJ
6138 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006139 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006140#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006141#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6142 buf->b_p_bexpr = empty_option;
6143#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006144#if defined(FEAT_CRYPT)
6145 buf->b_p_cm = empty_option;
6146#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006147#ifdef FEAT_PERSISTENT_UNDO
6148 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006149 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006150#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006151#ifdef FEAT_LISP
6152 buf->b_p_lw = empty_option;
6153#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006154 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
6156 /*
6157 * Don't copy the options set by ex_help(), use the saved values,
6158 * when going from a help buffer to a non-help buffer.
6159 * Don't touch these at all when BCO_NOHELP is used and going from
6160 * or to a help buffer.
6161 */
6162 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006165#ifdef FEAT_VARTABS
6166 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006167 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006168 else
6169 buf->b_p_vts_array = NULL;
6170#endif
6171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 else
6173 {
6174 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006175 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 did_isk = TRUE;
6177 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006178 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006179#ifdef FEAT_VARTABS
6180 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006181 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006182 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006183 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006184 else
6185 buf->b_p_vts_array = NULL;
6186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 if (buf->b_p_bt[0] == 'h')
6189 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006191 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 }
6193 }
6194
6195 /*
6196 * When the options should be copied (ignoring BCO_ALWAYS), set the
6197 * flag that indicates that the options have been initialized.
6198 */
6199 if (should_copy)
6200 buf->b_p_initialized = TRUE;
6201 }
6202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006203 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 if (did_isk)
6205 (void)buf_init_chartab(buf, FALSE);
6206}
6207
6208/*
6209 * Reset the 'modifiable' option and its default value.
6210 */
6211 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006212reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213{
6214 int opt_idx;
6215
6216 curbuf->b_p_ma = FALSE;
6217 p_ma = FALSE;
6218 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006219 if (opt_idx >= 0)
6220 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221}
6222
6223/*
6224 * Set the global value for 'iminsert' to the local value.
6225 */
6226 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006227set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
6229 p_iminsert = curbuf->b_p_iminsert;
6230}
6231
6232/*
6233 * Set the global value for 'imsearch' to the local value.
6234 */
6235 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006236set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
6238 p_imsearch = curbuf->b_p_imsearch;
6239}
6240
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241static int expand_option_idx = -1;
6242static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6243static int expand_option_flags = 0;
6244
6245 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006246set_context_in_set_cmd(
6247 expand_T *xp,
6248 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006249 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
6251 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006252 long_u flags = 0; // init for GCC
6253 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 char_u *p;
6255 char_u *s;
6256 int is_term_option = FALSE;
6257 int key;
6258
6259 expand_option_flags = opt_flags;
6260
6261 xp->xp_context = EXPAND_SETTINGS;
6262 if (*arg == NUL)
6263 {
6264 xp->xp_pattern = arg;
6265 return;
6266 }
6267 p = arg + STRLEN(arg) - 1;
6268 if (*p == ' ' && *(p - 1) != '\\')
6269 {
6270 xp->xp_pattern = p + 1;
6271 return;
6272 }
6273 while (p > arg)
6274 {
6275 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006276 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 if (*p == ' ' || *p == ',')
6278 {
6279 while (s > arg && *(s - 1) == '\\')
6280 --s;
6281 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006282 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 if (*p == ' ' && ((p - s) & 1) == 0)
6284 {
6285 ++p;
6286 break;
6287 }
6288 --p;
6289 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006290 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 {
6292 xp->xp_context = EXPAND_BOOL_SETTINGS;
6293 p += 2;
6294 }
6295 if (STRNCMP(p, "inv", 3) == 0)
6296 {
6297 xp->xp_context = EXPAND_BOOL_SETTINGS;
6298 p += 3;
6299 }
6300 xp->xp_pattern = arg = p;
6301 if (*arg == '<')
6302 {
6303 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006304 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 return;
6306 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006307 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 {
6309 xp->xp_context = EXPAND_NOTHING;
6310 return;
6311 }
6312 nextchar = *++p;
6313 is_term_option = TRUE;
6314 expand_option_name[2] = KEY2TERMCAP0(key);
6315 expand_option_name[3] = KEY2TERMCAP1(key);
6316 }
6317 else
6318 {
6319 if (p[0] == 't' && p[1] == '_')
6320 {
6321 p += 2;
6322 if (*p != NUL)
6323 ++p;
6324 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006325 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 nextchar = *++p;
6327 is_term_option = TRUE;
6328 expand_option_name[2] = p[-2];
6329 expand_option_name[3] = p[-1];
6330 }
6331 else
6332 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006333 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6335 p++;
6336 if (*p == NUL)
6337 return;
6338 nextchar = *p;
6339 *p = NUL;
6340 opt_idx = findoption(arg);
6341 *p = nextchar;
6342 if (opt_idx == -1 || options[opt_idx].var == NULL)
6343 {
6344 xp->xp_context = EXPAND_NOTHING;
6345 return;
6346 }
6347 flags = options[opt_idx].flags;
6348 if (flags & P_BOOL)
6349 {
6350 xp->xp_context = EXPAND_NOTHING;
6351 return;
6352 }
6353 }
6354 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006355 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6357 {
6358 ++p;
6359 nextchar = '=';
6360 }
6361 if ((nextchar != '=' && nextchar != ':')
6362 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6363 {
6364 xp->xp_context = EXPAND_UNSUCCESSFUL;
6365 return;
6366 }
6367 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6368 {
6369 xp->xp_context = EXPAND_OLD_SETTING;
6370 if (is_term_option)
6371 expand_option_idx = -1;
6372 else
6373 expand_option_idx = opt_idx;
6374 xp->xp_pattern = p + 1;
6375 return;
6376 }
6377 xp->xp_context = EXPAND_NOTHING;
6378 if (is_term_option || (flags & P_NUM))
6379 return;
6380
6381 xp->xp_pattern = p + 1;
6382
6383 if (flags & P_EXPAND)
6384 {
6385 p = options[opt_idx].var;
6386 if (p == (char_u *)&p_bdir
6387 || p == (char_u *)&p_dir
6388 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006389 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 || p == (char_u *)&p_rtp
6391#ifdef FEAT_SEARCHPATH
6392 || p == (char_u *)&p_cdpath
6393#endif
6394#ifdef FEAT_SESSION
6395 || p == (char_u *)&p_vdir
6396#endif
6397 )
6398 {
6399 xp->xp_context = EXPAND_DIRECTORIES;
6400 if (p == (char_u *)&p_path
6401#ifdef FEAT_SEARCHPATH
6402 || p == (char_u *)&p_cdpath
6403#endif
6404 )
6405 xp->xp_backslash = XP_BS_THREE;
6406 else
6407 xp->xp_backslash = XP_BS_ONE;
6408 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006409 else if (p == (char_u *)&p_ft)
6410 {
6411 xp->xp_context = EXPAND_FILETYPE;
6412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 else
6414 {
6415 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006416 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 if (p == (char_u *)&p_tags)
6418 xp->xp_backslash = XP_BS_THREE;
6419 else
6420 xp->xp_backslash = XP_BS_ONE;
6421 }
6422 }
6423
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006424 // For an option that is a list of file names, find the start of the
6425 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6427 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006428 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 if (*p == ' ' || *p == ',')
6430 {
6431 s = p;
6432 while (s > xp->xp_pattern && *(s - 1) == '\\')
6433 --s;
6434 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6435 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6436 {
6437 xp->xp_pattern = p + 1;
6438 break;
6439 }
6440 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006441
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006442#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006443 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006444 if (options[opt_idx].var == (char_u *)&p_sps
6445 && STRNCMP(p, "file:", 5) == 0)
6446 {
6447 xp->xp_pattern = p + 5;
6448 break;
6449 }
6450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452}
6453
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006454/*
6455 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
6456 *
6457 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
6458 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
6459 *
6460 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
6461 * regular expression 'regmatch', then stores the match in matches[idx] and
6462 * returns TRUE.
6463 *
6464 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6465 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
6466 *
6467 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6468 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
6469 */
6470 static int
6471match_str(
6472 char_u *str,
6473 regmatch_T *regmatch,
6474 char_u **matches,
6475 int idx,
6476 int test_only,
6477 int fuzzy,
6478 char_u *fuzzystr,
6479 fuzmatch_str_T *fuzmatch)
6480{
6481 if (!fuzzy)
6482 {
6483 if (vim_regexec(regmatch, str, (colnr_T)0))
6484 {
6485 if (!test_only)
6486 matches[idx] = vim_strsave(str);
6487 return TRUE;
6488 }
6489 }
6490 else
6491 {
6492 int score;
6493
6494 score = fuzzy_match_str(str, fuzzystr);
6495 if (score != 0)
6496 {
6497 if (!test_only)
6498 {
6499 fuzmatch[idx].idx = idx;
6500 fuzmatch[idx].str = vim_strsave(str);
6501 fuzmatch[idx].score = score;
6502 }
6503
6504 return TRUE;
6505 }
6506 }
6507
6508 return FALSE;
6509}
6510
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006512ExpandSettings(
6513 expand_T *xp,
6514 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006515 char_u *fuzzystr,
6516 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01006517 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006518 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006520 int num_normal = 0; // Nr of matching non-term-code settings
6521 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 int opt_idx;
6523 int match;
6524 int count = 0;
6525 char_u *str;
6526 int loop;
6527 int is_term_opt;
6528 char_u name_buf[MAX_KEY_NAME_LEN];
6529 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006530 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006531 int fuzzy;
6532 fuzmatch_str_T *fuzmatch = NULL;
6533
Christian Brabandtcb747892022-05-08 21:10:56 +01006534 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006536 // do this loop twice:
6537 // loop == 0: count the number of matching options
6538 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 for (loop = 0; loop <= 1; ++loop)
6540 {
6541 regmatch->rm_ic = ic;
6542 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6543 {
K.Takataeeec2542021-06-02 13:28:16 +02006544 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006545 {
6546 if (match_str((char_u *)names[match], regmatch, *matches,
6547 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 {
6549 if (loop == 0)
6550 num_normal++;
6551 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006552 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 }
6556 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6557 opt_idx++)
6558 {
6559 if (options[opt_idx].var == NULL)
6560 continue;
6561 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6562 && !(options[opt_idx].flags & P_BOOL))
6563 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006564 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 if (is_term_opt && num_normal > 0)
6566 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006567
6568 if (match_str(str, regmatch, *matches, count, (loop == 0),
6569 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 {
6571 if (loop == 0)
6572 {
6573 if (is_term_opt)
6574 num_term++;
6575 else
6576 num_normal++;
6577 }
6578 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006579 count++;
6580 }
6581 else if (!fuzzy && options[opt_idx].shortname != NULL
6582 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006583 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006584 {
6585 // Compare against the abbreviated option name (for regular
6586 // expression match). Fuzzy matching (previous if) already
6587 // matches against both the expanded and abbreviated names.
6588 if (loop == 0)
6589 {
6590 if (is_term_opt)
6591 num_term++;
6592 else
6593 num_normal++;
6594 }
6595 else
6596 (*matches)[count++] = vim_strsave(str);
6597 }
6598 else if (is_term_opt)
6599 {
6600 name_buf[0] = '<';
6601 name_buf[1] = 't';
6602 name_buf[2] = '_';
6603 name_buf[3] = str[2];
6604 name_buf[4] = str[3];
6605 name_buf[5] = '>';
6606 name_buf[6] = NUL;
6607
6608 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6609 fuzzy, fuzzystr, fuzmatch))
6610 {
6611 if (loop == 0)
6612 num_term++;
6613 else
6614 count++;
6615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 }
6617 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006618
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 /*
6620 * Check terminal key codes, these are not in the option table
6621 */
6622 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6623 {
6624 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6625 {
6626 if (!isprint(str[0]) || !isprint(str[1]))
6627 continue;
6628
6629 name_buf[0] = 't';
6630 name_buf[1] = '_';
6631 name_buf[2] = str[0];
6632 name_buf[3] = str[1];
6633 name_buf[4] = NUL;
6634
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006635 if (match_str(name_buf, regmatch, *matches, count,
6636 (loop == 0), fuzzy, fuzzystr, fuzmatch))
6637 {
6638 if (loop == 0)
6639 num_term++;
6640 else
6641 count++;
6642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 else
6644 {
6645 name_buf[0] = '<';
6646 name_buf[1] = 't';
6647 name_buf[2] = '_';
6648 name_buf[3] = str[0];
6649 name_buf[4] = str[1];
6650 name_buf[5] = '>';
6651 name_buf[6] = NUL;
6652
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006653 if (match_str(name_buf, regmatch, *matches, count,
6654 (loop == 0), fuzzy, fuzzystr,
6655 fuzmatch))
6656 {
6657 if (loop == 0)
6658 num_term++;
6659 else
6660 count++;
6661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 }
6663 }
6664
6665 /*
6666 * Check special key names.
6667 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006668 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6670 {
6671 name_buf[0] = '<';
6672 STRCPY(name_buf + 1, str);
6673 STRCAT(name_buf, ">");
6674
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006675 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6676 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 {
6678 if (loop == 0)
6679 num_term++;
6680 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006681 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 }
6683 }
6684 }
6685 if (loop == 0)
6686 {
6687 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006688 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006690 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 else
6692 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006693 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006695 *matches = ALLOC_MULT(char_u *, *numMatches);
6696 if (*matches == NULL)
6697 {
6698 *matches = (char_u **)"";
6699 return FAIL;
6700 }
6701 }
6702 else
6703 {
6704 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
6705 if (fuzmatch == NULL)
6706 {
6707 *matches = (char_u **)"";
6708 return FAIL;
6709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 }
6711 }
6712 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006713
6714 if (fuzzy &&
6715 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
6716 return FAIL;
6717
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 return OK;
6719}
6720
6721 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006722ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006724 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 char_u *buf;
6726
6727 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006728 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 if (*file == NULL)
6730 return FAIL;
6731
6732 /*
6733 * For a terminal key code expand_option_idx is < 0.
6734 */
6735 if (expand_option_idx < 0)
6736 {
6737 var = find_termcode(expand_option_name + 2);
6738 if (var == NULL)
6739 expand_option_idx = findoption(expand_option_name);
6740 }
6741
6742 if (expand_option_idx >= 0)
6743 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006744 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 option_value2string(&options[expand_option_idx], expand_option_flags);
6746 var = NameBuff;
6747 }
6748 else if (var == NULL)
6749 var = (char_u *)"";
6750
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006751 // A backslash is required before some characters. This is the reverse of
6752 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 buf = vim_strsave_escaped(var, escape_chars);
6754
6755 if (buf == NULL)
6756 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006757 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 return FAIL;
6759 }
6760
6761#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006762 // For MS-Windows et al. we don't double backslashes at the start and
6763 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006764 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 if (var[0] == '\\' && var[1] == '\\'
6766 && expand_option_idx >= 0
6767 && (options[expand_option_idx].flags & P_EXPAND)
6768 && vim_isfilec(var[2])
6769 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006770 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771#endif
6772
6773 *file[0] = buf;
6774 *num_file = 1;
6775 return OK;
6776}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777
6778/*
6779 * Get the value for the numeric or string option *opp in a nice format into
6780 * NameBuff[]. Must not be called with a hidden option!
6781 */
6782 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006783option_value2string(
6784 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006785 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786{
6787 char_u *varp;
6788
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006789 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
6791 if (opp->flags & P_NUM)
6792 {
6793 long wc = 0;
6794
6795 if (wc_use_keyname(varp, &wc))
6796 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6797 else if (wc != 0)
6798 STRCPY(NameBuff, transchar((int)wc));
6799 else
6800 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6801 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006802 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {
6804 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006805 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 NameBuff[0] = NUL;
6807#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006808 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006809 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 STRCPY(NameBuff, "*****");
6811#endif
6812 else if (opp->flags & P_EXPAND)
6813 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006814 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 else if ((char_u **)opp->var == &p_pt)
6816 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6817 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006818 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 }
6820}
6821
6822/*
6823 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6824 * printed as a keyname.
6825 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6826 */
6827 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006828wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829{
6830 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6831 {
6832 *wcp = *(long *)varp;
6833 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6834 return TRUE;
6835 }
6836 return FALSE;
6837}
6838
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 * Return TRUE if "x" is present in 'shortmess' option, or
6841 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6842 */
6843 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006844shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006846 return p_shm != NULL &&
6847 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 || (vim_strchr(p_shm, 'a') != NULL
6849 && vim_strchr((char_u *)SHM_A, x) != NULL));
6850}
6851
6852/*
6853 * paste_option_changed() - Called after p_paste was set or reset.
6854 */
6855 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006856paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
6858 static int old_p_paste = FALSE;
6859 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006860 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861#ifdef FEAT_CMDL_INFO
6862 static int save_ru = 0;
6863#endif
6864#ifdef FEAT_RIGHTLEFT
6865 static int save_ri = 0;
6866 static int save_hkmap = 0;
6867#endif
6868 buf_T *buf;
6869
6870 if (p_paste)
6871 {
6872 /*
6873 * Paste switched from off to on.
6874 * Save the current values, so they can be restored later.
6875 */
6876 if (!old_p_paste)
6877 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006878 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006879 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
6881 buf->b_p_tw_nopaste = buf->b_p_tw;
6882 buf->b_p_wm_nopaste = buf->b_p_wm;
6883 buf->b_p_sts_nopaste = buf->b_p_sts;
6884 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006885 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006886#ifdef FEAT_VARTABS
6887 if (buf->b_p_vsts_nopaste)
6888 vim_free(buf->b_p_vsts_nopaste);
6889 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6890 ? vim_strsave(buf->b_p_vsts) : NULL;
6891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 }
6893
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006894 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006896 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897#ifdef FEAT_CMDL_INFO
6898 save_ru = p_ru;
6899#endif
6900#ifdef FEAT_RIGHTLEFT
6901 save_ri = p_ri;
6902 save_hkmap = p_hkmap;
6903#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006904 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006905 p_ai_nopaste = p_ai;
6906 p_et_nopaste = p_et;
6907 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 p_tw_nopaste = p_tw;
6909 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006910#ifdef FEAT_VARTABS
6911 if (p_vsts_nopaste)
6912 vim_free(p_vsts_nopaste);
6913 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 }
6916
6917 /*
6918 * Always set the option values, also when 'paste' is set when it is
6919 * already on.
6920 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006921 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006922 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006924 buf->b_p_tw = 0; // textwidth is 0
6925 buf->b_p_wm = 0; // wrapmargin is 0
6926 buf->b_p_sts = 0; // softtabstop is 0
6927 buf->b_p_ai = 0; // no auto-indent
6928 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006929#ifdef FEAT_VARTABS
6930 if (buf->b_p_vsts)
6931 free_string_option(buf->b_p_vsts);
6932 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006933 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 }
6936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006937 // set global options
6938 p_sm = 0; // no showmatch
6939 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006942 status_redraw_all(); // redraw to remove the ruler
6943 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944#endif
6945#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006946 p_ri = 0; // no reverse insert
6947 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006949 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 p_tw = 0;
6951 p_wm = 0;
6952 p_sts = 0;
6953 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006954#ifdef FEAT_VARTABS
6955 if (p_vsts)
6956 free_string_option(p_vsts);
6957 p_vsts = empty_option;
6958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960
6961 /*
6962 * Paste switched from on to off: Restore saved values.
6963 */
6964 else if (old_p_paste)
6965 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006966 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006967 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {
6969 buf->b_p_tw = buf->b_p_tw_nopaste;
6970 buf->b_p_wm = buf->b_p_wm_nopaste;
6971 buf->b_p_sts = buf->b_p_sts_nopaste;
6972 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006973 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006974#ifdef FEAT_VARTABS
6975 if (buf->b_p_vsts)
6976 free_string_option(buf->b_p_vsts);
6977 buf->b_p_vsts = buf->b_p_vsts_nopaste
6978 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006979 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006980 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006981 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006982 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006983 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 }
6986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006987 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006989 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006992 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 p_ru = save_ru;
6994#endif
6995#ifdef FEAT_RIGHTLEFT
6996 p_ri = save_ri;
6997 p_hkmap = save_hkmap;
6998#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006999 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007000 p_ai = p_ai_nopaste;
7001 p_et = p_et_nopaste;
7002 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 p_tw = p_tw_nopaste;
7004 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007005#ifdef FEAT_VARTABS
7006 if (p_vsts)
7007 free_string_option(p_vsts);
7008 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 }
7011
7012 old_p_paste = p_paste;
7013}
7014
7015/*
7016 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7017 *
7018 * Reset 'compatible' and set the values for options that didn't get set yet
7019 * to the Vim defaults.
7020 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007021 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 */
7023 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007024vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007026 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007027 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007028 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029
7030 if (!option_was_set((char_u *)"cp"))
7031 {
7032 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007033 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7035 set_option_default(opt_idx, OPT_FREE, FALSE);
7036 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007037 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007039
7040 if (fname != NULL)
7041 {
7042 p = vim_getenv(envname, &dofree);
7043 if (p == NULL)
7044 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007045 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007046 p = FullName_save(fname, FALSE);
7047 if (p != NULL)
7048 {
7049 vim_setenv(envname, p);
7050 vim_free(p);
7051 }
7052 }
7053 else if (dofree)
7054 vim_free(p);
7055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056}
7057
7058/*
7059 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7060 */
7061 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007062change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007064 int opt_idx;
7065
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 if (p_cp != on)
7067 {
7068 p_cp = on;
7069 compatible_set();
7070 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007071 opt_idx = findoption((char_u *)"cp");
7072 if (opt_idx >= 0)
7073 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074}
7075
7076/*
7077 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007078 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 */
7080 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007081option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082{
7083 int idx;
7084
7085 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007086 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 return FALSE;
7088 if (options[idx].flags & P_WAS_SET)
7089 return TRUE;
7090 return FALSE;
7091}
7092
7093/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007094 * Reset the flag indicating option "name" was set.
7095 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007096 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007097reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007098{
7099 int idx = findoption(name);
7100
7101 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007102 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007103 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007104 return OK;
7105 }
7106 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007107}
7108
7109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 * compatible_set() - Called when 'compatible' has been set or unset.
7111 *
7112 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7113 * flag) to a Vi compatible value.
7114 * When 'compatible' is unset: Set all options that have a different default
7115 * for Vim (without the P_VI_DEF flag) to that default.
7116 */
7117 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007118compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119{
7120 int opt_idx;
7121
Bram Moolenaardac13472019-09-16 21:06:21 +02007122 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7124 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7125 set_option_default(opt_idx, OPT_FREE, p_cp);
7126 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007127 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128}
7129
Bram Moolenaardac13472019-09-16 21:06:21 +02007130#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132/*
7133 * fill_breakat_flags() -- called when 'breakat' changes value.
7134 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007135 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007136fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007138 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 int i;
7140
7141 for (i = 0; i < 256; i++)
7142 breakat_flags[i] = FALSE;
7143
7144 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007145 for (p = p_breakat; *p; p++)
7146 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148#endif
7149
7150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 * Check if backspacing over something is allowed.
7152 */
7153 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007154can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007155 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007157#ifdef FEAT_JOB_CHANNEL
7158 if (what == BS_START && bt_prompt(curbuf))
7159 return FALSE;
7160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 switch (*p_bs)
7162 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007163 case '3': return TRUE;
7164 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 case '1': return (what != BS_START);
7166 case '0': return FALSE;
7167 }
7168 return vim_strchr(p_bs, what) != NULL;
7169}
7170
7171/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007172 * Return the effective 'scrolloff' value for the current window, using the
7173 * global value when appropriate.
7174 */
7175 long
7176get_scrolloff_value(void)
7177{
7178 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7179}
7180
7181/*
7182 * Return the effective 'sidescrolloff' value for the current window, using the
7183 * global value when appropriate.
7184 */
7185 long
7186get_sidescrolloff_value(void)
7187{
7188 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7189}
7190
7191/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007192 * Get the local or global value of 'backupcopy'.
7193 */
7194 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007195get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007196{
7197 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7198}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007199
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007200#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007201/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007202 * Get the local or global value of 'formatlistpat'.
7203 */
7204 char_u *
7205get_flp_value(buf_T *buf)
7206{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007207 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7208 return p_flp;
7209 return buf->b_p_flp;
7210}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007211#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007212
7213/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007214 * Get the local or global value of the 'virtualedit' flags.
7215 */
7216 unsigned int
7217get_ve_flags(void)
7218{
Gary Johnson51ad8502021-08-03 18:33:08 +02007219 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7220 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007221}
7222
Bram Moolenaaree857022019-11-09 23:26:40 +01007223#if defined(FEAT_LINEBREAK) || defined(PROTO)
7224/*
7225 * Get the local or global value of 'showbreak'.
7226 */
7227 char_u *
7228get_showbreak_value(win_T *win)
7229{
7230 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7231 return p_sbr;
7232 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7233 return empty_option;
7234 return win->w_p_sbr;
7235}
7236#endif
7237
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007238#if defined(FEAT_EVAL) || defined(PROTO)
7239/*
7240 * Get window or buffer local options.
7241 */
7242 dict_T *
7243get_winbuf_options(int bufopt)
7244{
7245 dict_T *d;
7246 int opt_idx;
7247
7248 d = dict_alloc();
7249 if (d == NULL)
7250 return NULL;
7251
Bram Moolenaardac13472019-09-16 21:06:21 +02007252 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007253 {
7254 struct vimoption *opt = &options[opt_idx];
7255
7256 if ((bufopt && (opt->indir & PV_BUF))
7257 || (!bufopt && (opt->indir & PV_WIN)))
7258 {
7259 char_u *varp = get_varp(opt);
7260
7261 if (varp != NULL)
7262 {
7263 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007264 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007265 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007266 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007267 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007268 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007269 }
7270 }
7271 }
7272
7273 return d;
7274}
7275#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007276
Bram Moolenaardac13472019-09-16 21:06:21 +02007277#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007278/*
7279 * This is called when 'culopt' is changed
7280 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007281 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007282fill_culopt_flags(char_u *val, win_T *wp)
7283{
7284 char_u *p;
7285 char_u culopt_flags_new = 0;
7286
7287 if (val == NULL)
7288 p = wp->w_p_culopt;
7289 else
7290 p = val;
7291 while (*p != NUL)
7292 {
7293 if (STRNCMP(p, "line", 4) == 0)
7294 {
7295 p += 4;
7296 culopt_flags_new |= CULOPT_LINE;
7297 }
7298 else if (STRNCMP(p, "both", 4) == 0)
7299 {
7300 p += 4;
7301 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7302 }
7303 else if (STRNCMP(p, "number", 6) == 0)
7304 {
7305 p += 6;
7306 culopt_flags_new |= CULOPT_NBR;
7307 }
7308 else if (STRNCMP(p, "screenline", 10) == 0)
7309 {
7310 p += 10;
7311 culopt_flags_new |= CULOPT_SCRLINE;
7312 }
7313
7314 if (*p != ',' && *p != NUL)
7315 return FAIL;
7316 if (*p == ',')
7317 ++p;
7318 }
7319
7320 // Can't have both "line" and "screenline".
7321 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7322 return FAIL;
7323 wp->w_p_culopt_flags = culopt_flags_new;
7324
7325 return OK;
7326}
7327#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007328
7329/*
7330 * Get the value of 'magic' adjusted for Vim9 script.
7331 */
7332 int
7333magic_isset(void)
7334{
7335 switch (magic_overruled)
7336 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007337 case OPTION_MAGIC_ON: return TRUE;
7338 case OPTION_MAGIC_OFF: return FALSE;
7339 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007340 }
7341#ifdef FEAT_EVAL
7342 if (in_vim9script())
7343 return TRUE;
7344#endif
7345 return p_magic;
7346}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007347
7348/*
7349 * Set the callback function value for an option that accepts a function name,
7350 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7351 * Returns OK if the option is successfully set to a function, otherwise
7352 * returns FAIL.
7353 */
7354 int
7355option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7356{
7357#ifdef FEAT_EVAL
7358 typval_T *tv;
7359 callback_T cb;
7360
7361 if (optval == NULL || *optval == NUL)
7362 {
7363 free_callback(optcb);
7364 return OK;
7365 }
7366
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007367 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007368 || (STRNCMP(optval, "function(", 9) == 0)
7369 || (STRNCMP(optval, "funcref(", 8) == 0))
7370 // Lambda expression or a funcref
7371 tv = eval_expr(optval, NULL);
7372 else
7373 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007374 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007375 if (tv == NULL)
7376 return FAIL;
7377
7378 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007379 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007380 {
7381 free_tv(tv);
7382 return FAIL;
7383 }
7384
7385 free_callback(optcb);
7386 set_callback(optcb, &cb);
7387 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007388
7389 // when using Vim9 style "import.funcname" it needs to be expanded to
7390 // "import#funcname".
7391 expand_autload_callback(optcb);
7392
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007393 return OK;
7394#else
7395 return FAIL;
7396#endif
7397}