blob: 1b4b9da0d328877ced11ca8bb5bf6b7aaffda6a0 [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 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662}
663
664/*
665 * Set the Vi-default value of a string option.
666 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100667 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100669 static void
670set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
672 char_u *p;
673 int opt_idx;
674
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100675 if (escape && vim_strchr(val, ' ') != NULL)
676 p = vim_strsave_escaped(val, (char_u *)" ");
677 else
678 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100679 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {
681 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000682 if (opt_idx >= 0)
683 {
684 if (options[opt_idx].flags & P_DEF_ALLOCED)
685 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
686 options[opt_idx].def_val[VI_DEFAULT] = p;
687 options[opt_idx].flags |= P_DEF_ALLOCED;
688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
690}
691
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100692 void
693set_string_default(char *name, char_u *val)
694{
695 set_string_default_esc(name, val, FALSE);
696}
697
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200699 * For an option value that contains comma separated items, find "newval" in
700 * "origval". Return NULL if not found.
701 */
702 static char_u *
703find_dup_item(char_u *origval, char_u *newval, long_u flags)
704{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200705 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200706 size_t newlen;
707 char_u *s;
708
709 if (origval == NULL)
710 return NULL;
711
712 newlen = STRLEN(newval);
713 for (s = origval; *s != NUL; ++s)
714 {
715 if ((!(flags & P_COMMA)
716 || s == origval
717 || (s[-1] == ',' && !(bs & 1)))
718 && STRNCMP(s, newval, newlen) == 0
719 && (!(flags & P_COMMA)
720 || s[newlen] == ','
721 || s[newlen] == NUL))
722 return s;
723 // Count backslashes. Only a comma with an even number of backslashes
724 // or a single backslash preceded by a comma before it is recognized as
725 // a separator.
726 if ((s > origval + 1
727 && s[-1] == '\\'
728 && s[-2] != ',')
729 || (s == origval + 1
730 && s[-1] == '\\'))
731 ++bs;
732 else
733 bs = 0;
734 }
735 return NULL;
736}
737
738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 * Set the Vi-default value of a number option.
740 * Used for 'lines' and 'columns'.
741 */
742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100743set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000745 int opt_idx;
746
747 opt_idx = findoption((char_u *)name);
748 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000749 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750}
751
Dominique Pelle748b3082022-01-08 12:41:16 +0000752#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200753/*
754 * Set all window-local and buffer-local options to the Vim default.
755 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200756 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200757 */
758 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200759set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200760{
761 win_T *save_curwin = curwin;
762 int i;
763
764 curwin = wp;
765 curbuf = curwin->w_buffer;
766 block_autocmds();
767
Bram Moolenaardac13472019-09-16 21:06:21 +0200768 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200769 {
770 struct vimoption *p = &(options[i]);
771 char_u *varp = get_varp_scope(p, OPT_LOCAL);
772
773 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200774 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200775 && !(options[i].flags & P_NODEFAULT)
776 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200777 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200778 }
779
780 unblock_autocmds();
781 curwin = save_curwin;
782 curbuf = curwin->w_buffer;
783}
Dominique Pelle748b3082022-01-08 12:41:16 +0000784#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200785
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000786#if defined(EXITFREE) || defined(PROTO)
787/*
788 * Free all options.
789 */
790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100791free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000792{
793 int i;
794
Bram Moolenaardac13472019-09-16 21:06:21 +0200795 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000796 {
797 if (options[i].indir == PV_NONE)
798 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100799 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100800 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000801 free_string_option(*(char_u **)options[i].var);
802 if (options[i].flags & P_DEF_ALLOCED)
803 free_string_option(options[i].def_val[VI_DEFAULT]);
804 }
805 else if (options[i].var != VAR_WIN
806 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100807 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200808 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000809 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000810 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000811 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000812}
813#endif
814
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816/*
817 * Initialize the options, part two: After getting Rows and Columns and
818 * setting 'term'.
819 */
820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100821set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000823 int idx;
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100826 * 'scroll' defaults to half the window height. The stored default is zero,
827 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000829 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000830 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000831 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 comp_col();
833
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000834 /*
835 * 'window' is only for backwards compatibility with Vi.
836 * Default is Rows - 1.
837 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000838 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000839 p_window = Rows - 1;
840 set_number_default("window", Rows - 1);
841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100842 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100843#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000844 /*
845 * If 'background' wasn't set by the user, try guessing the value,
846 * depending on the terminal name. Only need to check for terminals
847 * with a dark background, that can handle color.
848 */
849 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000850 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
851 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000853 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 // don't mark it as set, when starting the GUI it may be
855 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000856 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000859
860#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000862#endif
863#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000865#endif
866#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100867 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869}
870
871/*
872 * Initialize the options, part three: After reading the .vimrc
873 */
874 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100875set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100877#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878/*
879 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
880 * This is done after other initializations, where 'shell' might have been
881 * set, but only if they have not been set before.
882 */
883 char_u *p;
884 int idx_srr;
885 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100886# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 int idx_sp;
888 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
891 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000892 if (idx_srr < 0)
893 do_srr = FALSE;
894 else
895 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100896# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000898 if (idx_sp < 0)
899 do_sp = FALSE;
900 else
901 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100902# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200903 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (p != NULL)
905 {
906 /*
907 * Default for p_sp is "| tee", for p_srr is ">".
908 * For known shells it is changed here to include stderr.
909 */
910 if ( fnamecmp(p, "csh") == 0
911 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100912# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 || fnamecmp(p, "csh.exe") == 0
914 || fnamecmp(p, "tcsh.exe") == 0
915# endif
916 )
917 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100918# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (do_sp)
920 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100921# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100923# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
927 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (do_srr)
930 {
931 p_srr = (char_u *)">&";
932 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
933 }
934 }
Mike Williams12795022021-06-28 20:53:58 +0200935# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200936 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
937 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200938 else if ( fnamecmp(p, "powershell") == 0
939 || fnamecmp(p, "powershell.exe") == 0
940 )
941 {
942# if defined(FEAT_QUICKFIX)
943 if (do_sp)
944 {
945 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
946 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
947 }
948# endif
949 if (do_srr)
950 {
951 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
952 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
953 }
954 }
955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 else
Natanael Copa56318362021-05-06 18:46:35 +0200957 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if ( fnamecmp(p, "sh") == 0
959 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200960 || fnamecmp(p, "mksh") == 0
961 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000963 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200965 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200966 || fnamecmp(p, "ash") == 0
967 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200968 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100969# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 || fnamecmp(p, "cmd") == 0
971 || fnamecmp(p, "sh.exe") == 0
972 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200973 || fnamecmp(p, "mksh.exe") == 0
974 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000976 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 || fnamecmp(p, "bash.exe") == 0
978 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200979 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200980 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100982 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100984# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (do_sp)
986 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100987# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100989# else
Mike Williamsa3d1b292021-06-30 20:56:00 +0200990 if (fnamecmp(p, "pwsh") == 0)
991 p_sp = (char_u *)">%s 2>&1";
992 else
993 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
996 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (do_srr)
999 {
1000 p_srr = (char_u *)">%s 2>&1";
1001 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1002 }
1003 }
1004 vim_free(p);
1005 }
1006#endif
1007
Bram Moolenaar4f974752019-02-17 17:44:42 +01001008#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001010 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1011 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001013 * set, but only if they have not been set before.
1014 * Default values depend on shell (cmd.exe is default shell):
1015 *
1016 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001017 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001018 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001019 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001020 * "sh" like shells - "-c" "\""
1021 *
1022 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 */
Mike Williams12795022021-06-28 20:53:58 +02001024 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1025 {
1026 int idx_opt;
1027
1028 idx_opt = findoption((char_u *)"shcf");
1029 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1030 {
1031 p_shcf = (char_u*)"-Command";
1032 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1033 }
1034
1035 idx_opt = findoption((char_u *)"sxq");
1036 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1037 {
1038 p_sxq = (char_u*)"\"";
1039 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1040 }
1041 }
1042 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 int idx3;
1045
1046 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001047 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
1049 p_shcf = (char_u *)"-c";
1050 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1051 }
1052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001053 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001055 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
1057 p_sxq = (char_u *)"\"";
1058 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001061 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1062 {
1063 int idx3;
1064
1065 /*
1066 * cmd.exe on Windows will strip the first and last double quote given
1067 * on the command line, e.g. most of the time things like:
1068 * cmd /c "my path/to/echo" "my args to echo"
1069 * become:
1070 * my path/to/echo" "my args to echo
1071 * when executed.
1072 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001073 * To avoid this, set shellxquote to surround the command in
1074 * parenthesis. This appears to make most commands work, without
1075 * breaking commands that worked previously, such as
1076 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001077 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001078 idx3 = findoption((char_u *)"sxq");
1079 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1080 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001081 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001082 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1083 }
1084
Bram Moolenaara64ba222012-02-12 23:23:31 +01001085 idx3 = findoption((char_u *)"shcf");
1086 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1087 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001088 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001089 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1090 }
1091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#endif
1093
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001094 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001095 {
1096 int idx_ffs = findoption((char_u *)"ffs");
1097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001098 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001099 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1100 set_fileformat(default_fileformat(), OPT_LOCAL);
1101 }
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104}
1105
1106#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1107/*
1108 * When 'helplang' is still at its default value, set it to "lang".
1109 * Only the first two characters of "lang" are used.
1110 */
1111 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001112set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
1114 int idx;
1115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001116 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 return;
1118 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001119 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
1121 if (options[idx].flags & P_ALLOCED)
1122 free_string_option(p_hlg);
1123 p_hlg = vim_strsave(lang);
1124 if (p_hlg == NULL)
1125 p_hlg = empty_option;
1126 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001127 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001128 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001129 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1130 {
1131 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1132 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1133 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001134 // any C like setting, such as C.UTF-8, becomes "en"
1135 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1136 {
1137 p_hlg[0] = 'e';
1138 p_hlg[1] = 'n';
1139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 options[idx].flags |= P_ALLOCED;
1143 }
1144}
1145#endif
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147/*
1148 * 'title' and 'icon' only default to true if they have not been set or reset
1149 * in .vimrc and we can read the old value.
1150 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1151 * they can be reset. This reduces startup time when using X on a remote
1152 * machine.
1153 */
1154 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001155set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
1157 int idx1;
1158 long val;
1159
1160 /*
1161 * If GUI is (going to be) used, we can always set the window title and
1162 * icon name. Saves a bit of time, because the X11 display server does
1163 * not need to be contacted.
1164 */
1165 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001166 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
1168#ifdef FEAT_GUI
1169 if (gui.starting || gui.in_use)
1170 val = TRUE;
1171 else
1172#endif
1173 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001174 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 p_title = val;
1176 }
1177 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001178 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
1180#ifdef FEAT_GUI
1181 if (gui.starting || gui.in_use)
1182 val = TRUE;
1183 else
1184#endif
1185 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001186 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 p_icon = val;
1188 }
1189}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001191 void
1192ex_set(exarg_T *eap)
1193{
1194 int flags = 0;
1195
1196 if (eap->cmdidx == CMD_setlocal)
1197 flags = OPT_LOCAL;
1198 else if (eap->cmdidx == CMD_setglobal)
1199 flags = OPT_GLOBAL;
1200#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001201 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001202 ex_options(eap);
1203 else
1204#endif
1205 {
1206 if (eap->forceit)
1207 flags |= OPT_ONECOLUMN;
1208 (void)do_set(eap->arg, flags);
1209 }
1210}
1211
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212/*
1213 * Parse 'arg' for option settings.
1214 *
1215 * 'arg' may be IObuff, but only when no errors can be present and option
1216 * does not need to be expanded with option_expand().
1217 * "opt_flags":
1218 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001219 * OPT_GLOBAL for ":setglobal"
1220 * OPT_LOCAL for ":setlocal" and a modeline
1221 * OPT_MODELINE for a modeline
1222 * OPT_WINONLY to only set window-local options
1223 * OPT_NOWIN to skip setting window-local options
1224 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 *
1226 * returns FAIL if an error is detected, OK otherwise
1227 */
1228 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001229do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02001230 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001231 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232{
Bram Moolenaar1594f312021-07-08 16:40:13 +02001233 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001235 char *errmsg;
1236 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001238 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1239 int nextchar; // next non-white char after option name
1240 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 int len;
1242 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001243 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001245 long_u flags; // flags for current option
1246 char_u *varp = NULL; // pointer to variable for current option
1247 int did_show = FALSE; // already showed one value
1248 int adding; // "opt+=arg"
1249 int prepending; // "opt^=arg"
1250 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 int cp_val = 0;
1252 char_u key_name[2];
1253
1254 if (*arg == NUL)
1255 {
1256 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001257 did_show = TRUE;
1258 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 }
1260
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001261 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
1263 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001264 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001266 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1267 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
1269 /*
1270 * ":set all" show all options.
1271 * ":set all&" set all options to their default value.
1272 */
1273 arg += 3;
1274 if (*arg == '&')
1275 {
1276 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001277 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001279 didset_options();
1280 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001281 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001286 did_show = TRUE;
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001289 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {
1291 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001292 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001293 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 arg += 7;
1295 }
1296 else
1297 {
1298 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001299 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {
1301 prefix = 0;
1302 arg += 2;
1303 }
1304 else if (STRNCMP(arg, "inv", 3) == 0)
1305 {
1306 prefix = 2;
1307 arg += 3;
1308 }
1309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001310 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 key = 0;
1312 if (*arg == '<')
1313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001315 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1317 len = 5;
1318 else
1319 {
1320 len = 1;
1321 while (arg[len] != NUL && arg[len] != '>')
1322 ++len;
1323 }
1324 if (arg[len] != '>')
1325 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001326 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 goto skip;
1328 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001329 arg[len] = NUL; // put NUL after name
1330 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001332 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001334 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336 else
1337 {
1338 len = 0;
1339 /*
1340 * The two characters after "t_" may not be alphanumeric.
1341 */
1342 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1343 len = 4;
1344 else
1345 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1346 ++len;
1347 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001348 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001350 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001352 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001355 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 afterchar = arg[len];
1357
Bram Moolenaardeb108b2021-07-08 17:35:36 +02001358 if (in_vim9script())
1359 {
1360 char_u *p = skipwhite(arg + len);
1361
1362 // disallow white space before =val, +=val, -=val, ^=val
1363 if (p > arg + len && (p[0] == '='
1364 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1365 && p[1] == '=')))
1366 {
1367 errmsg = e_no_white_space_allowed_between_option_and;
1368 arg = p;
1369 startarg = p;
1370 goto skip;
1371 }
1372 }
1373 else
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001374 // skip white space, allow ":set ai ?", ":set hlsearch !"
1375 while (VIM_ISWHITE(arg[len]))
1376 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
1378 adding = FALSE;
1379 prepending = FALSE;
1380 removing = FALSE;
1381 if (arg[len] != NUL && arg[len + 1] == '=')
1382 {
1383 if (arg[len] == '+')
1384 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001385 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 ++len;
1387 }
1388 else if (arg[len] == '^')
1389 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001390 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 ++len;
1392 }
1393 else if (arg[len] == '-')
1394 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001395 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 ++len;
1397 }
1398 }
1399 nextchar = arg[len];
1400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001401 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 {
Bram Moolenaar1594f312021-07-08 16:40:13 +02001403 if (in_vim9script() && arg > arg_start
1404 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1405 errmsg = e_no_white_space_allowed_between_option_and;
1406 else
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001407 errmsg = e_unknown_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 goto skip;
1409 }
1410
1411 if (opt_idx >= 0)
1412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001413 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001415 // Only give an error message when requesting the value of
1416 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1418 && (!(options[opt_idx].flags & P_BOOL)
1419 || nextchar == '?'))
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001420 errmsg = e_option_not_supported;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 goto skip;
1422 }
1423
1424 flags = options[opt_idx].flags;
1425 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1426 }
1427 else
1428 {
1429 flags = P_STRING;
1430 if (key < 0)
1431 {
1432 key_name[0] = KEY2TERMCAP0(key);
1433 key_name[1] = KEY2TERMCAP1(key);
1434 }
1435 else
1436 {
1437 key_name[0] = KS_KEY;
1438 key_name[1] = (key & 0xff);
1439 }
1440 }
1441
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001442 // Skip all options that are not window-local (used when showing
1443 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001444 if ((opt_flags & OPT_WINONLY)
1445 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1446 goto skip;
1447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001448 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001449 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1450 && options[opt_idx].var == VAR_WIN)
1451 goto skip;
1452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001453 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001454 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001456 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001457 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001458 errmsg = e_not_allowed_in_modeline;
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001459 goto skip;
1460 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001461 if ((flags & P_MLE) && !p_mle)
1462 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001463 errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
Bram Moolenaar110289e2019-05-23 15:38:06 +02001464 goto skip;
1465 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001466#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001467 // In diff mode some options are overruled. This avoids that
1468 // 'foldmethod' becomes "marker" instead of "diff" and that
1469 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001470 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001471 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001472 && (
1473#ifdef FEAT_FOLDING
1474 options[opt_idx].indir == PV_FDM ||
1475#endif
1476 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001477 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 }
1480
1481#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001482 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001483 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001485 errmsg = e_not_allowed_in_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 goto skip;
1487 }
1488#endif
1489
1490 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1491 {
1492 arg += len;
1493 cp_val = p_cp;
1494 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1495 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001496 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
1498 cp_val = FALSE;
1499 arg += 3;
1500 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001501 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
1503 cp_val = TRUE;
1504 arg += 2;
1505 }
1506 }
1507 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001508 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001510 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 goto skip;
1512 }
1513 }
1514
1515 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001516 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001517 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 */
1519 if (nextchar == '?'
1520 || (prefix == 1
1521 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1522 && !(flags & P_BOOL)))
1523 {
1524 /*
1525 * print value
1526 */
1527 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001528 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 else
1530 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001531 gotocmdline(TRUE); // cursor at status line
1532 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534 if (opt_idx >= 0)
1535 {
1536 showoneopt(&options[opt_idx], opt_flags);
1537#ifdef FEAT_EVAL
1538 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001539 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001540 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001541 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001542 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001543 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001544 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001545 (int)options[opt_idx].indir & PV_MASK]);
1546 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001547 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001548 (int)options[opt_idx].indir & PV_MASK]);
1549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550#endif
1551 }
1552 else
1553 {
1554 char_u *p;
1555
1556 p = find_termcode(key_name);
1557 if (p == NULL)
1558 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001559 errmsg = e_key_code_not_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 goto skip;
1561 }
1562 else
1563 (void)show_one_termcode(key_name, p, TRUE);
1564 }
1565 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001566 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001567 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
1569 else
1570 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001571 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001572 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001573
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001574 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 {
1576 if (nextchar == '=' || nextchar == ':')
1577 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001578 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 goto skip;
1580 }
1581
1582 /*
1583 * ":set opt!": invert
1584 * ":set opt&": reset to default value
1585 * ":set opt<": reset to global value
1586 */
1587 if (nextchar == '!')
1588 value = *(int *)(varp) ^ 1;
1589 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001590 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 ((flags & P_VI_DEF) || cp_val)
1592 ? VI_DEFAULT : VIM_DEFAULT];
1593 else if (nextchar == '<')
1594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001595 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if ((int *)varp == &curbuf->b_p_ar
1597 && opt_flags == OPT_LOCAL)
1598 value = -1;
1599 else
1600 value = *(int *)get_varp_scope(&(options[opt_idx]),
1601 OPT_GLOBAL);
1602 }
1603 else
1604 {
1605 /*
1606 * ":set invopt": invert
1607 * ":set opt" or ":set noopt": set or reset
1608 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001609 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001611 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 goto skip;
1613 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001614 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 value = *(int *)(varp) ^ 1;
1616 else
1617 value = prefix;
1618 }
1619
1620 errmsg = set_bool_option(opt_idx, varp, (int)value,
1621 opt_flags);
1622 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001623 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1626 || prefix != 1)
1627 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001628 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 goto skip;
1630 }
1631
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001632 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
1634 /*
1635 * Different ways to set a number option:
1636 * & set to default value
1637 * < set to global value
1638 * <xx> accept special key codes for 'wildchar'
1639 * c accept any non-digit for 'wildchar'
1640 * [-]0-9 set number
1641 * other error
1642 */
1643 ++arg;
1644 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001645 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 ((flags & P_VI_DEF) || cp_val)
1647 ? VI_DEFAULT : VIM_DEFAULT];
1648 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001649 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001650 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1651 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001652 if ((long *)varp == &curbuf->b_p_ul
1653 && opt_flags == OPT_LOCAL)
1654 value = NO_LOCAL_UNDOLEVEL;
1655 else
1656 value = *(long *)get_varp_scope(
1657 &(options[opt_idx]), OPT_GLOBAL);
1658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 else if (((long *)varp == &p_wc
1660 || (long *)varp == &p_wcm)
1661 && (*arg == '<'
1662 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001663 || (*arg != NUL
1664 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 && !VIM_ISDIGIT(*arg))))
1666 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001667 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if (value == 0 && (long *)varp != &p_wcm)
1669 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001670 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 goto skip;
1672 }
1673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1675 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001676 // Allow negative (for 'undolevels'), octal and
1677 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001678 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001679 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001680 if (i == 0 || (arg[i] != NUL
1681 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001683 errmsg = e_number_required_after_equal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 goto skip;
1685 }
1686 }
1687 else
1688 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001689 errmsg = e_number_required_after_equal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 goto skip;
1691 }
1692
1693 if (adding)
1694 value = *(long *)varp + value;
1695 if (prepending)
1696 value = *(long *)varp * value;
1697 if (removing)
1698 value = *(long *)varp - value;
1699 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001700 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001702 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001704 char_u *save_arg = NULL;
1705 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001706 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001707 char_u *newval;
1708 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001709 char_u *origval_l = NULL;
1710 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001711#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001712 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001713 char_u *saved_origval_l = NULL;
1714 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001715 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001716#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001717 unsigned newlen;
1718 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001719 int new_value_alloced; // new string option
1720 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001722 // When using ":set opt=val" for a global option
1723 // with a local value the local value will be
1724 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001726 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 varp = options[opt_idx].var;
1728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001729 // The old value is kept until we are sure that the
1730 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001732
Bram Moolenaard7c96872019-06-15 17:12:48 +02001733 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1734 {
1735 origval_l = *(char_u **)get_varp_scope(
1736 &(options[opt_idx]), OPT_LOCAL);
1737 origval_g = *(char_u **)get_varp_scope(
1738 &(options[opt_idx]), OPT_GLOBAL);
1739
1740 // A global-local string option might have an empty
1741 // option as value to indicate that the global
1742 // value should be used.
1743 if (((int)options[opt_idx].indir & PV_BOTH)
1744 && origval_l == empty_option)
1745 origval_l = origval_g;
1746 }
1747
1748 // When setting the local value of a global
1749 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001750 if (((int)options[opt_idx].indir & PV_BOTH)
1751 && (opt_flags & OPT_LOCAL))
1752 origval = *(char_u **)get_varp(
1753 &options[opt_idx]);
1754 else
1755 origval = oldval;
1756
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001757 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
1759 newval = options[opt_idx].def_val[
1760 ((flags & P_VI_DEF) || cp_val)
1761 ? VI_DEFAULT : VIM_DEFAULT];
1762 if ((char_u **)varp == &p_bg)
1763 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001764 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#ifdef FEAT_GUI
1766 if (gui.in_use)
1767 newval = gui_bg_default();
1768 else
1769#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001770 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001772 else if ((char_u **)varp == &p_fencs && enc_utf8)
1773 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001775 // expand environment variables and ~ since the
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001776 // default value was already expanded, only
1777 // required when an environment variable was set
1778 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 if (newval == NULL)
1780 newval = empty_option;
1781 else
1782 {
1783 s = option_expand(opt_idx, newval);
1784 if (s == NULL)
1785 s = newval;
1786 newval = vim_strsave(s);
1787 }
1788 new_value_alloced = TRUE;
1789 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001790 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
1792 newval = vim_strsave(*(char_u **)get_varp_scope(
1793 &(options[opt_idx]), OPT_GLOBAL));
1794 new_value_alloced = TRUE;
1795 }
1796 else
1797 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001798 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
1800 /*
1801 * Set 'keywordprg' to ":help" if an empty
1802 * value was passed to :set by the user.
1803 * Misuse errbuf[] for the resulting string.
1804 */
1805 if (varp == (char_u *)&p_kp
1806 && (*arg == NUL || *arg == ' '))
1807 {
1808 STRCPY(errbuf, ":help");
1809 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001810 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001813 * Convert 'backspace' number to string, for
1814 * adding, prepending and removing string.
1815 */
1816 else if (varp == (char_u *)&p_bs
1817 && VIM_ISDIGIT(**(char_u **)varp))
1818 {
1819 i = getdigits((char_u **)varp);
1820 switch (i)
1821 {
1822 case 0:
1823 *(char_u **)varp = empty_option;
1824 break;
1825 case 1:
1826 *(char_u **)varp = vim_strsave(
1827 (char_u *)"indent,eol");
1828 break;
1829 case 2:
1830 *(char_u **)varp = vim_strsave(
1831 (char_u *)"indent,eol,start");
1832 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001833 case 3:
1834 *(char_u **)varp = vim_strsave(
1835 (char_u *)"indent,eol,nostop");
1836 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001837 }
1838 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001839 if (origval == oldval)
1840 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001841 if (origval_l == oldval)
1842 origval_l = *(char_u **)varp;
1843 if (origval_g == oldval)
1844 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001845 oldval = *(char_u **)varp;
1846 }
1847 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 * Convert 'whichwrap' number to string, for
1849 * backwards compatibility with Vim 3.0.
1850 * Misuse errbuf[] for the resulting string.
1851 */
1852 else if (varp == (char_u *)&p_ww
1853 && VIM_ISDIGIT(*arg))
1854 {
1855 *errbuf = NUL;
1856 i = getdigits(&arg);
1857 if (i & 1)
1858 STRCAT(errbuf, "b,");
1859 if (i & 2)
1860 STRCAT(errbuf, "s,");
1861 if (i & 4)
1862 STRCAT(errbuf, "h,l,");
1863 if (i & 8)
1864 STRCAT(errbuf, "<,>,");
1865 if (i & 16)
1866 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001867 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 errbuf[STRLEN(errbuf) - 1] = NUL;
1869 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001870 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 /*
1873 * Remove '>' before 'dir' and 'bdir', for
1874 * backwards compatibility with version 3.0
1875 */
1876 else if ( *arg == '>'
1877 && (varp == (char_u *)&p_dir
1878 || varp == (char_u *)&p_bdir))
1879 {
1880 ++arg;
1881 }
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 /*
1884 * Copy the new string into allocated memory.
1885 * Can't use set_string_option_direct(), because
1886 * we need to remove the backslashes.
1887 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001888 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 newlen = (unsigned)STRLEN(arg) + 1;
1890 if (adding || prepending || removing)
1891 newlen += (unsigned)STRLEN(origval) + 1;
1892 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001893 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 break;
1895 s = newval;
1896
1897 /*
1898 * Copy the string, skip over escaped chars.
1899 * For MS-DOS and WIN32 backslashes before normal
1900 * file name characters are not removed, and keep
1901 * backslash at start, for "\\machine\path", but
1902 * do remove it for "\\\\machine\\path".
1903 * The reverse is found in ExpandOldSetting().
1904 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001905 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
1907 if (*arg == '\\' && arg[1] != NUL
1908#ifdef BACKSLASH_IN_FILENAME
1909 && !((flags & P_EXPAND)
1910 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001911 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 && (arg[1] != '\\'
1913 || (s == newval
1914 && arg[2] != '\\')))
1915#endif
1916 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001917 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001919 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001921 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 mch_memmove(s, arg, (size_t)i);
1923 arg += i;
1924 s += i;
1925 }
1926 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 *s++ = *arg++;
1928 }
1929 *s = NUL;
1930
1931 /*
1932 * Expand environment variables and ~.
1933 * Don't do it when adding without inserting a
1934 * comma.
1935 */
1936 if (!(adding || prepending || removing)
1937 || (flags & P_COMMA))
1938 {
1939 s = option_expand(opt_idx, newval);
1940 if (s != NULL)
1941 {
1942 vim_free(newval);
1943 newlen = (unsigned)STRLEN(s) + 1;
1944 if (adding || prepending || removing)
1945 newlen += (unsigned)STRLEN(origval) + 1;
1946 newval = alloc(newlen);
1947 if (newval == NULL)
1948 break;
1949 STRCPY(newval, s);
1950 }
1951 }
1952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001953 // locate newval[] in origval[] when removing it
1954 // and when adding to avoid duplicates
1955 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (removing || (flags & P_NODUP))
1957 {
1958 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001959 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001961 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001962 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
1964 prepending = FALSE;
1965 adding = FALSE;
1966 STRCPY(newval, origval);
1967 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001968
1969 // if no duplicate, move pointer to end of
1970 // original value
1971 if (s == NULL)
1972 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 }
1974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001975 // concatenate the two strings; add a ',' if
1976 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 if (adding || prepending)
1978 {
1979 comma = ((flags & P_COMMA) && *origval != NUL
1980 && *newval != NUL);
1981 if (adding)
1982 {
1983 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001984 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001985 if (comma && i > 1
1986 && (flags & P_ONECOMMA) == P_ONECOMMA
1987 && origval[i - 1] == ','
1988 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001989 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 mch_memmove(newval + i + comma, newval,
1991 STRLEN(newval) + 1);
1992 mch_memmove(newval, origval, (size_t)i);
1993 }
1994 else
1995 {
1996 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001997 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 if (comma)
2000 newval[i] = ',';
2001 }
2002
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002003 // Remove newval[] from origval[]. (Note: "i" has
2004 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 if (removing)
2006 {
2007 STRCPY(newval, origval);
2008 if (*s)
2009 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002010 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if (flags & P_COMMA)
2012 {
2013 if (s == origval)
2014 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002015 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (s[i] == ',')
2017 ++i;
2018 }
2019 else
2020 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002021 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 --s;
2023 ++i;
2024 }
2025 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002026 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
2028 }
2029
2030 if (flags & P_FLAGLIST)
2031 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002032 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002033 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002034 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002035 // if options have P_FLAGLIST and
2036 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002037 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002039 if (*s != ',' && *(s + 1) == ','
2040 && vim_strchr(s + 2, *s) != NULL)
2041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002042 // Remove the duplicated value and
2043 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002044 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002045 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002048 else
2049 {
2050 if ((!(flags & P_COMMA) || *s != ',')
2051 && vim_strchr(s + 1, *s) != NULL)
2052 {
2053 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002054 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002055 }
2056 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002057 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002061 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 arg = save_arg;
2063 new_value_alloced = TRUE;
2064 }
2065
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002066 /*
2067 * Set the new value.
2068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 *(char_u **)(varp) = newval;
2070
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002071#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002072 if (!starting
2073# ifdef FEAT_CRYPT
2074 && options[opt_idx].indir != PV_KEY
2075# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002076 && origval != NULL && newval != NULL)
2077 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002078 // origval may be freed by
2079 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002080 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002081 // newval (and varp) may become invalid if the
2082 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002083 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002084 if (origval_l != NULL)
2085 saved_origval_l = vim_strsave(origval_l);
2086 if (origval_g != NULL)
2087 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002088 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002089#endif
2090
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002091 {
2092 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002093 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002094
2095 // When an option is set in the sandbox, from a
2096 // modeline or in secure mode, then deal with side
2097 // effects in secure mode. Also when the value was
2098 // set with the P_INSECURE flag and is not
2099 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002100 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002101#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002102 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002103#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002104 || (!value_is_replaced && (*p & P_INSECURE)))
2105 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002106
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002107 // Handle side effects, and set the global value
2108 // for ":set" on local options. Note: when setting
2109 // 'syntax' or 'filetype' autocommands may be
2110 // triggered that can cause havoc.
2111 errmsg = did_set_string_option(
2112 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002113 new_value_alloced, oldval, errbuf,
2114 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002115
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002116 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002119#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002120 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002121 trigger_optionsset_string(
2122 opt_idx, opt_flags, saved_origval,
2123 saved_origval_l, saved_origval_g,
2124 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002125 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002126 vim_free(saved_origval_l);
2127 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002128 vim_free(saved_newval);
2129#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002130 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 if (errmsg != NULL)
2132 goto skip;
2133 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002134 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
2136 char_u *p;
2137
2138 if (nextchar == '&')
2139 {
2140 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002141 errmsg = e_not_found_in_termcap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143 else
2144 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002145 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002146 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (*p == '\\' && p[1] != NUL)
2148 ++p;
2149 nextchar = *p;
2150 *p = NUL;
2151 add_termcode(key_name, arg, FALSE);
2152 *p = nextchar;
2153 }
2154 if (full_screen)
2155 ttest(FALSE);
2156 redraw_all_later(CLEAR);
2157 }
2158 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002159
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002161 did_set_option(
2162 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164
2165skip:
2166 /*
2167 * Advance to next argument.
2168 * - skip until a blank found, taking care of backslashes
2169 * - skip blanks
2170 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2171 */
2172 for (i = 0; i < 2 ; ++i)
2173 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002174 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (*arg++ == '\\' && *arg != NUL)
2176 ++arg;
2177 arg = skipwhite(arg);
2178 if (*arg != '=')
2179 break;
2180 }
2181 }
2182
2183 if (errmsg != NULL)
2184 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002185 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002186 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (i + (arg - startarg) < IOSIZE)
2188 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002189 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 STRCAT(IObuff, ": ");
2191 mch_memmove(IObuff + i, startarg, (arg - startarg));
2192 IObuff[i + (arg - startarg)] = NUL;
2193 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002194 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 trans_characters(IObuff, IOSIZE);
2196
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002197 ++no_wait_return; // wait_return done later
2198 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 --no_wait_return;
2200
2201 return FAIL;
2202 }
2203
2204 arg = skipwhite(arg);
2205 }
2206
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002207theend:
2208 if (silent_mode && did_show)
2209 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002210 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002211 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002212 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002213 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002214 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002215 out_flush();
2216 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002217 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002218 }
2219
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 return OK;
2221}
2222
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002223/*
2224 * Call this when an option has been given a new value through a user command.
2225 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2226 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002227 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002228did_set_option(
2229 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002230 int opt_flags, // possibly with OPT_MODELINE
2231 int new_value, // value was replaced completely
2232 int value_checked) // value was checked to be safe, no need to set the
2233 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002234{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002235 long_u *p;
2236
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002237 options[opt_idx].flags |= P_WAS_SET;
2238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002239 // When an option is set in the sandbox, from a modeline or in secure mode
2240 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2241 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002242 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002243 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002244#ifdef HAVE_SANDBOX
2245 || sandbox != 0
2246#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002247 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002248 *p = *p | P_INSECURE;
2249 else if (new_value)
2250 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002251}
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253/*
2254 * Convert a key name or string into a key value.
2255 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002256 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002258 int
2259string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260{
2261 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002262 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 if (*arg == '^')
2264 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002265 if (multi_byte)
2266 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 return *arg;
2268}
2269
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270/*
2271 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2272 * maketitle() to create and display it.
2273 * When switching the title or icon off, call mch_restore_title() to get
2274 * the old value back.
2275 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002276 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002277did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
2279 if (starting != NO_SCREEN
2280#ifdef FEAT_GUI
2281 && !gui.starting
2282#endif
2283 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287/*
2288 * set_options_bin - called when 'bin' changes value.
2289 */
2290 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002291set_options_bin(
2292 int oldval,
2293 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002294 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
2296 /*
2297 * The option values that are changed when 'bin' changes are
2298 * copied when 'bin is set and restored when 'bin' is reset.
2299 */
2300 if (newval)
2301 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002302 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 if (!(opt_flags & OPT_GLOBAL))
2305 {
2306 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2307 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2308 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2309 curbuf->b_p_et_nobin = curbuf->b_p_et;
2310 }
2311 if (!(opt_flags & OPT_LOCAL))
2312 {
2313 p_tw_nobin = p_tw;
2314 p_wm_nobin = p_wm;
2315 p_ml_nobin = p_ml;
2316 p_et_nobin = p_et;
2317 }
2318 }
2319
2320 if (!(opt_flags & OPT_GLOBAL))
2321 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002322 curbuf->b_p_tw = 0; // no automatic line wrap
2323 curbuf->b_p_wm = 0; // no automatic line wrap
2324 curbuf->b_p_ml = 0; // no modelines
2325 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327 if (!(opt_flags & OPT_LOCAL))
2328 {
2329 p_tw = 0;
2330 p_wm = 0;
2331 p_ml = FALSE;
2332 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002333 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002336 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
2338 if (!(opt_flags & OPT_GLOBAL))
2339 {
2340 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2341 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2342 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2343 curbuf->b_p_et = curbuf->b_p_et_nobin;
2344 }
2345 if (!(opt_flags & OPT_LOCAL))
2346 {
2347 p_tw = p_tw_nobin;
2348 p_wm = p_wm_nobin;
2349 p_ml = p_ml_nobin;
2350 p_et = p_et_nobin;
2351 }
2352 }
2353}
2354
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355/*
2356 * Expand environment variables for some string options.
2357 * These string options cannot be indirect!
2358 * If "val" is NULL expand the current value of the option.
2359 * Return pointer to NameBuff, or NULL when not expanded.
2360 */
2361 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002362option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002364 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2366 return NULL;
2367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002368 // If val is longer than MAXPATHL no meaningful expansion can be done,
2369 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 if (val != NULL && STRLEN(val) > MAXPATHL)
2371 return NULL;
2372
2373 if (val == NULL)
2374 val = *(char_u **)options[opt_idx].var;
2375
2376 /*
2377 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2378 * Escape spaces when expanding 'tags', they are used to separate file
2379 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002380 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 */
2382 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002383 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002384#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002385 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2386#endif
2387 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002388 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 return NULL;
2390
2391 return NameBuff;
2392}
2393
2394/*
2395 * After setting various option values: recompute variables that depend on
2396 * option values.
2397 */
2398 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002399didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002401 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 (void)init_chartab();
2403
Bram Moolenaardac13472019-09-16 21:06:21 +02002404 didset_string_options();
2405
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002406#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002407 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002408 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002409 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002410 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002413 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 (void)check_cedit();
2415#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002416#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002417 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002418 fill_breakat_flags();
2419#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002420 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002421}
2422
2423/*
2424 * More side effects of setting options.
2425 */
2426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002427didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002428{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002429 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002430 (void)highlight_changed();
2431
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002432 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002433 check_opt_wim();
2434
Bram Moolenaareed9d462021-02-15 20:38:25 +01002435 // Parse default for 'listchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002436 (void)set_chars_option(curwin, &curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002438 // Parse default for 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002439 (void)set_chars_option(curwin, &curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002440
2441#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002442 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002443 (void)check_clipboard_option();
2444#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002445#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002446 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002447 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002448 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002449 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451}
2452
2453/*
2454 * Check for string options that are NULL (normally only termcap options).
2455 */
2456 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002457check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
2459 int opt_idx;
2460
2461 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2462 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2463 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2464}
2465
2466/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002467 * Return the option index found by a pointer into term_strings[].
2468 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002470 int
2471get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002473 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2476 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002477 return opt_idx;
2478 return -1; // cannot happen: didn't find it!
2479}
2480
2481/*
2482 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2483 * Return the option index or -1 if not found.
2484 */
2485 int
2486set_term_option_alloced(char_u **p)
2487{
2488 int opt_idx = get_term_opt_idx(p);
2489
2490 if (opt_idx >= 0)
2491 options[opt_idx].flags |= P_ALLOCED;
2492 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493}
2494
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002495#if defined(FEAT_EVAL) || defined(PROTO)
2496/*
2497 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2498 * Return FALSE when it wasn't.
2499 * Return -1 for an unknown option.
2500 */
2501 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002502was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002503{
2504 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002505 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002506
2507 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002508 {
2509 flagp = insecure_flag(idx, opt_flags);
2510 return (*flagp & P_INSECURE) != 0;
2511 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002512 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002513 return -1;
2514}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002515
2516/*
2517 * Get a pointer to the flags used for the P_INSECURE flag of option
2518 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002519 * NOTE: Caller must make sure that "curwin" is set to the window from which
2520 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002521 */
2522 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002523insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002524{
2525 if (opt_flags & OPT_LOCAL)
2526 switch ((int)options[opt_idx].indir)
2527 {
2528#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002529 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002530#endif
2531#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002532# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002533 case PV_FDE: return &curwin->w_p_fde_flags;
2534 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002535# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002536# ifdef FEAT_BEVAL
2537 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2538# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002539 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002540 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002541# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002542 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002543# endif
2544#endif
2545 }
2546
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002547 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002548 return &options[opt_idx].flags;
2549}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002550#endif
2551
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002552/*
2553 * Redraw the window title and/or tab page text later.
2554 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002555void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002556{
2557 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002558 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002559}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002562 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2563 * characters or characters in "allowed".
2564 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002565 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002566valid_name(char_u *val, char *allowed)
2567{
2568 char_u *s;
2569
2570 for (s = val; *s != NUL; ++s)
2571 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2572 return FALSE;
2573 return TRUE;
2574}
2575
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002576#if defined(FEAT_EVAL) || defined(PROTO)
2577/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002578 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002579 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002580 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002581 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002582set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002583{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002584 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2585 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002586 sctx_T new_script_ctx = script_ctx;
2587
Bram Moolenaar51258742020-05-03 17:19:33 +02002588 // Modeline already has the line number set.
2589 if (!(opt_flags & OPT_MODELINE))
2590 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002591
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002592 // Remember where the option was set. For local options need to do that
2593 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002594 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002595 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002596 if (both || (opt_flags & OPT_LOCAL))
2597 {
2598 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002599 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002600 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002601 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002602 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002603 if (both)
2604 // also setting the "all buffers" value
2605 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2606 new_script_ctx;
2607 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002608 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002609}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002610
2611/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002612 * Get the script context of global option "name".
2613 *
2614 */
2615 sctx_T *
2616get_option_sctx(char *name)
2617{
2618 int idx = findoption((char_u *)name);
2619
2620 if (idx >= 0)
2621 return &options[idx].script_ctx;
2622 siemsg("no such option: %s", name);
2623 return NULL;
2624}
2625
2626/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002627 * Set the script_ctx for a termcap option.
2628 * "name" must be the two character code, e.g. "RV".
2629 * When "name" is NULL use "opt_idx".
2630 */
2631 void
2632set_term_option_sctx_idx(char *name, int opt_idx)
2633{
2634 char_u buf[5];
2635 int idx;
2636
2637 if (name == NULL)
2638 idx = opt_idx;
2639 else
2640 {
2641 buf[0] = 't';
2642 buf[1] = '_';
2643 buf[2] = name[0];
2644 buf[3] = name[1];
2645 buf[4] = 0;
2646 idx = findoption(buf);
2647 }
2648 if (idx >= 0)
2649 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2650}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002651#endif
2652
Bram Moolenaarf4140482020-02-15 23:06:45 +01002653#if defined(FEAT_EVAL)
2654/*
2655 * Apply the OptionSet autocommand.
2656 */
2657 static void
2658apply_optionset_autocmd(
2659 int opt_idx,
2660 long opt_flags,
2661 long oldval,
2662 long oldval_g,
2663 long newval,
2664 char *errmsg)
2665{
2666 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2667
2668 // Don't do this while starting up, failure or recursively.
2669 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2670 return;
2671
2672 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2673 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2674 oldval_g);
2675 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2676 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2677 (opt_flags & OPT_LOCAL) ? "local" : "global");
2678 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2679 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2680 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2681 if (opt_flags & OPT_LOCAL)
2682 {
2683 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2684 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2685 }
2686 if (opt_flags & OPT_GLOBAL)
2687 {
2688 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2689 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2690 }
2691 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2692 {
2693 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2694 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2695 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2696 }
2697 if (opt_flags & OPT_MODELINE)
2698 {
2699 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2700 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2701 }
2702 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2703 NULL, FALSE, NULL);
2704 reset_v_option_vars();
2705}
2706#endif
2707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708/*
2709 * Set the value of a boolean option, and take care of side effects.
2710 * Returns NULL for success, or an error message for an error.
2711 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002712 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002713set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002714 int opt_idx, // index in options[] table
2715 char_u *varp, // pointer to the option variable
2716 int value, // new value
2717 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
2719 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002720#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002721 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002722#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002723 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 if ((secure
2727#ifdef HAVE_SANDBOX
2728 || sandbox != 0
2729#endif
2730 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002731 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002733#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002734 // Save the global value before changing anything. This is needed as for
2735 // a global-only option setting the "local value" in fact sets the global
2736 // value (since there is only one value).
2737 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2738 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2739 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002740#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002741
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002742 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002744 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002745 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746#endif
2747
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002748#ifdef FEAT_GUI
2749 need_mouse_correct = TRUE;
2750#endif
2751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002752 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2754 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2755
2756 /*
2757 * Handle side effects of changing a bool option.
2758 */
2759
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002760 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
Bram Moolenaar920694c2016-08-21 17:45:02 +02002764#ifdef FEAT_LANGMAP
2765 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002766 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002767 p_lnr = !p_lrm;
2768 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002770 p_lrm = !p_lnr;
2771#endif
2772
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002773#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002775 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2776 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002777 // Only take action when the option was set. When reset we do not
2778 // delete the undo file, the option may be set again without making
2779 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002780 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002781 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002782 char_u hash[UNDO_HASH_SIZE];
2783 buf_T *save_curbuf = curbuf;
2784
Bram Moolenaar29323592016-07-24 22:04:11 +02002785 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002786 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002787 // When 'undofile' is set globally: for every buffer, otherwise
2788 // only for the current buffer: Try to read in the undofile,
2789 // if one exists, the buffer wasn't changed and the buffer was
2790 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002791 if ((curbuf == save_curbuf
2792 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2793 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2794 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002795#ifdef FEAT_CRYPT
2796 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2797 continue;
2798#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002799 u_compute_hash(hash);
2800 u_read_undo(NULL, hash, curbuf->b_fname);
2801 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002802 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002803 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002804 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002805 }
2806#endif
2807
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 else if ((int *)varp == &curbuf->b_p_ro)
2809 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002810 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2812 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002814 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002815 if (curbuf->b_p_ro)
2816 curbuf->b_did_warn = FALSE;
2817
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002818 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
2820
Bram Moolenaar9c449722010-07-20 18:44:27 +02002821#ifdef FEAT_GUI
2822 else if ((int *)varp == &p_mh)
2823 {
2824 if (!p_mh)
2825 gui_mch_mousehide(FALSE);
2826 }
2827#endif
2828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002829 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002831 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002832# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002833 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002834 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2835 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002836 {
2837 curbuf->b_p_ma = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002838 return e_cannot_make_terminal_with_running_job_modifiable;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002839 }
2840# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002841 redraw_titles();
2842 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002843 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002845 {
2846 redraw_titles();
2847 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002848 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002849 else if ((int *)varp == &curbuf->b_p_fixeol)
2850 {
2851 redraw_titles();
2852 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002854 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002855 {
2856 redraw_titles();
2857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 else if ((int *)varp == &curbuf->b_p_bin)
2861 {
2862 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002863 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
2865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002866 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2868 {
2869 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2870 NULL, NULL, TRUE, curbuf);
2871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002873 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 else if ((int *)varp == &curbuf->b_p_swf)
2875 {
2876 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002879 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2880 // buf->b_p_swf
2881 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
2883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002884 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 else if ((int *)varp == &p_terse)
2886 {
2887 char_u *p;
2888
2889 p = vim_strchr(p_shm, SHM_SEARCH);
2890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002891 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 if (p_terse && p == NULL)
2893 {
2894 STRCPY(IObuff, p_shm);
2895 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002896 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002898 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002900 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
2902
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002903 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 else if ((int *)varp == &p_paste)
2905 {
2906 paste_option_changed();
2907 }
2908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 else if ((int *)varp == &p_im)
2911 {
2912 if (p_im)
2913 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002914 if ((State & MODE_INSERT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 need_start_insertmode = TRUE;
2916 stop_insert_mode = FALSE;
2917 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002918 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002919 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 {
2921 need_start_insertmode = FALSE;
2922 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002923 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002924 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 restart_edit = 0;
2926 }
2927 }
2928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002929 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 else if ((int *)varp == &p_ic && p_hls)
2931 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002932 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
2934
2935#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002936 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 else if ((int *)varp == &p_hls)
2938 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002939 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 }
2941#endif
2942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002943 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2944 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 else if ((int *)varp == &curwin->w_p_scb)
2946 {
2947 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002950 curwin->w_scbind_pos = curwin->w_topline;
2951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
Bram Moolenaar4033c552017-09-16 20:54:51 +02002954#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 else if ((int *)varp == &curwin->w_p_pvw)
2957 {
2958 if (curwin->w_p_pvw)
2959 {
2960 win_T *win;
2961
Bram Moolenaar29323592016-07-24 22:04:11 +02002962 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 if (win->w_p_pvw && win != curwin)
2964 {
2965 curwin->w_p_pvw = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002966 return e_preview_window_already_exists;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 }
2968 }
2969 }
2970#endif
2971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 else if ((int *)varp == &curbuf->b_p_tx)
2974 {
2975 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2976 }
2977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002978 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 set_string_option_direct((char_u *)"ffs", -1,
2982 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002983 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986 /*
2987 * When 'lisp' option changes include/exclude '-' in
2988 * keyword characters.
2989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2991 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002992 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002995 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002996 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002998 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 else if ((int *)varp == &curbuf->b_changed)
3002 {
3003 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003004 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003005 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 }
3008
3009#ifdef BACKSLASH_IN_FILENAME
3010 else if ((int *)varp == &p_ssl)
3011 {
3012 if (p_ssl)
3013 {
3014 psepc = '/';
3015 psepcN = '\\';
3016 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 }
3018 else
3019 {
3020 psepc = '\\';
3021 psepcN = '/';
3022 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 }
3024
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003025 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 buflist_slash_adjust();
3027 alist_slash_adjust();
3028# ifdef FEAT_EVAL
3029 scriptnames_slash_adjust();
3030# endif
3031 }
3032#endif
3033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003034 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 else if ((int *)varp == &curwin->w_p_wrap)
3036 {
3037 if (curwin->w_p_wrap)
3038 curwin->w_leftcol = 0;
3039 }
3040
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 else if ((int *)varp == &p_ea)
3042 {
3043 if (p_ea && !old_value)
3044 win_equal(curwin, FALSE, 0);
3045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046
3047 else if ((int *)varp == &p_wiv)
3048 {
3049 /*
3050 * When 'weirdinvert' changed, set/reset 't_xs'.
3051 * Then set 'weirdinvert' according to value of 't_xs'.
3052 */
3053 if (p_wiv && !old_value)
3054 T_XS = (char_u *)"y";
3055 else if (!p_wiv && old_value)
3056 T_XS = empty_option;
3057 p_wiv = (*T_XS != NUL);
3058 }
3059
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003060#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 else if ((int *)varp == &p_beval)
3062 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003063 if (!balloonEvalForTerm)
3064 {
3065 if (p_beval && !old_value)
3066 gui_mch_enable_beval_area(balloonEval);
3067 else if (!p_beval && old_value)
3068 gui_mch_disable_beval_area(balloonEval);
3069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003071#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003072#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003073 else if ((int *)varp == &p_bevalterm)
3074 {
3075 mch_bevalterm_changed();
3076 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003079#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 else if ((int *)varp == &p_acd)
3081 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003082 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003083 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 }
3085#endif
3086
3087#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003088 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 else if ((int *)varp == &curwin->w_p_diff)
3090 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003091 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003092 diff_buf_adjust(curwin);
3093# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (foldmethodIsDiff(curwin))
3095 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 }
3098#endif
3099
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003100#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003101 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 else if ((int *)varp == &p_imdisable)
3103 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003104 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 if (p_imdisable)
3106 im_set_active(FALSE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003107 else if (State & MODE_INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003108 // When the option is set from an autocommand, it may need to take
3109 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003110 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 }
3112#endif
3113
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003114#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003115 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003116 else if ((int *)varp == &curwin->w_p_spell)
3117 {
3118 if (curwin->w_p_spell)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003119 errmsg = did_set_spelllang(curwin);
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003120 }
3121#endif
3122
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#ifdef FEAT_ARABIC
3124 if ((int *)varp == &curwin->w_p_arab)
3125 {
3126 if (curwin->w_p_arab)
3127 {
3128 /*
3129 * 'arabic' is set, handle various sub-settings.
3130 */
3131 if (!p_tbidi)
3132 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003133 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 if (!curwin->w_p_rl)
3135 {
3136 curwin->w_p_rl = TRUE;
3137 changed_window_setting();
3138 }
3139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003140 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (!p_arshape)
3142 {
3143 p_arshape = TRUE;
3144 redraw_later_clear();
3145 }
3146 }
3147
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003148 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003149 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003151 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003152 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3153
Bram Moolenaar8820b482017-03-16 17:23:31 +01003154 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003155 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003156#ifdef FEAT_EVAL
3157 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3158#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003161 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003165 // Force-set the necessary keymap for arabic
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003166 errmsg = set_option_value((char_u *)"keymap",
3167 0L, (char_u *)"arabic", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
3170 else
3171 {
3172 /*
3173 * 'arabic' is reset, handle various sub-settings.
3174 */
3175 if (!p_tbidi)
3176 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003177 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 if (curwin->w_p_rl)
3179 {
3180 curwin->w_p_rl = FALSE;
3181 changed_window_setting();
3182 }
3183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003184 // 'arabicshape' isn't reset, it is a global option and
3185 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003188 // 'delcombine' isn't reset, it is a global option and another
3189 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
3191# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003192 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 curbuf->b_p_iminsert = B_IMODE_NONE;
3194 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3195# endif
3196 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003197 }
3198
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003199#endif
3200
Bram Moolenaar44826212019-08-22 21:23:20 +02003201#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3202 else if (((int *)varp == &curwin->w_p_nu
3203 || (int *)varp == &curwin->w_p_rnu)
3204 && gui.in_use
3205 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3206 && curbuf->b_signlist != NULL)
3207 {
3208 // If the 'number' or 'relativenumber' options are modified and
3209 // 'signcolumn' is set to 'number', then clear the screen for a full
3210 // refresh. Otherwise the sign icons are not displayed properly in the
3211 // number column. If the 'number' option is set and only the
3212 // 'relativenumber' option is toggled, then don't refresh the screen
3213 // (optimization).
3214 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3215 redraw_all_later(CLEAR);
3216 }
3217#endif
3218
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003219#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003220 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003221 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003222 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003223# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003224 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003225 if (
3226# ifdef VIMDLL
3227 !gui.in_use && !gui.starting &&
3228# endif
3229 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003230 {
3231 p_tgc = 0;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003232 return e_24_bit_colors_are_not_supported_on_this_environment;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003233 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003234 if (is_term_win32())
3235 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003236# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003237# ifdef FEAT_GUI
3238 if (!gui.in_use && !gui.starting)
3239# endif
3240 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003241# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003242 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003243 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003244 {
3245 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003246 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003247 init_highlight(TRUE, FALSE);
3248 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003249# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003250# ifdef FEAT_TERMINAL
3251 term_update_colors_all();
LemonBoyb2b3acb2022-05-20 10:10:34 +01003252 term_update_palette_all();
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003253 term_update_wincolor_all();
3254# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003255 }
3256#endif
3257
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 /*
3259 * End of handling side effects for bool options.
3260 */
3261
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003262 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 options[opt_idx].flags |= P_WAS_SET;
3265
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003266#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003267 apply_optionset_autocmd(opt_idx, opt_flags,
3268 (long)(old_value ? TRUE : FALSE),
3269 (long)(old_global_value ? TRUE : FALSE),
3270 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003271#endif
3272
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003273 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003274 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003275 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003276 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003277
3278 if ((opt_flags & OPT_NO_REDRAW) == 0)
3279 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003281 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282}
3283
3284/*
3285 * Set the value of a number option, and take care of side effects.
3286 * Returns NULL for success, or an error message for an error.
3287 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003288 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003289set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003290 int opt_idx, // index in options[] table
3291 char_u *varp, // pointer to the option variable
3292 long value, // new value
3293 char *errbuf, // buffer for error messages
3294 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003295 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3296 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003298 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003300#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003301 long old_global_value = 0; // only used when setting a local and
3302 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003303#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003304 long old_Rows = Rows; // remember old Rows
3305 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 long *pp = (long *)varp;
3307
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003308 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003309 if ((secure
3310#ifdef HAVE_SANDBOX
3311 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003313 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003314 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003316#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003317 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003318 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003319 // value (since there is only one value).
3320 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003321 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3322 OPT_GLOBAL);
3323#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003324
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 *pp = value;
3326#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003327 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003328 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003330#ifdef FEAT_GUI
3331 need_mouse_correct = TRUE;
3332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaar14f24742012-08-08 18:01:05 +02003334 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003336 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003337#ifdef FEAT_VARTABS
3338 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3339 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003340 ? tabstop_first(curbuf->b_p_vts_array)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003341 : curbuf->b_p_ts;
3342#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
3346
3347 /*
3348 * Number options that need some action when changed
3349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 if (pp == &p_wh || pp == &p_hh)
3351 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003352 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 if (p_wh < 1)
3354 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003355 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 p_wh = 1;
3357 }
3358 if (p_wmh > p_wh)
3359 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003360 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 p_wh = p_wmh;
3362 }
3363 if (p_hh < 0)
3364 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003365 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 p_hh = 0;
3367 }
3368
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003369 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003370 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
3372 if (pp == &p_wh && curwin->w_height < p_wh)
3373 win_setheight((int)p_wh);
3374 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3375 win_setheight((int)p_hh);
3376 }
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 else if (pp == &p_wmh)
3379 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003380 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 if (p_wmh < 0)
3382 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003383 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 p_wmh = 0;
3385 }
3386 if (p_wmh > p_wh)
3387 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003388 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 p_wmh = p_wh;
3390 }
3391 win_setminheight();
3392 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003393 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003395 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 if (p_wiw < 1)
3397 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003398 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 p_wiw = 1;
3400 }
3401 if (p_wmw > p_wiw)
3402 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003403 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 p_wiw = p_wmw;
3405 }
3406
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003407 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003408 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 win_setwidth((int)p_wiw);
3410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 else if (pp == &p_wmw)
3412 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003413 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (p_wmw < 0)
3415 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003416 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 p_wmw = 0;
3418 }
3419 if (p_wmw > p_wiw)
3420 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003421 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 p_wmw = p_wiw;
3423 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003424 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003427 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 else if (pp == &p_ls)
3429 {
3430 last_status(FALSE);
3431 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003432
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003433 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003434 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003435 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003436 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
3439#ifdef FEAT_GUI
3440 else if (pp == &p_linespace)
3441 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003442 // Recompute gui.char_height and resize the Vim window to keep the
3443 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003444 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003445 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447#endif
3448
3449#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003450 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 else if (pp == &curwin->w_p_fdl)
3452 {
3453 if (curwin->w_p_fdl < 0)
3454 curwin->w_p_fdl = 0;
3455 newFoldLevel();
3456 }
3457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003458 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 else if (pp == &curwin->w_p_fml)
3460 {
3461 foldUpdateAll(curwin);
3462 }
3463
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003464 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 else if (pp == &curwin->w_p_fdn)
3466 {
3467 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3468 foldUpdateAll(curwin);
3469 }
3470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003471 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 else if (pp == &curwin->w_p_fdc)
3473 {
3474 if (curwin->w_p_fdc < 0)
3475 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003476 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 curwin->w_p_fdc = 0;
3478 }
3479 else if (curwin->w_p_fdc > 12)
3480 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003481 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 curwin->w_p_fdc = 12;
3483 }
3484 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003485#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003487 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3489 {
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003490#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (foldmethodIsIndent(curwin))
3492 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003493#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003494 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3495 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003496 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3497 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003500 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003501 else if (pp == &p_mco)
3502 {
3503 if (p_mco > MAX_MCO)
3504 p_mco = MAX_MCO;
3505 else if (p_mco < 0)
3506 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003507 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003508 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003509
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 else if (pp == &curbuf->b_p_iminsert)
3511 {
3512 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3513 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003514 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 curbuf->b_p_iminsert = B_IMODE_NONE;
3516 }
3517 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003518 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003520#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003521 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 status_redraw_curbuf();
3523#endif
3524 }
3525
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003526#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003527 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003528 else if (pp == &p_imst)
3529 {
3530 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003531 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003532 }
3533#endif
3534
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003535 else if (pp == &p_window)
3536 {
3537 if (p_window < 1)
3538 p_window = 1;
3539 else if (p_window >= Rows)
3540 p_window = Rows - 1;
3541 }
3542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 else if (pp == &curbuf->b_p_imsearch)
3544 {
3545 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3546 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003547 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 curbuf->b_p_imsearch = B_IMODE_NONE;
3549 }
3550 p_imsearch = curbuf->b_p_imsearch;
3551 }
3552
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003553 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 else if (pp == &p_titlelen)
3555 {
3556 if (p_titlelen < 0)
3557 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003558 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 p_titlelen = 85;
3560 }
3561 if (starting != NO_SCREEN && old_value != p_titlelen)
3562 need_maketitle = TRUE;
3563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003565 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 else if (pp == &p_ch)
3567 {
3568 if (p_ch < 1)
3569 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003570 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 p_ch = 1;
3572 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003573 if (p_ch > Rows - min_rows() + 1)
3574 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003576 // Only compute the new window layout when startup has been
3577 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (p_ch != old_value && full_screen
3579#ifdef FEAT_GUI
3580 && !gui.starting
3581#endif
3582 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003583 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
3585
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003586 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 else if (pp == &p_uc)
3588 {
3589 if (p_uc < 0)
3590 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003591 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 p_uc = 100;
3593 }
3594 if (p_uc && !old_value)
3595 ml_open_files();
3596 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003597#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003598 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003599 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003600 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003601 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003602 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003603 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003604 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003605 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003606 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003607 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003608 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003609 }
3610 }
3611#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003612#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003613 else if (pp == &p_mzq)
3614 mzvim_reset_timer();
3615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003617#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003618 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003619 else if (pp == &p_pyx)
3620 {
3621 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003622 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003623 }
3624#endif
3625
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003626 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 else if (pp == &p_ul)
3628 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003629 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003631 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 p_ul = value;
3633 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003634 else if (pp == &curbuf->b_p_ul)
3635 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003636 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003637 curbuf->b_p_ul = old_value;
3638 u_sync(TRUE);
3639 curbuf->b_p_ul = value;
3640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003642#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003643 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003644 else if (pp == &curwin->w_p_nuw)
3645 {
3646 if (curwin->w_p_nuw < 1)
3647 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003648 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003649 curwin->w_p_nuw = 1;
3650 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003651 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003652 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003653 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003654 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003655 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003656 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003657 }
3658#endif
3659
Bram Moolenaar1a384422010-07-14 19:53:30 +02003660 else if (pp == &curbuf->b_p_tw)
3661 {
3662 if (curbuf->b_p_tw < 0)
3663 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003664 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003665 curbuf->b_p_tw = 0;
3666 }
3667#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003668 {
3669 win_T *wp;
3670 tabpage_T *tp;
3671
3672 FOR_ALL_TAB_WINDOWS(tp, wp)
3673 check_colorcolumn(wp);
3674 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003675#endif
3676 }
3677
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 /*
3679 * Check the bounds for numeric options here
3680 */
3681 if (Rows < min_rows() && full_screen)
3682 {
3683 if (errbuf != NULL)
3684 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003685 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003686 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 errmsg = errbuf;
3688 }
3689 Rows = min_rows();
3690 }
3691 if (Columns < MIN_COLUMNS && 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_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 errmsg = errbuf;
3698 }
3699 Columns = MIN_COLUMNS;
3700 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003701 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 /*
3704 * If the screen (shell) height has been changed, assume it is the
3705 * physical screenheight.
3706 */
3707 if (old_Rows != Rows || old_Columns != Columns)
3708 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003709 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 if (updating_screen)
3711 *pp = old_value;
3712 else if (full_screen
3713#ifdef FEAT_GUI
3714 && !gui.starting
3715#endif
3716 )
3717 set_shellsize((int)Columns, (int)Rows, TRUE);
3718 else
3719 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003720 // Postpone the resizing; check the size and cmdline position for
3721 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 check_shellsize();
3723 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3724 cmdline_row = Rows - p_ch;
3725 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003726 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003727 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 if (curbuf->b_p_ts <= 0)
3731 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003732 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 curbuf->b_p_ts = 8;
3734 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003735 else if (curbuf->b_p_ts > TABSTOP_MAX)
3736 {
3737 errmsg = e_invalid_argument;
3738 curbuf->b_p_ts = 8;
3739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (p_tm < 0)
3741 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003742 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 p_tm = 0;
3744 }
3745 if ((curwin->w_p_scr <= 0
3746 || (curwin->w_p_scr > curwin->w_height
3747 && curwin->w_height > 0))
3748 && full_screen)
3749 {
3750 if (pp == &(curwin->w_p_scr))
3751 {
3752 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003753 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 win_comp_scroll(curwin);
3755 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003756 // If 'scroll' became invalid because of a side effect silently adjust
3757 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 else if (curwin->w_p_scr <= 0)
3759 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003760 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 curwin->w_p_scr = curwin->w_height;
3762 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003763 if (p_hi < 0)
3764 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003765 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003766 p_hi = 0;
3767 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003768 else if (p_hi > 10000)
3769 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003770 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003771 p_hi = 10000;
3772 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003773 if (p_re < 0 || p_re > 2)
3774 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003775 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003776 p_re = 0;
3777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 if (p_report < 0)
3779 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003780 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 p_report = 1;
3782 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003783 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003785 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 p_sj = Rows / 2;
3787 else
3788 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003789 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 p_sj = 1;
3791 }
3792 }
3793 if (p_so < 0 && full_screen)
3794 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003795 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 p_so = 0;
3797 }
3798 if (p_siso < 0 && full_screen)
3799 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003800 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 p_siso = 0;
3802 }
3803#ifdef FEAT_CMDWIN
3804 if (p_cwh < 1)
3805 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003806 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 p_cwh = 1;
3808 }
3809#endif
3810 if (p_ut < 0)
3811 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003812 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 p_ut = 2000;
3814 }
3815 if (p_ss < 0)
3816 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003817 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 p_ss = 0;
3819 }
3820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003821 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3823 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3824
3825 options[opt_idx].flags |= P_WAS_SET;
3826
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003827#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003828 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3829 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003830#endif
3831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003832 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003833 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003834 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003835 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003836 if ((opt_flags & OPT_NO_REDRAW) == 0)
3837 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
3839 return errmsg;
3840}
3841
3842/*
3843 * Called after an option changed: check if something needs to be redrawn.
3844 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003845 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003846check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003848 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003849 int doclear = (flags & P_RCLR) == P_RCLR;
3850 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003852 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
3855 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3856 changed_window_setting();
3857 if (flags & P_RBUF)
3858 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003859 if (flags & P_RWINONLY)
3860 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003861 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 redraw_all_later(CLEAR);
3863 else if (all)
3864 redraw_all_later(NOT_VALID);
3865}
3866
3867/*
3868 * Find index for option 'arg'.
3869 * Return -1 if not found.
3870 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003871 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003872findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873{
3874 int opt_idx;
3875 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003876 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 int is_term_opt;
3878
3879 /*
3880 * For first call: Initialize the quick-access table.
3881 * It contains the index for the first option that starts with a certain
3882 * letter. There are 26 letters, plus the first "t_" option.
3883 */
3884 if (quick_tab[1] == 0)
3885 {
3886 p = options[0].fullname;
3887 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3888 {
3889 if (s[0] != p[0])
3890 {
3891 if (s[0] == 't' && s[1] == '_')
3892 quick_tab[26] = opt_idx;
3893 else
3894 quick_tab[CharOrdLow(s[0])] = opt_idx;
3895 }
3896 p = s;
3897 }
3898 }
3899
3900 /*
3901 * Check for name starting with an illegal character.
3902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 return -1;
3905
3906 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3907 if (is_term_opt)
3908 opt_idx = quick_tab[26];
3909 else
3910 opt_idx = quick_tab[CharOrdLow(arg[0])];
3911 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3912 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003913 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 break;
3915 }
3916 if (s == NULL && !is_term_opt)
3917 {
3918 opt_idx = quick_tab[CharOrdLow(arg[0])];
3919 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3920 {
3921 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003922 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 break;
3924 s = NULL;
3925 }
3926 }
3927 if (s == NULL)
3928 opt_idx = -1;
3929 return opt_idx;
3930}
3931
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003932#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933/*
3934 * Get the value for an option.
3935 *
3936 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003937 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003938 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003939 * String option: gov_string, *stringval gets allocated string.
3940 * Hidden Number option: gov_hidden_number.
3941 * Hidden Toggle option: gov_hidden_bool.
3942 * Hidden String option: gov_hidden_string.
3943 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003944 *
3945 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003947 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003948get_option_value(
3949 char_u *name,
3950 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003951 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003952 int *flagsp,
3953 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
3955 int opt_idx;
3956 char_u *varp;
3957
3958 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003959 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003960 {
3961 int key;
3962
3963 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003964 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003965 {
3966 char_u key_name[2];
3967 char_u *p;
3968
Bram Moolenaar10c75c42021-12-28 20:53:30 +00003969 if (flagsp != NULL)
3970 *flagsp = 0; // terminal option has no flags
3971
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003972 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003973 if (key < 0)
3974 {
3975 key_name[0] = KEY2TERMCAP0(key);
3976 key_name[1] = KEY2TERMCAP1(key);
3977 }
3978 else
3979 {
3980 key_name[0] = KS_KEY;
3981 key_name[1] = (key & 0xff);
3982 }
3983 p = find_termcode(key_name);
3984 if (p != NULL)
3985 {
3986 if (stringval != NULL)
3987 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003988 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003989 }
3990 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003991 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003994 varp = get_varp_scope(&(options[opt_idx]), scope);
3995
3996 if (flagsp != NULL)
3997 // Return the P_xxxx option flags.
3998 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
4000 if (options[opt_idx].flags & P_STRING)
4001 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004002 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004003 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (stringval != NULL)
4005 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004006 if ((char_u **)varp == &p_pt) // 'pastetoggle'
4007 *stringval = str2special_save(*(char_u **)(varp), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004009 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004010 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004011 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004014 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 *stringval = vim_strsave(*(char_u **)(varp));
4016 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004017 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004020 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004021 return (options[opt_idx].flags & P_NUM)
4022 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 if (options[opt_idx].flags & P_NUM)
4024 *numval = *(long *)varp;
4025 else
4026 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004027 // Special case: 'modified' is b_changed, but we also want to consider
4028 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 if ((int *)varp == &curbuf->b_changed)
4030 *numval = curbufIsChanged();
4031 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004032 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004034 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035}
4036#endif
4037
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004038#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004039/*
4040 * Returns the option attributes and its value. Unlike the above function it
4041 * will return either global value or local value of the option depending on
4042 * what was requested, but it will never return global value if it was
4043 * requested to return local one and vice versa. Neither it will return
4044 * buffer-local value if it was requested to return window-local one.
4045 *
4046 * Pretends that option is absent if it is not present in the requested scope
4047 * (i.e. has no global, window-local or buffer-local value depending on
4048 * opt_type). Uses
4049 *
4050 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004051 * 0 hidden or unknown option, also option that does not have requested
4052 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004053 * see SOPT_* in vim.h for other flags
4054 *
4055 * Possible opt_type values: see SREQ_* in vim.h
4056 */
4057 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004058get_option_value_strict(
4059 char_u *name,
4060 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004061 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004062 int opt_type,
4063 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004064{
4065 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004066 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004067 struct vimoption *p;
4068 int r = 0;
4069
4070 opt_idx = findoption(name);
4071 if (opt_idx < 0)
4072 return 0;
4073
4074 p = &(options[opt_idx]);
4075
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004076 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004077 if (p->var == NULL)
4078 return 0;
4079
4080 if (p->flags & P_BOOL)
4081 r |= SOPT_BOOL;
4082 else if (p->flags & P_NUM)
4083 r |= SOPT_NUM;
4084 else if (p->flags & P_STRING)
4085 r |= SOPT_STRING;
4086
4087 if (p->indir == PV_NONE)
4088 {
4089 if (opt_type == SREQ_GLOBAL)
4090 r |= SOPT_GLOBAL;
4091 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004092 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004093 }
4094 else
4095 {
4096 if (p->indir & PV_BOTH)
4097 r |= SOPT_GLOBAL;
4098 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004099 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004100
4101 if (p->indir & PV_WIN)
4102 {
4103 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004104 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004105 else
4106 r |= SOPT_WIN;
4107 }
4108 else if (p->indir & PV_BUF)
4109 {
4110 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004111 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004112 else
4113 r |= SOPT_BUF;
4114 }
4115 }
4116
4117 if (stringval == NULL)
4118 return r;
4119
4120 if (opt_type == SREQ_GLOBAL)
4121 varp = p->var;
4122 else
4123 {
4124 if (opt_type == SREQ_BUF)
4125 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004126 // Special case: 'modified' is b_changed, but we also want to
4127 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004128 if (p->indir == PV_MOD)
4129 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004130 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004131 varp = NULL;
4132 }
4133#ifdef FEAT_CRYPT
4134 else if (p->indir == PV_KEY)
4135 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004136 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004137 *stringval = NULL;
4138 varp = NULL;
4139 }
4140#endif
4141 else
4142 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004143 buf_T *save_curbuf = curbuf;
4144
4145 // only getting a pointer, no need to use aucmd_prepbuf()
4146 curbuf = (buf_T *)from;
4147 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004148 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004149 curbuf = save_curbuf;
4150 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004151 }
4152 }
4153 else if (opt_type == SREQ_WIN)
4154 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004155 win_T *save_curwin = curwin;
4156
4157 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004158 curbuf = curwin->w_buffer;
4159 varp = get_varp(p);
4160 curwin = save_curwin;
4161 curbuf = curwin->w_buffer;
4162 }
4163 if (varp == p->var)
4164 return (r | SOPT_UNSET);
4165 }
4166
4167 if (varp != NULL)
4168 {
4169 if (p->flags & P_STRING)
4170 *stringval = vim_strsave(*(char_u **)(varp));
4171 else if (p->flags & P_NUM)
4172 *numval = *(long *) varp;
4173 else
4174 *numval = *(int *)varp;
4175 }
4176
4177 return r;
4178}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004179
4180/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004181 * Iterate over options. First argument is a pointer to a pointer to a
4182 * structure inside options[] array, second is option type like in the above
4183 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004184 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004185 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004186 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004187 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004188 * first argument to NULL.
4189 *
4190 * Returns full option name for current option on each call.
4191 */
4192 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004193option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004194{
4195 struct vimoption *ret = NULL;
4196 do
4197 {
4198 if (*option == NULL)
4199 *option = (void *) options;
4200 else if (((struct vimoption *) (*option))->fullname == NULL)
4201 {
4202 *option = NULL;
4203 return NULL;
4204 }
4205 else
4206 *option = (void *) (((struct vimoption *) (*option)) + 1);
4207
4208 ret = ((struct vimoption *) (*option));
4209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004210 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004211 if (ret->var == NULL)
4212 {
4213 ret = NULL;
4214 continue;
4215 }
4216
4217 switch (opt_type)
4218 {
4219 case SREQ_GLOBAL:
4220 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4221 ret = NULL;
4222 break;
4223 case SREQ_BUF:
4224 if (!(ret->indir & PV_BUF))
4225 ret = NULL;
4226 break;
4227 case SREQ_WIN:
4228 if (!(ret->indir & PV_WIN))
4229 ret = NULL;
4230 break;
4231 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004232 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004233 return NULL;
4234 }
4235 }
4236 while (ret == NULL);
4237
4238 return (char_u *)ret->fullname;
4239}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004240#endif
4241
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004243 * Return the flags for the option at 'opt_idx'.
4244 */
4245 long_u
4246get_option_flags(int opt_idx)
4247{
4248 return options[opt_idx].flags;
4249}
4250
4251/*
4252 * Set a flag for the option at 'opt_idx'.
4253 */
4254 void
4255set_option_flag(int opt_idx, long_u flag)
4256{
4257 options[opt_idx].flags |= flag;
4258}
4259
4260/*
4261 * Clear a flag for the option at 'opt_idx'.
4262 */
4263 void
4264clear_option_flag(int opt_idx, long_u flag)
4265{
4266 options[opt_idx].flags &= ~flag;
4267}
4268
4269/*
4270 * Returns TRUE if the option at 'opt_idx' is a global option
4271 */
4272 int
4273is_global_option(int opt_idx)
4274{
4275 return options[opt_idx].indir == PV_NONE;
4276}
4277
4278/*
4279 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4280 * local value.
4281 */
4282 int
4283is_global_local_option(int opt_idx)
4284{
4285 return options[opt_idx].indir & PV_BOTH;
4286}
4287
4288/*
4289 * Returns TRUE if the option at 'opt_idx' is a window-local option
4290 */
4291 int
4292is_window_local_option(int opt_idx)
4293{
4294 return options[opt_idx].var == VAR_WIN;
4295}
4296
4297/*
4298 * Returns TRUE if the option at 'opt_idx' is a hidden option
4299 */
4300 int
4301is_hidden_option(int opt_idx)
4302{
4303 return options[opt_idx].var == NULL;
4304}
4305
4306#if defined(FEAT_CRYPT) || defined(PROTO)
4307/*
4308 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4309 */
4310 int
4311is_crypt_key_option(int opt_idx)
4312{
4313 return options[opt_idx].indir == PV_KEY;
4314}
4315#endif
4316
4317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 * Set the value of option "name".
4319 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004320 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004321 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004323 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004324set_option_value(
4325 char_u *name,
4326 long number,
4327 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004328 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329{
4330 int opt_idx;
4331 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004332 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
4334 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004335 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004336 {
4337 int key;
4338
4339 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004340 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004341 {
4342 char_u key_name[2];
4343
4344 if (key < 0)
4345 {
4346 key_name[0] = KEY2TERMCAP0(key);
4347 key_name[1] = KEY2TERMCAP1(key);
4348 }
4349 else
4350 {
4351 key_name[0] = KS_KEY;
4352 key_name[1] = (key & 0xff);
4353 }
4354 add_termcode(key_name, string, FALSE);
4355 if (full_screen)
4356 ttest(FALSE);
4357 redraw_all_later(CLEAR);
4358 return NULL;
4359 }
4360
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004361 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 else
4364 {
4365 flags = options[opt_idx].flags;
4366#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004367 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004369 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004370 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004371 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004374 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004375 return set_string_option(opt_idx, string, opt_flags);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004376
4377 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
4378 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004380 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004382 int idx;
4383
4384 // Either we are given a string or we are setting option
4385 // to zero.
4386 for (idx = 0; string[idx] == '0'; ++idx)
4387 ;
4388 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004389 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004390 // There's another character after zeros or the string
4391 // is empty. In both cases, we are trying to set a
4392 // num option using a string.
4393 semsg(_(e_number_required_after_str_equal_str),
4394 name, string);
4395 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004396
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004399 if (flags & P_NUM)
4400 return set_num_option(opt_idx, varp, number,
4401 NULL, 0, opt_flags);
4402 else
4403 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004405
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004407 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408}
4409
4410/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004411 * Call set_option_value() and when an error is returned report it.
4412 */
4413 void
4414set_option_value_give_err(
4415 char_u *name,
4416 long number,
4417 char_u *string,
4418 int opt_flags) // OPT_LOCAL or 0 (both)
4419{
4420 char *errmsg = set_option_value(name, number, string, opt_flags);
4421
4422 if (errmsg != NULL)
4423 emsg(_(errmsg));
4424}
4425
4426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 * Get the terminal code for a terminal option.
4428 * Returns NULL when not found.
4429 */
4430 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004431get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432{
4433 int opt_idx;
4434 char_u *varp;
4435
4436 if (tname[0] != 't' || tname[1] != '_' ||
4437 tname[2] == NUL || tname[3] == NUL)
4438 return NULL;
4439 if ((opt_idx = findoption(tname)) >= 0)
4440 {
4441 varp = get_varp(&(options[opt_idx]));
4442 if (varp != NULL)
4443 varp = *(char_u **)(varp);
4444 return varp;
4445 }
4446 return find_termcode(tname + 2);
4447}
4448
4449 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004450get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452 int i;
4453
4454 i = findoption((char_u *)"hl");
4455 if (i >= 0)
4456 return options[i].def_val[VI_DEFAULT];
4457 return (char_u *)NULL;
4458}
4459
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004460 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004461get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004462{
4463 int i;
4464
4465 i = findoption((char_u *)"enc");
4466 if (i >= 0)
4467 return options[i].def_val[VI_DEFAULT];
4468 return (char_u *)NULL;
4469}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004470
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004471#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004472 int
4473is_option_allocated(char *name)
4474{
4475 int idx = findoption((char_u *)name);
4476
4477 return idx >= 0 && (options[idx].flags & P_ALLOCED);
4478}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004479#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004480
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481/*
4482 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004483 * When "has_lt" is true there is a '<' before "*arg_arg".
4484 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 */
4486 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004487find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004489 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004491 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
4493 /*
4494 * Don't use get_special_key_code() for t_xx, we don't want it to call
4495 * add_termcap_entry().
4496 */
4497 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4498 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004499 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004501 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004503 key = find_special_key(&arg, &modifiers,
4504 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004505 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 key = 0;
4507 }
4508 return key;
4509}
4510
4511/*
4512 * if 'all' == 0: show changed options
4513 * if 'all' == 1: show all normal options
4514 * if 'all' == 2: show all terminal options
4515 */
4516 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004517showoptions(
4518 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004519 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520{
4521 struct vimoption *p;
4522 int col;
4523 int isterm;
4524 char_u *varp;
4525 struct vimoption **items;
4526 int item_count;
4527 int run;
4528 int row, rows;
4529 int cols;
4530 int i;
4531 int len;
4532
4533#define INC 20
4534#define GAP 3
4535
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004536 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 if (items == NULL)
4538 return;
4539
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004540 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004542 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004544 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004546 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004548 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
4550 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004551 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 * 1. display the short items
4553 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004554 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 */
4556 for (run = 1; run <= 2 && !got_int; ++run)
4557 {
4558 /*
4559 * collect the items in items[]
4560 */
4561 item_count = 0;
4562 for (p = &options[0]; p->fullname != NULL; p++)
4563 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004564 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004565 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004566 continue;
4567
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 varp = NULL;
4569 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004570 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
4572 if (p->indir != PV_NONE && !isterm)
4573 varp = get_varp_scope(p, opt_flags);
4574 }
4575 else
4576 varp = get_varp(p);
4577 if (varp != NULL
4578 && ((all == 2 && isterm)
4579 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004580 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004582 if (opt_flags & OPT_ONECOLUMN)
4583 len = Columns;
4584 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004585 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 else
4587 {
4588 option_value2string(p, opt_flags);
4589 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4590 }
4591 if ((len <= INC - GAP && run == 1) ||
4592 (len > INC - GAP && run == 2))
4593 items[item_count++] = p;
4594 }
4595 }
4596
4597 /*
4598 * display the items
4599 */
4600 if (run == 1)
4601 {
4602 cols = (Columns + GAP - 3) / INC;
4603 if (cols == 0)
4604 cols = 1;
4605 rows = (item_count + cols - 1) / cols;
4606 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004607 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 rows = item_count;
4609 for (row = 0; row < rows && !got_int; ++row)
4610 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004611 msg_putchar('\n'); // go to next line
4612 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 break;
4614 col = 0;
4615 for (i = row; i < item_count; i += rows)
4616 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004617 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 showoneopt(items[i], opt_flags);
4619 col += INC;
4620 }
4621 out_flush();
4622 ui_breakcheck();
4623 }
4624 }
4625 vim_free(items);
4626}
4627
4628/*
4629 * Return TRUE if option "p" has its default value.
4630 */
4631 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004632optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633{
4634 int dvi;
4635
4636 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004637 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004638 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004640 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004642 // the cast to long is required for Manx C, long_i is
4643 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004644 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004645 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4647}
4648
4649/*
4650 * showoneopt: show the value of one option
4651 * must not be called with a hidden option!
4652 */
4653 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004654showoneopt(
4655 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004656 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004658 char_u *varp;
4659 int save_silent = silent_mode;
4660
4661 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004662 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
4664 varp = get_varp_scope(p, opt_flags);
4665
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004666 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4668 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004669 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004671 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004673 msg_puts(" ");
4674 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (!(p->flags & P_BOOL))
4676 {
4677 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004678 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 option_value2string(p, opt_flags);
4680 msg_outtrans(NameBuff);
4681 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004682
4683 silent_mode = save_silent;
4684 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685}
4686
4687/*
4688 * Write modified options as ":set" commands to a file.
4689 *
4690 * There are three values for "opt_flags":
4691 * OPT_GLOBAL: Write global option values and fresh values of
4692 * buffer-local options (used for start of a session
4693 * file).
4694 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4695 * curwin (used for a vimrc file).
4696 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4697 * and local values for window-local options of
4698 * curwin. Local values are also written when at the
4699 * default value, because a modeline or autocommand
4700 * may have set them when doing ":edit file" and the
4701 * user has set them back at the default or fresh
4702 * value.
4703 * When "local_only" is TRUE, don't write fresh
4704 * values, only local values (for ":mkview").
4705 * (fresh value = value used for a new buffer or window for a local option).
4706 *
4707 * Return FAIL on error, OK otherwise.
4708 */
4709 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004710makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
4712 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004713 char_u *varp; // currently used value
4714 char_u *varp_fresh; // local value
4715 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 char *cmd;
4717 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004718 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719
4720 /*
4721 * The options that don't have a default (terminal name, columns, lines)
4722 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004723 * Do the loop over "options[]" twice: once for options with the
4724 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004726 for (pri = 1; pri >= 0; --pri)
4727 {
4728 for (p = &options[0]; !istermoption(p); p++)
4729 if (!(p->flags & P_NO_MKRC)
4730 && !istermoption(p)
4731 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004733 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4735 continue;
4736
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004737 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4738 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4740 continue;
4741
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004742 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004744 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 continue;
4746
Bram Moolenaard23b7142021-04-17 21:04:34 +02004747 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4748 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004749 continue;
4750
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 round = 2;
4752 if (p->indir != PV_NONE)
4753 {
4754 if (p->var == VAR_WIN)
4755 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004756 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 if (!(opt_flags & OPT_LOCAL))
4758 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004759 // When fresh value of window-local option is not at the
4760 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4762 {
4763 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004764 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
4766 round = 1;
4767 varp_local = varp;
4768 varp = varp_fresh;
4769 }
4770 }
4771 }
4772 }
4773
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004774 // Round 1: fresh value for window-local options.
4775 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 for ( ; round <= 2; varp = varp_local, ++round)
4777 {
4778 if (round == 1 || (opt_flags & OPT_GLOBAL))
4779 cmd = "set";
4780 else
4781 cmd = "setlocal";
4782
4783 if (p->flags & P_BOOL)
4784 {
4785 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4786 return FAIL;
4787 }
4788 else if (p->flags & P_NUM)
4789 {
4790 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4791 return FAIL;
4792 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004793 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004795 int do_endif = FALSE;
4796
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004797 // Don't set 'syntax' and 'filetype' again if the value is
4798 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004799 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004800#if defined(FEAT_SYN_HL)
4801 p->indir == PV_SYN ||
4802#endif
4803 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
4805 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4806 *(char_u **)(varp)) < 0
4807 || put_eol(fd) < 0)
4808 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004809 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 }
4811 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004812 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004814 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
4816 if (put_line(fd, "endif") == FAIL)
4817 return FAIL;
4818 }
4819 }
4820 }
4821 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 return OK;
4824}
4825
4826#if defined(FEAT_FOLDING) || defined(PROTO)
4827/*
4828 * Generate set commands for the local fold options only. Used when
4829 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4830 */
4831 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004832makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004834 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004836 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 == FAIL
4838# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004839 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004841 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 == FAIL
4843 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4844 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4845 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4846 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4847 )
4848 return FAIL;
4849
4850 return OK;
4851}
4852#endif
4853
4854 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004855put_setstring(
4856 FILE *fd,
4857 char *cmd,
4858 char *name,
4859 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004860 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
4862 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004863 char_u *buf = NULL;
4864 char_u *part = NULL;
4865 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
4867 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4868 return FAIL;
4869 if (*valuep != NULL)
4870 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004871 // Output 'pastetoggle' as key names. For other
4872 // options some characters have to be escaped with
4873 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 if (valuep == &p_pt)
4875 {
4876 s = *valuep;
4877 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004878 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 return FAIL;
4880 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004881 // expand the option value, replace $HOME by ~
4882 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004884 int size = (int)STRLEN(*valuep) + 1;
4885
4886 // replace home directory in the whole option value into "buf"
4887 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004888 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004889 goto fail;
4890 home_replace(NULL, *valuep, buf, size, FALSE);
4891
4892 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004893 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004894 // can be expanded when read back.
4895 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4896 && vim_strchr(*valuep, ',') != NULL)
4897 {
4898 part = alloc(size);
4899 if (part == NULL)
4900 goto fail;
4901
4902 // write line break to clear the option, e.g. ':set rtp='
4903 if (put_eol(fd) == FAIL)
4904 goto fail;
4905
4906 p = buf;
4907 while (*p != NUL)
4908 {
4909 // for each comma separated option part, append value to
4910 // the option, :set rtp+=value
4911 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4912 goto fail;
4913 (void)copy_option_part(&p, part, size, ",");
4914 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4915 goto fail;
4916 }
4917 vim_free(buf);
4918 vim_free(part);
4919 return OK;
4920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004922 {
4923 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004925 }
4926 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else if (put_escstr(fd, *valuep, 2) == FAIL)
4929 return FAIL;
4930 }
4931 if (put_eol(fd) < 0)
4932 return FAIL;
4933 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004934fail:
4935 vim_free(buf);
4936 vim_free(part);
4937 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938}
4939
4940 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004941put_setnum(
4942 FILE *fd,
4943 char *cmd,
4944 char *name,
4945 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947 long wc;
4948
4949 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4950 return FAIL;
4951 if (wc_use_keyname((char_u *)valuep, &wc))
4952 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004953 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4955 return FAIL;
4956 }
4957 else if (fprintf(fd, "%ld", *valuep) < 0)
4958 return FAIL;
4959 if (put_eol(fd) < 0)
4960 return FAIL;
4961 return OK;
4962}
4963
4964 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004965put_setbool(
4966 FILE *fd,
4967 char *cmd,
4968 char *name,
4969 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004971 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004972 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4974 || put_eol(fd) < 0)
4975 return FAIL;
4976 return OK;
4977}
4978
4979/*
4980 * Clear all the terminal options.
4981 * If the option has been allocated, free the memory.
4982 * Terminal options are never hidden or indirect.
4983 */
4984 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004985clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 /*
4988 * Reset a few things before clearing the old options. This may cause
4989 * outputting a few things that the terminal doesn't understand, but the
4990 * screen will be cleared later, so this is OK.
4991 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004992 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004993 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004995 // When starting the GUI close the display opened for the clipboard.
4996 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 if (gui.starting)
4998 clear_xterm_clip();
4999#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005000 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005002 free_termoptions();
5003}
5004
5005 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005006free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005007{
5008 struct vimoption *p;
5009
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005010 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 if (istermoption(p))
5012 {
5013 if (p->flags & P_ALLOCED)
5014 free_string_option(*(char_u **)(p->var));
5015 if (p->flags & P_DEF_ALLOCED)
5016 free_string_option(p->def_val[VI_DEFAULT]);
5017 *(char_u **)(p->var) = empty_option;
5018 p->def_val[VI_DEFAULT] = empty_option;
5019 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005020#ifdef FEAT_EVAL
5021 // remember where the option was cleared
5022 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
5025 clear_termcodes();
5026}
5027
5028/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005029 * Free the string for one term option, if it was allocated.
5030 * Set the string to empty_option and clear allocated flag.
5031 * "var" points to the option value.
5032 */
5033 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005034free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005035{
5036 struct vimoption *p;
5037
5038 for (p = &options[0]; p->fullname != NULL; p++)
5039 if (p->var == var)
5040 {
5041 if (p->flags & P_ALLOCED)
5042 free_string_option(*(char_u **)(p->var));
5043 *(char_u **)(p->var) = empty_option;
5044 p->flags &= ~P_ALLOCED;
5045 break;
5046 }
5047}
5048
5049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 * Set the terminal option defaults to the current value.
5051 * Used after setting the terminal name.
5052 */
5053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005054set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055{
5056 struct vimoption *p;
5057
5058 for (p = &options[0]; p->fullname != NULL; p++)
5059 {
5060 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5061 {
5062 if (p->flags & P_DEF_ALLOCED)
5063 {
5064 free_string_option(p->def_val[VI_DEFAULT]);
5065 p->flags &= ~P_DEF_ALLOCED;
5066 }
5067 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5068 if (p->flags & P_ALLOCED)
5069 {
5070 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005071 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073 }
5074 }
5075}
5076
5077/*
5078 * return TRUE if 'p' starts with 't_'
5079 */
5080 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005081istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
5083 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5084}
5085
Bram Moolenaardac13472019-09-16 21:06:21 +02005086/*
5087 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5088 */
5089 int
5090istermoption_idx(int opt_idx)
5091{
5092 return istermoption(&options[opt_idx]);
5093}
5094
Bram Moolenaar113e1072019-01-20 15:30:40 +01005095#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005097 * Unset local option value, similar to ":set opt<".
5098 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005099 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005100unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005101{
5102 struct vimoption *p;
5103 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005104 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005105
5106 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005107 if (opt_idx < 0)
5108 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005109 p = &(options[opt_idx]);
5110
5111 switch ((int)p->indir)
5112 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005113 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005114 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005115 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005116 break;
5117 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005118 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005119 break;
5120 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005121 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005122 break;
5123 case PV_AR:
5124 buf->b_p_ar = -1;
5125 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005126 case PV_BKC:
5127 clear_string_option(&buf->b_p_bkc);
5128 buf->b_bkc_flags = 0;
5129 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005130 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005131 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005132 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005133 case PV_TC:
5134 clear_string_option(&buf->b_p_tc);
5135 buf->b_tc_flags = 0;
5136 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005137 case PV_SISO:
5138 curwin->w_p_siso = -1;
5139 break;
5140 case PV_SO:
5141 curwin->w_p_so = -1;
5142 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005143#ifdef FEAT_FIND_ID
5144 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005145 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005146 break;
5147 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005148 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005149 break;
5150#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005151 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005152 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005153 break;
5154 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005155 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005156 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005157#ifdef FEAT_COMPL_FUNC
5158 case PV_TSRFU:
5159 clear_string_option(&buf->b_p_tsrfu);
5160 break;
5161#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005162 case PV_FP:
5163 clear_string_option(&buf->b_p_fp);
5164 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005165#ifdef FEAT_QUICKFIX
5166 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005167 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005168 break;
5169 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005170 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005171 break;
5172 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005173 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005174 break;
5175#endif
5176#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5177 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005178 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005179 break;
5180#endif
5181#if defined(FEAT_CRYPT)
5182 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005183 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005184 break;
5185#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005186#ifdef FEAT_LINEBREAK
5187 case PV_SBR:
5188 clear_string_option(&((win_T *)from)->w_p_sbr);
5189 break;
5190#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005191#ifdef FEAT_STL_OPT
5192 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005193 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005194 break;
5195#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005196 case PV_UL:
5197 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5198 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005199 case PV_LW:
5200 clear_string_option(&buf->b_p_lw);
5201 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005202 case PV_MENC:
5203 clear_string_option(&buf->b_p_menc);
5204 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005205 case PV_LCS:
5206 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005207 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005208 redraw_later(NOT_VALID);
5209 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005210 case PV_FCS:
5211 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005212 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005213 redraw_later(NOT_VALID);
5214 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005215 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005216 clear_string_option(&((win_T *)from)->w_p_ve);
5217 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005218 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005219 }
5220}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005221#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005222
5223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005225 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 */
5227 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005228get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005230 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
5232 if (p->var == VAR_WIN)
5233 return (char_u *)GLOBAL_WO(get_varp(p));
5234 return p->var;
5235 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005236 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
5238 switch ((int)p->indir)
5239 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005240 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005242 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5243 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5244 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005246 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5247 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5248 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005249 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005250 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005251 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005252 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5253 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005255 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5256 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005258 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5259 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005260#ifdef FEAT_COMPL_FUNC
5261 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5262#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005263#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5264 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5265#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005266#if defined(FEAT_CRYPT)
5267 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5268#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005269#ifdef FEAT_LINEBREAK
5270 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5271#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005272#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005273 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005274#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005275 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005276 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005277 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005278 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005279 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005280 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005281 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005282
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005284 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286 return get_varp(p);
5287}
5288
5289/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005290 * Get pointer to option variable at 'opt_idx', depending on local or global
5291 * scope.
5292 */
5293 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005294get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005295{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005296 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005297}
5298
5299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 * Get pointer to option variable.
5301 */
5302 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005303get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005305 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 if (p->var == NULL)
5307 return NULL;
5308
5309 switch ((int)p->indir)
5310 {
5311 case PV_NONE: return p->var;
5312
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005313 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005314 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005316 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005318 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005320 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005322 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005324 case PV_TC: return *curbuf->b_p_tc != NUL
5325 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005326 case PV_BKC: return *curbuf->b_p_bkc != NUL
5327 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005328 case PV_SISO: return curwin->w_p_siso >= 0
5329 ? (char_u *)&(curwin->w_p_siso) : p->var;
5330 case PV_SO: return curwin->w_p_so >= 0
5331 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005333 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005335 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5337#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005338 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005340 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005342#ifdef FEAT_COMPL_FUNC
5343 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5344 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5345#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005346 case PV_FP: return *curbuf->b_p_fp != NUL
5347 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005349 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005351 case PV_GP: return *curbuf->b_p_gp != NUL
5352 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5353 case PV_MP: return *curbuf->b_p_mp != NUL
5354 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005356#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5357 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5358 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5359#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005360#if defined(FEAT_CRYPT)
5361 case PV_CM: return *curbuf->b_p_cm != NUL
5362 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5363#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005364#ifdef FEAT_LINEBREAK
5365 case PV_SBR: return *curwin->w_p_sbr != NUL
5366 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5367#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005368#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005369 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005370 ? (char_u *)&(curwin->w_p_stl) : p->var;
5371#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005372 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5373 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005374 case PV_LW: return *curbuf->b_p_lw != NUL
5375 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005376 case PV_MENC: return *curbuf->b_p_menc != NUL
5377 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378#ifdef FEAT_ARABIC
5379 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5380#endif
5381 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005382 case PV_LCS: return *curwin->w_p_lcs != NUL
5383 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005384 case PV_FCS: return *curwin->w_p_fcs != NUL
5385 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005386 case PV_VE: return *curwin->w_p_ve != NUL
5387 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005388#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005389 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5390#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005391#ifdef FEAT_SYN_HL
5392 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5393 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005394 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005395 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397#ifdef FEAT_DIFF
5398 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5399#endif
5400#ifdef FEAT_FOLDING
5401 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5402 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5403 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5404 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5405 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5406 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5407 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5408# ifdef FEAT_EVAL
5409 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5410 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5411# endif
5412 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5413#endif
5414 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005415 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005416#ifdef FEAT_LINEBREAK
5417 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005420 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005421#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5423#endif
5424#ifdef FEAT_RIGHTLEFT
5425 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5426 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5427#endif
5428 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5429 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5430#ifdef FEAT_LINEBREAK
5431 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005432 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5433 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005435 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005437 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005438#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005439 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5440 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5441#endif
5442#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005443 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5444 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5445 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447
5448 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5449 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5452 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5454 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5456 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5457 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01005458 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461#ifdef FEAT_FOLDING
5462 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005465#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005466 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005468#ifdef FEAT_COMPL_FUNC
5469 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005470 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005471#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005472#ifdef FEAT_EVAL
5473 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005476 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005482 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5484 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5485 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5486 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5487#ifdef FEAT_FIND_ID
5488# ifdef FEAT_EVAL
5489 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5490# endif
5491#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01005492#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5494 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5495#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005496#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005497 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499#ifdef FEAT_CRYPT
5500 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5504 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5505 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5506 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5507 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005509#ifdef FEAT_TEXTOBJ
5510 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5516#ifdef FEAT_SEARCHPATH
5517 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5518#endif
5519 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5520#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005521 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005522 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5523#endif
5524#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005525 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5526 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5527 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005528 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529#endif
5530 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5531 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5532 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5533 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005534#ifdef FEAT_PERSISTENT_UNDO
5535 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5538#ifdef FEAT_KEYMAP
5539 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5540#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005541#ifdef FEAT_SIGNS
5542 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5543#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005544#ifdef FEAT_VARTABS
5545 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5546 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5547#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005548 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005550 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 return (char_u *)&(curbuf->b_p_wm);
5552}
5553
5554/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005555 * Return a pointer to the variable for option at 'opt_idx'
5556 */
5557 char_u *
5558get_option_var(int opt_idx)
5559{
5560 return options[opt_idx].var;
5561}
5562
Dominique Pelle748b3082022-01-08 12:41:16 +00005563#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005564/*
5565 * Return the full name of the option at 'opt_idx'
5566 */
5567 char_u *
5568get_option_fullname(int opt_idx)
5569{
5570 return (char_u *)options[opt_idx].fullname;
5571}
Dominique Pelle748b3082022-01-08 12:41:16 +00005572#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005573
5574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 * Get the value of 'equalprg', either the buffer-local one or the global one.
5576 */
5577 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005578get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 if (*curbuf->b_p_ep == NUL)
5581 return p_ep;
5582 return curbuf->b_p_ep;
5583}
5584
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585/*
5586 * Copy options from one window to another.
5587 * Used when splitting a window.
5588 */
5589 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005590win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5593 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005594 after_copy_winopt(wp_to);
5595}
5596
5597/*
5598 * After copying window options: update variables depending on options.
5599 */
5600 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00005601after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005602{
5603#ifdef FEAT_LINEBREAK
5604 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005605#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005606#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005607 fill_culopt_flags(NULL, wp);
5608 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005609#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005610 set_chars_option(wp, &wp->w_p_lcs, TRUE);
5611 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005612}
5613
5614 static char_u *
5615copy_option_val(char_u *val)
5616{
5617 if (val == empty_option)
5618 return empty_option; // no need to allocate memory
5619 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
5622/*
5623 * Copy the options from one winopt_T to another.
5624 * Doesn't free the old option values in "to", use clear_winopt() for that.
5625 * The 'scroll' option is not copied, because it depends on the window height.
5626 * The 'previewwindow' option is reset, there can be only one preview window.
5627 */
5628 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005629copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
5631#ifdef FEAT_ARABIC
5632 to->wo_arab = from->wo_arab;
5633#endif
5634 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005635 to->wo_lcs = copy_option_val(from->wo_lcs);
5636 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005638 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005639 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02005640 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005641#ifdef FEAT_LINEBREAK
5642 to->wo_nuw = from->wo_nuw;
5643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644#ifdef FEAT_RIGHTLEFT
5645 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005646 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005648#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005649 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01005650#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005651#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005652 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005655#ifdef FEAT_DIFF
5656 to->wo_wrap_save = from->wo_wrap_save;
5657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658#ifdef FEAT_LINEBREAK
5659 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005660 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005661 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005663 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005665 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005666 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005667 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005668#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005669 to->wo_spell = from->wo_spell;
5670#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005671#ifdef FEAT_SYN_HL
5672 to->wo_cuc = from->wo_cuc;
5673 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005674 to->wo_culopt = copy_option_val(from->wo_culopt);
5675 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677#ifdef FEAT_DIFF
5678 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005679 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005681#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005682 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005683 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005684#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005685#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005686 to->wo_twk = copy_option_val(from->wo_twk);
5687 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689#ifdef FEAT_FOLDING
5690 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005691 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005693 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005694 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 to->wo_fml = from->wo_fml;
5696 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005697 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005698 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005699 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005700 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 to->wo_fdn = from->wo_fdn;
5702# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005703 to->wo_fde = copy_option_val(from->wo_fde);
5704 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005706 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005708#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005709 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005710#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005711
5712#ifdef FEAT_EVAL
5713 // Copy the script context so that we know where the value was last set.
5714 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5715 sizeof(to->wo_script_ctx));
5716#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005717 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718}
5719
5720/*
5721 * Check string options in a window for a NULL value.
5722 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005723 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005724check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725{
5726 check_winopt(&win->w_onebuf_opt);
5727 check_winopt(&win->w_allbuf_opt);
5728}
5729
5730/*
5731 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5732 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005733 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005734check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
5736#ifdef FEAT_FOLDING
5737 check_string_option(&wop->wo_fdi);
5738 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005739 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740# ifdef FEAT_EVAL
5741 check_string_option(&wop->wo_fde);
5742 check_string_option(&wop->wo_fdt);
5743# endif
5744 check_string_option(&wop->wo_fmr);
5745#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005746#ifdef FEAT_SIGNS
5747 check_string_option(&wop->wo_scl);
5748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749#ifdef FEAT_RIGHTLEFT
5750 check_string_option(&wop->wo_rlc);
5751#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005752#ifdef FEAT_LINEBREAK
5753 check_string_option(&wop->wo_sbr);
5754#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005755#ifdef FEAT_STL_OPT
5756 check_string_option(&wop->wo_stl);
5757#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005758#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005759 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005760 check_string_option(&wop->wo_cc);
5761#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005762#ifdef FEAT_CONCEAL
5763 check_string_option(&wop->wo_cocu);
5764#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005765#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005766 check_string_option(&wop->wo_twk);
5767 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005768#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005769#ifdef FEAT_LINEBREAK
5770 check_string_option(&wop->wo_briopt);
5771#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005772 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005773 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005774 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005775 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776}
5777
5778/*
5779 * Free the allocated memory inside a winopt_T.
5780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005782clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783{
5784#ifdef FEAT_FOLDING
5785 clear_string_option(&wop->wo_fdi);
5786 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005787 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788# ifdef FEAT_EVAL
5789 clear_string_option(&wop->wo_fde);
5790 clear_string_option(&wop->wo_fdt);
5791# endif
5792 clear_string_option(&wop->wo_fmr);
5793#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005794#ifdef FEAT_SIGNS
5795 clear_string_option(&wop->wo_scl);
5796#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005797#ifdef FEAT_LINEBREAK
5798 clear_string_option(&wop->wo_briopt);
5799#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005800 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801#ifdef FEAT_RIGHTLEFT
5802 clear_string_option(&wop->wo_rlc);
5803#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005804#ifdef FEAT_LINEBREAK
5805 clear_string_option(&wop->wo_sbr);
5806#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005807#ifdef FEAT_STL_OPT
5808 clear_string_option(&wop->wo_stl);
5809#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005810#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005811 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005812 clear_string_option(&wop->wo_cc);
5813#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005814#ifdef FEAT_CONCEAL
5815 clear_string_option(&wop->wo_cocu);
5816#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005817#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005818 clear_string_option(&wop->wo_twk);
5819 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005820#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005821 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005822 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005823 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824}
5825
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005826#ifdef FEAT_EVAL
5827// Index into the options table for a buffer-local option enum.
5828static int buf_opt_idx[BV_COUNT];
5829# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5830
5831/*
5832 * Initialize buf_opt_idx[] if not done already.
5833 */
5834 static void
5835init_buf_opt_idx(void)
5836{
5837 static int did_init_buf_opt_idx = FALSE;
5838 int i;
5839
5840 if (did_init_buf_opt_idx)
5841 return;
5842 did_init_buf_opt_idx = TRUE;
5843 for (i = 0; !istermoption_idx(i); i++)
5844 if (options[i].indir & PV_BUF)
5845 buf_opt_idx[options[i].indir & PV_MASK] = i;
5846}
5847#else
5848# define COPY_OPT_SCTX(buf, bv)
5849#endif
5850
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851/*
5852 * Copy global option values to local options for one buffer.
5853 * Used when creating a new buffer and sometimes when entering a buffer.
5854 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005855 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5857 * appropriate.
5858 * BCO_NOHELP Don't copy the values to a help buffer.
5859 */
5860 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005861buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
5863 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005864 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 int dont_do_help;
5866 int did_isk = FALSE;
5867
5868 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 * Skip this when the option defaults have not been set yet. Happens when
5870 * main() allocates the first buffer.
5871 */
5872 if (p_cpo != NULL)
5873 {
5874 /*
5875 * Always copy when entering and 'cpo' contains 'S'.
5876 * Don't copy when already initialized.
5877 * Don't copy when 'cpo' contains 's' and not entering.
5878 * 'S' BCO_ENTER initialized 's' should_copy
5879 * yes yes X X TRUE
5880 * yes no yes X FALSE
5881 * no X yes X FALSE
5882 * X no no yes FALSE
5883 * X no no no TRUE
5884 * no yes no X TRUE
5885 */
5886 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5887 && (buf->b_p_initialized
5888 || (!(flags & BCO_ENTER)
5889 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5890 should_copy = FALSE;
5891
5892 if (should_copy || (flags & BCO_ALWAYS))
5893 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005894#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005895 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005896 init_buf_opt_idx();
5897#endif
5898 // Don't copy the options specific to a help buffer when
5899 // BCO_NOHELP is given or the options were initialized already
5900 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5902 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005903 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 {
5905 save_p_isk = buf->b_p_isk;
5906 buf->b_p_isk = NULL;
5907 }
5908 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005909 * Always free the allocated strings. If not already initialized,
5910 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 */
5912 if (!buf->b_p_initialized)
5913 {
5914 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005915 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005918 switch (*p_ffs)
5919 {
5920 case 'm':
5921 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5922 case 'd':
5923 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5924 case 'u':
5925 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5926 default:
5927 buf->b_p_ff = vim_strsave(p_ff);
5928 }
5929 if (buf->b_p_ff != NULL)
5930 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 buf->b_p_bh = empty_option;
5932 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 }
5934 else
5935 free_buf_options(buf, FALSE);
5936
5937 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005938 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 buf->b_p_ai_nopaste = p_ai_nopaste;
5940 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005941 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005943 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 buf->b_p_tw_nopaste = p_tw_nopaste;
5945 buf->b_p_tw_nobin = p_tw_nobin;
5946 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005947 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 buf->b_p_wm_nopaste = p_wm_nopaste;
5949 buf->b_p_wm_nobin = p_wm_nobin;
5950 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005951 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005952 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005953 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005954 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005955 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005957 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005959 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005961 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 buf->b_p_ml_nobin = p_ml_nobin;
5963 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005964 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005965 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005966 buf->b_p_swf = FALSE;
5967 else
5968 {
5969 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00005970 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005973 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005974#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005975 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005976 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005978#ifdef FEAT_COMPL_FUNC
5979 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005980 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005981 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005982 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005983 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005984 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005985#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005986#ifdef FEAT_EVAL
5987 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005988 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005989 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005992 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005994#ifdef FEAT_VARTABS
5995 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005996 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005997 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02005998 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005999 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006000 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006001 buf->b_p_vsts_nopaste = p_vsts_nopaste
6002 ? vim_strsave(p_vsts_nopaste) : NULL;
6003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006005 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006007 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008#ifdef FEAT_FOLDING
6009 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006010 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011#endif
6012 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006013 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006014 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006015 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006016 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6017 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006019 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006021 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006023 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006025 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006026
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006028 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006030 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006032 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006033 buf->b_p_cinsd = vim_strsave(p_cinsd);
6034 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006035
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006036 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006039 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006041 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006043 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006045 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006047 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006048 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006049 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006050#endif
6051#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006052 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006053 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006054 (void)compile_cap_prog(&buf->b_s);
6055 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006056 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006057 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006058 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006059 buf->b_s.b_p_spo = vim_strsave(p_spo);
6060 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006062#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006064 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006066 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006068 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006069#if defined(FEAT_EVAL)
6070 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006071 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073#ifdef FEAT_CRYPT
6074 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006075 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076#endif
6077#ifdef FEAT_SEARCHPATH
6078 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006079 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080#endif
6081#ifdef FEAT_KEYMAP
6082 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006083 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 buf->b_kmap_state |= KEYMAP_INIT;
6085#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006086#ifdef FEAT_TERMINAL
6087 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006088 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006089#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006090 // This isn't really an option, but copying the langmap and IME
6091 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006093 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006095 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006097 // options that are normally global but also have a local value
6098 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006100 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006101 buf->b_p_bkc = empty_option;
6102 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103#ifdef FEAT_QUICKFIX
6104 buf->b_p_gp = empty_option;
6105 buf->b_p_mp = empty_option;
6106 buf->b_p_efm = empty_option;
6107#endif
6108 buf->b_p_ep = empty_option;
6109 buf->b_p_kp = empty_option;
6110 buf->b_p_path = empty_option;
6111 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006112 buf->b_p_tc = empty_option;
6113 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114#ifdef FEAT_FIND_ID
6115 buf->b_p_def = empty_option;
6116 buf->b_p_inc = empty_option;
6117# ifdef FEAT_EVAL
6118 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006119 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120# endif
6121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 buf->b_p_dict = empty_option;
6123 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006124#ifdef FEAT_COMPL_FUNC
6125 buf->b_p_tsrfu = empty_option;
6126#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006127#ifdef FEAT_TEXTOBJ
6128 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006129 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006130#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006131#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6132 buf->b_p_bexpr = empty_option;
6133#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006134#if defined(FEAT_CRYPT)
6135 buf->b_p_cm = empty_option;
6136#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006137#ifdef FEAT_PERSISTENT_UNDO
6138 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006139 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006140#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006141 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006142 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
6144 /*
6145 * Don't copy the options set by ex_help(), use the saved values,
6146 * when going from a help buffer to a non-help buffer.
6147 * Don't touch these at all when BCO_NOHELP is used and going from
6148 * or to a help buffer.
6149 */
6150 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006153#ifdef FEAT_VARTABS
6154 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006155 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006156 else
6157 buf->b_p_vts_array = NULL;
6158#endif
6159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 else
6161 {
6162 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006163 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 did_isk = TRUE;
6165 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006166 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006167#ifdef FEAT_VARTABS
6168 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006169 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006170 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006171 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006172 else
6173 buf->b_p_vts_array = NULL;
6174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 if (buf->b_p_bt[0] == 'h')
6177 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006179 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 }
6181 }
6182
6183 /*
6184 * When the options should be copied (ignoring BCO_ALWAYS), set the
6185 * flag that indicates that the options have been initialized.
6186 */
6187 if (should_copy)
6188 buf->b_p_initialized = TRUE;
6189 }
6190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006191 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 if (did_isk)
6193 (void)buf_init_chartab(buf, FALSE);
6194}
6195
6196/*
6197 * Reset the 'modifiable' option and its default value.
6198 */
6199 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006200reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201{
6202 int opt_idx;
6203
6204 curbuf->b_p_ma = FALSE;
6205 p_ma = FALSE;
6206 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006207 if (opt_idx >= 0)
6208 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209}
6210
6211/*
6212 * Set the global value for 'iminsert' to the local value.
6213 */
6214 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006215set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
6217 p_iminsert = curbuf->b_p_iminsert;
6218}
6219
6220/*
6221 * Set the global value for 'imsearch' to the local value.
6222 */
6223 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006224set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225{
6226 p_imsearch = curbuf->b_p_imsearch;
6227}
6228
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229static int expand_option_idx = -1;
6230static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6231static int expand_option_flags = 0;
6232
6233 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006234set_context_in_set_cmd(
6235 expand_T *xp,
6236 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006237 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
6239 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006240 long_u flags = 0; // init for GCC
6241 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 char_u *p;
6243 char_u *s;
6244 int is_term_option = FALSE;
6245 int key;
6246
6247 expand_option_flags = opt_flags;
6248
6249 xp->xp_context = EXPAND_SETTINGS;
6250 if (*arg == NUL)
6251 {
6252 xp->xp_pattern = arg;
6253 return;
6254 }
6255 p = arg + STRLEN(arg) - 1;
6256 if (*p == ' ' && *(p - 1) != '\\')
6257 {
6258 xp->xp_pattern = p + 1;
6259 return;
6260 }
6261 while (p > arg)
6262 {
6263 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006264 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (*p == ' ' || *p == ',')
6266 {
6267 while (s > arg && *(s - 1) == '\\')
6268 --s;
6269 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006270 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 if (*p == ' ' && ((p - s) & 1) == 0)
6272 {
6273 ++p;
6274 break;
6275 }
6276 --p;
6277 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006278 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 {
6280 xp->xp_context = EXPAND_BOOL_SETTINGS;
6281 p += 2;
6282 }
6283 if (STRNCMP(p, "inv", 3) == 0)
6284 {
6285 xp->xp_context = EXPAND_BOOL_SETTINGS;
6286 p += 3;
6287 }
6288 xp->xp_pattern = arg = p;
6289 if (*arg == '<')
6290 {
6291 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006292 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 return;
6294 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006295 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 {
6297 xp->xp_context = EXPAND_NOTHING;
6298 return;
6299 }
6300 nextchar = *++p;
6301 is_term_option = TRUE;
6302 expand_option_name[2] = KEY2TERMCAP0(key);
6303 expand_option_name[3] = KEY2TERMCAP1(key);
6304 }
6305 else
6306 {
6307 if (p[0] == 't' && p[1] == '_')
6308 {
6309 p += 2;
6310 if (*p != NUL)
6311 ++p;
6312 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006313 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 nextchar = *++p;
6315 is_term_option = TRUE;
6316 expand_option_name[2] = p[-2];
6317 expand_option_name[3] = p[-1];
6318 }
6319 else
6320 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006321 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6323 p++;
6324 if (*p == NUL)
6325 return;
6326 nextchar = *p;
6327 *p = NUL;
6328 opt_idx = findoption(arg);
6329 *p = nextchar;
6330 if (opt_idx == -1 || options[opt_idx].var == NULL)
6331 {
6332 xp->xp_context = EXPAND_NOTHING;
6333 return;
6334 }
6335 flags = options[opt_idx].flags;
6336 if (flags & P_BOOL)
6337 {
6338 xp->xp_context = EXPAND_NOTHING;
6339 return;
6340 }
6341 }
6342 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006343 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6345 {
6346 ++p;
6347 nextchar = '=';
6348 }
6349 if ((nextchar != '=' && nextchar != ':')
6350 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6351 {
6352 xp->xp_context = EXPAND_UNSUCCESSFUL;
6353 return;
6354 }
6355 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6356 {
6357 xp->xp_context = EXPAND_OLD_SETTING;
6358 if (is_term_option)
6359 expand_option_idx = -1;
6360 else
6361 expand_option_idx = opt_idx;
6362 xp->xp_pattern = p + 1;
6363 return;
6364 }
6365 xp->xp_context = EXPAND_NOTHING;
6366 if (is_term_option || (flags & P_NUM))
6367 return;
6368
6369 xp->xp_pattern = p + 1;
6370
6371 if (flags & P_EXPAND)
6372 {
6373 p = options[opt_idx].var;
6374 if (p == (char_u *)&p_bdir
6375 || p == (char_u *)&p_dir
6376 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006377 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 || p == (char_u *)&p_rtp
6379#ifdef FEAT_SEARCHPATH
6380 || p == (char_u *)&p_cdpath
6381#endif
6382#ifdef FEAT_SESSION
6383 || p == (char_u *)&p_vdir
6384#endif
6385 )
6386 {
6387 xp->xp_context = EXPAND_DIRECTORIES;
6388 if (p == (char_u *)&p_path
6389#ifdef FEAT_SEARCHPATH
6390 || p == (char_u *)&p_cdpath
6391#endif
6392 )
6393 xp->xp_backslash = XP_BS_THREE;
6394 else
6395 xp->xp_backslash = XP_BS_ONE;
6396 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006397 else if (p == (char_u *)&p_ft)
6398 {
6399 xp->xp_context = EXPAND_FILETYPE;
6400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 else
6402 {
6403 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006404 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 if (p == (char_u *)&p_tags)
6406 xp->xp_backslash = XP_BS_THREE;
6407 else
6408 xp->xp_backslash = XP_BS_ONE;
6409 }
6410 }
6411
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006412 // For an option that is a list of file names, find the start of the
6413 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6415 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006416 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 if (*p == ' ' || *p == ',')
6418 {
6419 s = p;
6420 while (s > xp->xp_pattern && *(s - 1) == '\\')
6421 --s;
6422 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6423 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6424 {
6425 xp->xp_pattern = p + 1;
6426 break;
6427 }
6428 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006429
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006430#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006431 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006432 if (options[opt_idx].var == (char_u *)&p_sps
6433 && STRNCMP(p, "file:", 5) == 0)
6434 {
6435 xp->xp_pattern = p + 5;
6436 break;
6437 }
6438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440}
6441
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006442/*
6443 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
6444 *
6445 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
6446 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
6447 *
6448 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
6449 * regular expression 'regmatch', then stores the match in matches[idx] and
6450 * returns TRUE.
6451 *
6452 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6453 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
6454 *
6455 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6456 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
6457 */
6458 static int
6459match_str(
6460 char_u *str,
6461 regmatch_T *regmatch,
6462 char_u **matches,
6463 int idx,
6464 int test_only,
6465 int fuzzy,
6466 char_u *fuzzystr,
6467 fuzmatch_str_T *fuzmatch)
6468{
6469 if (!fuzzy)
6470 {
6471 if (vim_regexec(regmatch, str, (colnr_T)0))
6472 {
6473 if (!test_only)
6474 matches[idx] = vim_strsave(str);
6475 return TRUE;
6476 }
6477 }
6478 else
6479 {
6480 int score;
6481
6482 score = fuzzy_match_str(str, fuzzystr);
6483 if (score != 0)
6484 {
6485 if (!test_only)
6486 {
6487 fuzmatch[idx].idx = idx;
6488 fuzmatch[idx].str = vim_strsave(str);
6489 fuzmatch[idx].score = score;
6490 }
6491
6492 return TRUE;
6493 }
6494 }
6495
6496 return FALSE;
6497}
6498
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006500ExpandSettings(
6501 expand_T *xp,
6502 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006503 char_u *fuzzystr,
6504 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01006505 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006506 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006508 int num_normal = 0; // Nr of matching non-term-code settings
6509 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 int opt_idx;
6511 int match;
6512 int count = 0;
6513 char_u *str;
6514 int loop;
6515 int is_term_opt;
6516 char_u name_buf[MAX_KEY_NAME_LEN];
6517 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006518 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006519 int fuzzy;
6520 fuzmatch_str_T *fuzmatch = NULL;
6521
Christian Brabandtcb747892022-05-08 21:10:56 +01006522 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006524 // do this loop twice:
6525 // loop == 0: count the number of matching options
6526 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 for (loop = 0; loop <= 1; ++loop)
6528 {
6529 regmatch->rm_ic = ic;
6530 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6531 {
K.Takataeeec2542021-06-02 13:28:16 +02006532 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006533 {
6534 if (match_str((char_u *)names[match], regmatch, *matches,
6535 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
6537 if (loop == 0)
6538 num_normal++;
6539 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006540 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 }
6544 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6545 opt_idx++)
6546 {
6547 if (options[opt_idx].var == NULL)
6548 continue;
6549 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6550 && !(options[opt_idx].flags & P_BOOL))
6551 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006552 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 if (is_term_opt && num_normal > 0)
6554 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006555
6556 if (match_str(str, regmatch, *matches, count, (loop == 0),
6557 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 {
6559 if (loop == 0)
6560 {
6561 if (is_term_opt)
6562 num_term++;
6563 else
6564 num_normal++;
6565 }
6566 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006567 count++;
6568 }
6569 else if (!fuzzy && options[opt_idx].shortname != NULL
6570 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006571 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006572 {
6573 // Compare against the abbreviated option name (for regular
6574 // expression match). Fuzzy matching (previous if) already
6575 // matches against both the expanded and abbreviated names.
6576 if (loop == 0)
6577 {
6578 if (is_term_opt)
6579 num_term++;
6580 else
6581 num_normal++;
6582 }
6583 else
6584 (*matches)[count++] = vim_strsave(str);
6585 }
6586 else if (is_term_opt)
6587 {
6588 name_buf[0] = '<';
6589 name_buf[1] = 't';
6590 name_buf[2] = '_';
6591 name_buf[3] = str[2];
6592 name_buf[4] = str[3];
6593 name_buf[5] = '>';
6594 name_buf[6] = NUL;
6595
6596 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6597 fuzzy, fuzzystr, fuzmatch))
6598 {
6599 if (loop == 0)
6600 num_term++;
6601 else
6602 count++;
6603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 }
6605 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006606
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 /*
6608 * Check terminal key codes, these are not in the option table
6609 */
6610 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6611 {
6612 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6613 {
6614 if (!isprint(str[0]) || !isprint(str[1]))
6615 continue;
6616
6617 name_buf[0] = 't';
6618 name_buf[1] = '_';
6619 name_buf[2] = str[0];
6620 name_buf[3] = str[1];
6621 name_buf[4] = NUL;
6622
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006623 if (match_str(name_buf, regmatch, *matches, count,
6624 (loop == 0), fuzzy, fuzzystr, fuzmatch))
6625 {
6626 if (loop == 0)
6627 num_term++;
6628 else
6629 count++;
6630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 else
6632 {
6633 name_buf[0] = '<';
6634 name_buf[1] = 't';
6635 name_buf[2] = '_';
6636 name_buf[3] = str[0];
6637 name_buf[4] = str[1];
6638 name_buf[5] = '>';
6639 name_buf[6] = NUL;
6640
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006641 if (match_str(name_buf, regmatch, *matches, count,
6642 (loop == 0), fuzzy, fuzzystr,
6643 fuzmatch))
6644 {
6645 if (loop == 0)
6646 num_term++;
6647 else
6648 count++;
6649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 }
6651 }
6652
6653 /*
6654 * Check special key names.
6655 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006656 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6658 {
6659 name_buf[0] = '<';
6660 STRCPY(name_buf + 1, str);
6661 STRCAT(name_buf, ">");
6662
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006663 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6664 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
6666 if (loop == 0)
6667 num_term++;
6668 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006669 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 }
6671 }
6672 }
6673 if (loop == 0)
6674 {
6675 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006676 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006678 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 else
6680 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006681 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006683 *matches = ALLOC_MULT(char_u *, *numMatches);
6684 if (*matches == NULL)
6685 {
6686 *matches = (char_u **)"";
6687 return FAIL;
6688 }
6689 }
6690 else
6691 {
6692 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
6693 if (fuzmatch == NULL)
6694 {
6695 *matches = (char_u **)"";
6696 return FAIL;
6697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 }
6699 }
6700 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006701
6702 if (fuzzy &&
6703 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
6704 return FAIL;
6705
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 return OK;
6707}
6708
6709 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006710ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006712 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 char_u *buf;
6714
6715 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006716 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 if (*file == NULL)
6718 return FAIL;
6719
6720 /*
6721 * For a terminal key code expand_option_idx is < 0.
6722 */
6723 if (expand_option_idx < 0)
6724 {
6725 var = find_termcode(expand_option_name + 2);
6726 if (var == NULL)
6727 expand_option_idx = findoption(expand_option_name);
6728 }
6729
6730 if (expand_option_idx >= 0)
6731 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006732 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 option_value2string(&options[expand_option_idx], expand_option_flags);
6734 var = NameBuff;
6735 }
6736 else if (var == NULL)
6737 var = (char_u *)"";
6738
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006739 // A backslash is required before some characters. This is the reverse of
6740 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 buf = vim_strsave_escaped(var, escape_chars);
6742
6743 if (buf == NULL)
6744 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006745 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 return FAIL;
6747 }
6748
6749#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006750 // For MS-Windows et al. we don't double backslashes at the start and
6751 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006752 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 if (var[0] == '\\' && var[1] == '\\'
6754 && expand_option_idx >= 0
6755 && (options[expand_option_idx].flags & P_EXPAND)
6756 && vim_isfilec(var[2])
6757 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006758 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759#endif
6760
6761 *file[0] = buf;
6762 *num_file = 1;
6763 return OK;
6764}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
6766/*
6767 * Get the value for the numeric or string option *opp in a nice format into
6768 * NameBuff[]. Must not be called with a hidden option!
6769 */
6770 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006771option_value2string(
6772 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006773 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774{
6775 char_u *varp;
6776
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006777 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778
6779 if (opp->flags & P_NUM)
6780 {
6781 long wc = 0;
6782
6783 if (wc_use_keyname(varp, &wc))
6784 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6785 else if (wc != 0)
6786 STRCPY(NameBuff, transchar((int)wc));
6787 else
6788 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6789 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006790 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
6792 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006793 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 NameBuff[0] = NUL;
6795#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006796 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006797 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 STRCPY(NameBuff, "*****");
6799#endif
6800 else if (opp->flags & P_EXPAND)
6801 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006802 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 else if ((char_u **)opp->var == &p_pt)
6804 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6805 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006806 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 }
6808}
6809
6810/*
6811 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6812 * printed as a keyname.
6813 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6814 */
6815 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006816wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817{
6818 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6819 {
6820 *wcp = *(long *)varp;
6821 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6822 return TRUE;
6823 }
6824 return FALSE;
6825}
6826
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 * Return TRUE if "x" is present in 'shortmess' option, or
6829 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6830 */
6831 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006832shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006834 return p_shm != NULL &&
6835 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 || (vim_strchr(p_shm, 'a') != NULL
6837 && vim_strchr((char_u *)SHM_A, x) != NULL));
6838}
6839
6840/*
6841 * paste_option_changed() - Called after p_paste was set or reset.
6842 */
6843 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006844paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
6846 static int old_p_paste = FALSE;
6847 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006848 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849#ifdef FEAT_CMDL_INFO
6850 static int save_ru = 0;
6851#endif
6852#ifdef FEAT_RIGHTLEFT
6853 static int save_ri = 0;
6854 static int save_hkmap = 0;
6855#endif
6856 buf_T *buf;
6857
6858 if (p_paste)
6859 {
6860 /*
6861 * Paste switched from off to on.
6862 * Save the current values, so they can be restored later.
6863 */
6864 if (!old_p_paste)
6865 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006866 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006867 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 {
6869 buf->b_p_tw_nopaste = buf->b_p_tw;
6870 buf->b_p_wm_nopaste = buf->b_p_wm;
6871 buf->b_p_sts_nopaste = buf->b_p_sts;
6872 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006873 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006874#ifdef FEAT_VARTABS
6875 if (buf->b_p_vsts_nopaste)
6876 vim_free(buf->b_p_vsts_nopaste);
6877 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6878 ? vim_strsave(buf->b_p_vsts) : NULL;
6879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 }
6881
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006882 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006884 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885#ifdef FEAT_CMDL_INFO
6886 save_ru = p_ru;
6887#endif
6888#ifdef FEAT_RIGHTLEFT
6889 save_ri = p_ri;
6890 save_hkmap = p_hkmap;
6891#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006892 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006893 p_ai_nopaste = p_ai;
6894 p_et_nopaste = p_et;
6895 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 p_tw_nopaste = p_tw;
6897 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006898#ifdef FEAT_VARTABS
6899 if (p_vsts_nopaste)
6900 vim_free(p_vsts_nopaste);
6901 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 }
6904
6905 /*
6906 * Always set the option values, also when 'paste' is set when it is
6907 * already on.
6908 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006909 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006910 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006912 buf->b_p_tw = 0; // textwidth is 0
6913 buf->b_p_wm = 0; // wrapmargin is 0
6914 buf->b_p_sts = 0; // softtabstop is 0
6915 buf->b_p_ai = 0; // no auto-indent
6916 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006917#ifdef FEAT_VARTABS
6918 if (buf->b_p_vsts)
6919 free_string_option(buf->b_p_vsts);
6920 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006921 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 }
6924
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006925 // set global options
6926 p_sm = 0; // no showmatch
6927 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006930 status_redraw_all(); // redraw to remove the ruler
6931 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932#endif
6933#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006934 p_ri = 0; // no reverse insert
6935 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006937 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 p_tw = 0;
6939 p_wm = 0;
6940 p_sts = 0;
6941 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006942#ifdef FEAT_VARTABS
6943 if (p_vsts)
6944 free_string_option(p_vsts);
6945 p_vsts = empty_option;
6946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 }
6948
6949 /*
6950 * Paste switched from on to off: Restore saved values.
6951 */
6952 else if (old_p_paste)
6953 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006954 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006955 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {
6957 buf->b_p_tw = buf->b_p_tw_nopaste;
6958 buf->b_p_wm = buf->b_p_wm_nopaste;
6959 buf->b_p_sts = buf->b_p_sts_nopaste;
6960 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006961 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006962#ifdef FEAT_VARTABS
6963 if (buf->b_p_vsts)
6964 free_string_option(buf->b_p_vsts);
6965 buf->b_p_vsts = buf->b_p_vsts_nopaste
6966 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006967 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006968 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006969 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006970 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006971 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 }
6974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006975 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006977 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006980 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 p_ru = save_ru;
6982#endif
6983#ifdef FEAT_RIGHTLEFT
6984 p_ri = save_ri;
6985 p_hkmap = save_hkmap;
6986#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006987 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006988 p_ai = p_ai_nopaste;
6989 p_et = p_et_nopaste;
6990 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 p_tw = p_tw_nopaste;
6992 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006993#ifdef FEAT_VARTABS
6994 if (p_vsts)
6995 free_string_option(p_vsts);
6996 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 }
6999
7000 old_p_paste = p_paste;
7001}
7002
7003/*
7004 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7005 *
7006 * Reset 'compatible' and set the values for options that didn't get set yet
7007 * to the Vim defaults.
7008 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007009 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 */
7011 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007012vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007014 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007015 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007016 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017
7018 if (!option_was_set((char_u *)"cp"))
7019 {
7020 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007021 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7023 set_option_default(opt_idx, OPT_FREE, FALSE);
7024 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007025 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007027
7028 if (fname != NULL)
7029 {
7030 p = vim_getenv(envname, &dofree);
7031 if (p == NULL)
7032 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007033 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007034 p = FullName_save(fname, FALSE);
7035 if (p != NULL)
7036 {
7037 vim_setenv(envname, p);
7038 vim_free(p);
7039 }
7040 }
7041 else if (dofree)
7042 vim_free(p);
7043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044}
7045
7046/*
7047 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7048 */
7049 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007050change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007052 int opt_idx;
7053
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 if (p_cp != on)
7055 {
7056 p_cp = on;
7057 compatible_set();
7058 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007059 opt_idx = findoption((char_u *)"cp");
7060 if (opt_idx >= 0)
7061 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062}
7063
7064/*
7065 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007066 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 */
7068 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007069option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 int idx;
7072
7073 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007074 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 return FALSE;
7076 if (options[idx].flags & P_WAS_SET)
7077 return TRUE;
7078 return FALSE;
7079}
7080
7081/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007082 * Reset the flag indicating option "name" was set.
7083 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007084 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007085reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007086{
7087 int idx = findoption(name);
7088
7089 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007090 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007091 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007092 return OK;
7093 }
7094 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007095}
7096
7097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 * compatible_set() - Called when 'compatible' has been set or unset.
7099 *
7100 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7101 * flag) to a Vi compatible value.
7102 * When 'compatible' is unset: Set all options that have a different default
7103 * for Vim (without the P_VI_DEF flag) to that default.
7104 */
7105 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007106compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107{
7108 int opt_idx;
7109
Bram Moolenaardac13472019-09-16 21:06:21 +02007110 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7112 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7113 set_option_default(opt_idx, OPT_FREE, p_cp);
7114 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007115 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116}
7117
Bram Moolenaardac13472019-09-16 21:06:21 +02007118#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120/*
7121 * fill_breakat_flags() -- called when 'breakat' changes value.
7122 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007123 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007124fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007126 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 int i;
7128
7129 for (i = 0; i < 256; i++)
7130 breakat_flags[i] = FALSE;
7131
7132 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007133 for (p = p_breakat; *p; p++)
7134 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136#endif
7137
7138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 * Check if backspacing over something is allowed.
7140 */
7141 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007142can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007143 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007145#ifdef FEAT_JOB_CHANNEL
7146 if (what == BS_START && bt_prompt(curbuf))
7147 return FALSE;
7148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 switch (*p_bs)
7150 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007151 case '3': return TRUE;
7152 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 case '1': return (what != BS_START);
7154 case '0': return FALSE;
7155 }
7156 return vim_strchr(p_bs, what) != NULL;
7157}
7158
7159/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007160 * Return the effective 'scrolloff' value for the current window, using the
7161 * global value when appropriate.
7162 */
7163 long
7164get_scrolloff_value(void)
7165{
7166 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7167}
7168
7169/*
7170 * Return the effective 'sidescrolloff' value for the current window, using the
7171 * global value when appropriate.
7172 */
7173 long
7174get_sidescrolloff_value(void)
7175{
7176 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7177}
7178
7179/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007180 * Get the local or global value of 'backupcopy'.
7181 */
7182 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007183get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007184{
7185 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7186}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007187
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007188#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007189/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007190 * Get the local or global value of 'formatlistpat'.
7191 */
7192 char_u *
7193get_flp_value(buf_T *buf)
7194{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007195 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7196 return p_flp;
7197 return buf->b_p_flp;
7198}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007199#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007200
7201/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007202 * Get the local or global value of the 'virtualedit' flags.
7203 */
7204 unsigned int
7205get_ve_flags(void)
7206{
Gary Johnson51ad8502021-08-03 18:33:08 +02007207 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7208 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007209}
7210
Bram Moolenaaree857022019-11-09 23:26:40 +01007211#if defined(FEAT_LINEBREAK) || defined(PROTO)
7212/*
7213 * Get the local or global value of 'showbreak'.
7214 */
7215 char_u *
7216get_showbreak_value(win_T *win)
7217{
7218 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7219 return p_sbr;
7220 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7221 return empty_option;
7222 return win->w_p_sbr;
7223}
7224#endif
7225
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007226#if defined(FEAT_EVAL) || defined(PROTO)
7227/*
7228 * Get window or buffer local options.
7229 */
7230 dict_T *
7231get_winbuf_options(int bufopt)
7232{
7233 dict_T *d;
7234 int opt_idx;
7235
7236 d = dict_alloc();
7237 if (d == NULL)
7238 return NULL;
7239
Bram Moolenaardac13472019-09-16 21:06:21 +02007240 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007241 {
7242 struct vimoption *opt = &options[opt_idx];
7243
7244 if ((bufopt && (opt->indir & PV_BUF))
7245 || (!bufopt && (opt->indir & PV_WIN)))
7246 {
7247 char_u *varp = get_varp(opt);
7248
7249 if (varp != NULL)
7250 {
7251 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007252 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007253 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007254 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007255 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007256 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007257 }
7258 }
7259 }
7260
7261 return d;
7262}
7263#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007264
Bram Moolenaardac13472019-09-16 21:06:21 +02007265#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007266/*
7267 * This is called when 'culopt' is changed
7268 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007269 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007270fill_culopt_flags(char_u *val, win_T *wp)
7271{
7272 char_u *p;
7273 char_u culopt_flags_new = 0;
7274
7275 if (val == NULL)
7276 p = wp->w_p_culopt;
7277 else
7278 p = val;
7279 while (*p != NUL)
7280 {
7281 if (STRNCMP(p, "line", 4) == 0)
7282 {
7283 p += 4;
7284 culopt_flags_new |= CULOPT_LINE;
7285 }
7286 else if (STRNCMP(p, "both", 4) == 0)
7287 {
7288 p += 4;
7289 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7290 }
7291 else if (STRNCMP(p, "number", 6) == 0)
7292 {
7293 p += 6;
7294 culopt_flags_new |= CULOPT_NBR;
7295 }
7296 else if (STRNCMP(p, "screenline", 10) == 0)
7297 {
7298 p += 10;
7299 culopt_flags_new |= CULOPT_SCRLINE;
7300 }
7301
7302 if (*p != ',' && *p != NUL)
7303 return FAIL;
7304 if (*p == ',')
7305 ++p;
7306 }
7307
7308 // Can't have both "line" and "screenline".
7309 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7310 return FAIL;
7311 wp->w_p_culopt_flags = culopt_flags_new;
7312
7313 return OK;
7314}
7315#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007316
7317/*
7318 * Get the value of 'magic' adjusted for Vim9 script.
7319 */
7320 int
7321magic_isset(void)
7322{
7323 switch (magic_overruled)
7324 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007325 case OPTION_MAGIC_ON: return TRUE;
7326 case OPTION_MAGIC_OFF: return FALSE;
7327 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007328 }
7329#ifdef FEAT_EVAL
7330 if (in_vim9script())
7331 return TRUE;
7332#endif
7333 return p_magic;
7334}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007335
7336/*
7337 * Set the callback function value for an option that accepts a function name,
7338 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7339 * Returns OK if the option is successfully set to a function, otherwise
7340 * returns FAIL.
7341 */
7342 int
7343option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7344{
7345#ifdef FEAT_EVAL
7346 typval_T *tv;
7347 callback_T cb;
7348
7349 if (optval == NULL || *optval == NUL)
7350 {
7351 free_callback(optcb);
7352 return OK;
7353 }
7354
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007355 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007356 || (STRNCMP(optval, "function(", 9) == 0)
7357 || (STRNCMP(optval, "funcref(", 8) == 0))
7358 // Lambda expression or a funcref
7359 tv = eval_expr(optval, NULL);
7360 else
7361 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007362 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007363 if (tv == NULL)
7364 return FAIL;
7365
7366 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007367 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007368 {
7369 free_tv(tv);
7370 return FAIL;
7371 }
7372
7373 free_callback(optcb);
7374 set_callback(optcb, &cb);
7375 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007376
7377 // when using Vim9 style "import.funcname" it needs to be expanded to
7378 // "import#funcname".
7379 expand_autload_callback(optcb);
7380
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007381 return OK;
7382#else
7383 return FAIL;
7384#endif
7385}