blob: 05e0527cd3a8bb3f7528167463ac3a32c93b8780 [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 {
224 char_u *cdpath;
225 char_u *buf;
226 int i;
227 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000228 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100230 // Initialize the 'cdpath' option's default value.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000231 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 if (cdpath != NULL)
233 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200234 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 if (buf != NULL)
236 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100237 buf[0] = ','; // start with ",", current dir first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 j = 1;
239 for (i = 0; cdpath[i] != NUL; ++i)
240 {
241 if (vim_ispathlistsep(cdpath[i]))
242 buf[j++] = ',';
243 else
244 {
245 if (cdpath[i] == ' ' || cdpath[i] == ',')
246 buf[j++] = '\\';
247 buf[j++] = cdpath[i];
248 }
249 }
250 buf[j] = NUL;
251 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000252 if (opt_idx >= 0)
253 {
254 options[opt_idx].def_val[VI_DEFAULT] = buf;
255 options[opt_idx].flags |= P_DEF_ALLOCED;
256 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200257 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100258 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000260 if (mustfree)
261 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 }
263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
ichizok02560422022-04-05 14:18:44 +0100265#if defined(FEAT_POSTSCRIPT) && \
266 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100267 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100269# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100271# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100273# elif defined(MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100275# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277# endif
278 );
279#endif
280
281#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100282 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100284# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000285 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100286# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
288
ichizok02560422022-04-05 14:18:44 +0100289# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291# endif
292 );
293#endif
294
295 /*
296 * Set all the options (except the terminal options) to their default
297 * value. Also set the global value for local options.
298 */
299 set_options_default(0);
300
matveytadbb1bf2022-02-01 17:26:12 +0000301#ifdef UNIX
302 // Force restricted-mode on for "nologin" or "false" $SHELL
303 p = get_isolated_shell_name();
304 if (p != NULL)
305 {
306 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
307 restricted = TRUE;
308 vim_free(p);
309 }
310#endif
311
Bram Moolenaar07268702018-03-01 21:57:32 +0100312#ifdef CLEAN_RUNTIMEPATH
313 if (clean_arg)
314 {
315 opt_idx = findoption((char_u *)"runtimepath");
316 if (opt_idx >= 0)
317 {
318 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
319 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
320 }
321 opt_idx = findoption((char_u *)"packpath");
322 if (opt_idx >= 0)
323 {
324 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
325 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
326 }
327 }
328#endif
329
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef FEAT_GUI
331 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100332 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333#endif
334
335 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100336 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100337 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 check_buf_options(curbuf);
339 check_win_options(curwin);
340 check_options();
341
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100342 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 didset_options();
344
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000345#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100346 // Use the current chartab for the generic chartab. This is not in
347 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000348 init_spell_chartab();
349#endif
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 /*
352 * Expand environment variables and things like "~" for the defaults.
353 * If option_expand() returns non-NULL the variable is expanded. This can
354 * only happen for non-indirect options.
355 * Also set the default to the expanded value, so ":set" does not list
356 * them.
357 * Don't set the P_ALLOCED flag, because we don't want to free the
358 * default.
359 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200360 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
362 if ((options[opt_idx].flags & P_GETTEXT)
363 && options[opt_idx].var != NULL)
364 p = (char_u *)_(*(char **)options[opt_idx].var);
365 else
366 p = option_expand(opt_idx, NULL);
367 if (p != NULL && (p = vim_strsave(p)) != NULL)
368 {
369 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100370 // VIMEXP
371 // Defaults for all expanded options are currently the same for Vi
372 // and Vim. When this changes, add some code here! Also need to
373 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 if (options[opt_idx].flags & P_DEF_ALLOCED)
375 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
376 options[opt_idx].def_val[VI_DEFAULT] = p;
377 options[opt_idx].flags |= P_DEF_ALLOCED;
378 }
379 }
380
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100381 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100384 // Detect use of mlterm.
385 // Mlterm is a terminal emulator akin to xterm that has some special
386 // abilities (bidi namely).
387 // NOTE: mlterm's author is being asked to 'set' a variable
388 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100390 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
392
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200393 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Bram Moolenaar4f974752019-02-17 17:44:42 +0100395# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 /*
397 * If $LANG isn't set, try to get a good value for it. This makes the
398 * right language be used automatically. Don't do this for English.
399 */
400 if (mch_getenv((char_u *)"LANG") == NULL)
401 {
402 char buf[20];
403
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100404 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
405 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
406 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
408 (LPTSTR)buf, 20);
409 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
410 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100411 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
413 STRCPY(buf, "zh_TW");
414 else if (STRNICMP(buf, "chs", 3) == 0
415 || STRNICMP(buf, "zhc", 3) == 0)
416 STRCPY(buf, "zh_CN");
417 else if (STRNICMP(buf, "jp", 2) == 0)
418 STRCPY(buf, "ja");
419 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100420 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100421 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 }
423 }
ichizok02560422022-04-05 14:18:44 +0100424# elif defined(MACOS_CONVERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100425 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000426 mac_lang_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427# endif
428
K.Takataf883d902021-05-30 18:04:19 +0200429# ifdef MSWIN
430 // MS-Windows has builtin support for conversion to and from Unicode, using
431 // "utf-8" for 'encoding' should work best for most users.
432 p = vim_strsave((char_u *)ENC_DFLT);
433# else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100434 // enc_locale() will try to find the encoding of the current locale.
K.Takataf883d902021-05-30 18:04:19 +0200435 // This works best for properly configured systems, old and new.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 p = enc_locale();
K.Takataf883d902021-05-30 18:04:19 +0200437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 if (p != NULL)
439 {
440 char_u *save_enc;
441
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100442 // Try setting 'encoding' and check if the value is valid.
K.Takataf883d902021-05-30 18:04:19 +0200443 // If not, go back to the default encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 save_enc = p_enc;
445 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000446 if (STRCMP(p_enc, "gb18030") == 0)
447 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100448 // We don't support "gb18030", but "cp936" is a good substitute
449 // for practical purposes, thus use that. It's not an alias to
450 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000451 p_enc = vim_strsave((char_u *)"cp936");
452 vim_free(p);
453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (mb_init() == NULL)
455 {
456 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000457 if (opt_idx >= 0)
458 {
459 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
460 options[opt_idx].flags |= P_DEF_ALLOCED;
461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
Bram Moolenaard0573012017-10-28 21:11:06 +0200463#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100464 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000465 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100466 // Adjust the default for 'isprint' and 'iskeyword' to match
467 // latin1. Also set the defaults for when 'nocompatible' is
468 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000469 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000470 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000471 set_string_option_direct((char_u *)"isk", -1,
472 ISK_LATIN1, OPT_FREE, SID_NONE);
473 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000474 if (opt_idx >= 0)
475 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000476 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000477 if (opt_idx >= 0)
478 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000479 (void)init_chartab();
480 }
481#endif
482
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200483#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100484 // Win32 console: When GetACP() returns a different value from
485 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200486 if (
487# ifdef VIMDLL
488 (!gui.in_use && !gui.starting) &&
489# endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100490 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 {
492 char buf[50];
493
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100494 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
495 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100496 if (GetConsoleCP() == 0)
497 sprintf(buf, "cp%ld", (long)GetACP());
498 else
499 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 p_tenc = vim_strsave((char_u *)buf);
501 if (p_tenc != NULL)
502 {
503 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000504 if (opt_idx >= 0)
505 {
506 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
507 options[opt_idx].flags |= P_DEF_ALLOCED;
508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 convert_setup(&input_conv, p_tenc, p_enc);
510 convert_setup(&output_conv, p_enc, p_tenc);
511 }
512 else
513 p_tenc = empty_option;
514 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100515#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100516#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100517 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000518 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 }
521 else
522 {
523 vim_free(p_enc);
524 p_enc = save_enc;
525 }
526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527
528#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100529 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 set_helplang_default(get_mess_lang());
531#endif
532}
533
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200534static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
535
536/*
537 * Set the "fileencodings" option to the default value for when 'encoding' is
538 * utf-8.
539 */
540 void
541set_fencs_unicode()
542{
543 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
544 OPT_FREE, 0);
545}
546
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547/*
548 * Set an option to its default value.
549 * This does not take care of side effects!
550 */
551 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100552set_option_default(
553 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100554 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
555 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100557 char_u *varp; // pointer to variable for current option
558 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000560 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
562
563 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
564 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100565 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {
567 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
568 if (flags & P_STRING)
569 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200570 // 'fencs' default value depends on 'encoding'
571 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
572 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100573 // Use set_string_option_direct() for local options to handle
574 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200575 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200576 set_string_option_direct(NULL, opt_idx,
577 options[opt_idx].def_val[dvi], opt_flags, 0);
578 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200580 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
581 free_string_option(*(char_u **)(varp));
582 *(char_u **)varp = options[opt_idx].def_val[dvi];
583 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 }
585 }
586 else if (flags & P_NUM)
587 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000588 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 win_comp_scroll(curwin);
590 else
591 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100592 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
593
594 if ((long *)varp == &curwin->w_p_so
595 || (long *)varp == &curwin->w_p_siso)
596 // 'scrolloff' and 'sidescrolloff' local values have a
597 // different default value than the global default.
598 *(long *)varp = -1;
599 else
600 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100601 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (both)
603 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100604 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 }
606 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100607 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100609 // the cast to long is required for Manx C, long_i is needed for
610 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000611 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000612#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100613 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000614 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
615 *(int *)varp = FALSE;
616#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100617 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 if (both)
619 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
620 *(int *)varp;
621 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000622
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100623 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000624 flagsp = insecure_flag(opt_idx, opt_flags);
625 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 }
627
628#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200629 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630#endif
631}
632
633/*
634 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200635 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 */
637 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100638set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100639 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
641 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000643 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
Bram Moolenaardac13472019-09-16 21:06:21 +0200645 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200646 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200647 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100648 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200649# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200650 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200651 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200652# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100653 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 set_option_default(i, opt_flags, p_cp);
655
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100656 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000657 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200659 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660}
661
662/*
663 * Set the Vi-default value of a string option.
664 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100665 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100667 static void
668set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
670 char_u *p;
671 int opt_idx;
672
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100673 if (escape && vim_strchr(val, ' ') != NULL)
674 p = vim_strsave_escaped(val, (char_u *)" ");
675 else
676 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100677 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
679 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000680 if (opt_idx >= 0)
681 {
682 if (options[opt_idx].flags & P_DEF_ALLOCED)
683 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
684 options[opt_idx].def_val[VI_DEFAULT] = p;
685 options[opt_idx].flags |= P_DEF_ALLOCED;
686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
688}
689
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100690 void
691set_string_default(char *name, char_u *val)
692{
693 set_string_default_esc(name, val, FALSE);
694}
695
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200697 * For an option value that contains comma separated items, find "newval" in
698 * "origval". Return NULL if not found.
699 */
700 static char_u *
701find_dup_item(char_u *origval, char_u *newval, long_u flags)
702{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200703 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200704 size_t newlen;
705 char_u *s;
706
707 if (origval == NULL)
708 return NULL;
709
710 newlen = STRLEN(newval);
711 for (s = origval; *s != NUL; ++s)
712 {
713 if ((!(flags & P_COMMA)
714 || s == origval
715 || (s[-1] == ',' && !(bs & 1)))
716 && STRNCMP(s, newval, newlen) == 0
717 && (!(flags & P_COMMA)
718 || s[newlen] == ','
719 || s[newlen] == NUL))
720 return s;
721 // Count backslashes. Only a comma with an even number of backslashes
722 // or a single backslash preceded by a comma before it is recognized as
723 // a separator.
724 if ((s > origval + 1
725 && s[-1] == '\\'
726 && s[-2] != ',')
727 || (s == origval + 1
728 && s[-1] == '\\'))
729 ++bs;
730 else
731 bs = 0;
732 }
733 return NULL;
734}
735
736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * Set the Vi-default value of a number option.
738 * Used for 'lines' and 'columns'.
739 */
740 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100741set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000743 int opt_idx;
744
745 opt_idx = findoption((char_u *)name);
746 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000747 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748}
749
Dominique Pelle748b3082022-01-08 12:41:16 +0000750#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200751/*
752 * Set all window-local and buffer-local options to the Vim default.
753 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200754 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200755 */
756 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200757set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200758{
759 win_T *save_curwin = curwin;
760 int i;
761
762 curwin = wp;
763 curbuf = curwin->w_buffer;
764 block_autocmds();
765
Bram Moolenaardac13472019-09-16 21:06:21 +0200766 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200767 {
768 struct vimoption *p = &(options[i]);
769 char_u *varp = get_varp_scope(p, OPT_LOCAL);
770
771 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200772 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200773 && !(options[i].flags & P_NODEFAULT)
774 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200775 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200776 }
777
778 unblock_autocmds();
779 curwin = save_curwin;
780 curbuf = curwin->w_buffer;
781}
Dominique Pelle748b3082022-01-08 12:41:16 +0000782#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200783
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000784#if defined(EXITFREE) || defined(PROTO)
785/*
786 * Free all options.
787 */
788 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100789free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000790{
791 int i;
792
Bram Moolenaardac13472019-09-16 21:06:21 +0200793 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000794 {
795 if (options[i].indir == PV_NONE)
796 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100797 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100798 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000799 free_string_option(*(char_u **)options[i].var);
800 if (options[i].flags & P_DEF_ALLOCED)
801 free_string_option(options[i].def_val[VI_DEFAULT]);
802 }
803 else if (options[i].var != VAR_WIN
804 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100805 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200806 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000807 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000808 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000809 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000810}
811#endif
812
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814/*
815 * Initialize the options, part two: After getting Rows and Columns and
816 * setting 'term'.
817 */
818 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100819set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000821 int idx;
822
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100824 * 'scroll' defaults to half the window height. The stored default is zero,
825 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000827 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000828 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000829 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 comp_col();
831
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000832 /*
833 * 'window' is only for backwards compatibility with Vi.
834 * Default is Rows - 1.
835 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000836 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000837 p_window = Rows - 1;
838 set_number_default("window", Rows - 1);
839
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100840 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100841#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000842 /*
843 * If 'background' wasn't set by the user, try guessing the value,
844 * depending on the terminal name. Only need to check for terminals
845 * with a dark background, that can handle color.
846 */
847 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000848 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
849 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000851 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100852 // don't mark it as set, when starting the GUI it may be
853 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000854 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
856#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000857
858#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100859 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000860#endif
861#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100862 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000863#endif
864#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100865 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867}
868
869/*
870 * Initialize the options, part three: After reading the .vimrc
871 */
872 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100873set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100875#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876/*
877 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
878 * This is done after other initializations, where 'shell' might have been
879 * set, but only if they have not been set before.
880 */
881 char_u *p;
882 int idx_srr;
883 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100884# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 int idx_sp;
886 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000890 if (idx_srr < 0)
891 do_srr = FALSE;
892 else
893 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100894# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000896 if (idx_sp < 0)
897 do_sp = FALSE;
898 else
899 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100900# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200901 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (p != NULL)
903 {
904 /*
905 * Default for p_sp is "| tee", for p_srr is ">".
906 * For known shells it is changed here to include stderr.
907 */
908 if ( fnamecmp(p, "csh") == 0
909 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100910# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 || fnamecmp(p, "csh.exe") == 0
912 || fnamecmp(p, "tcsh.exe") == 0
913# endif
914 )
915 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100916# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (do_sp)
918 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100919# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100921# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
925 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 if (do_srr)
928 {
929 p_srr = (char_u *)">&";
930 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
931 }
932 }
Mike Williams12795022021-06-28 20:53:58 +0200933# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200934 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
935 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200936 else if ( fnamecmp(p, "powershell") == 0
937 || fnamecmp(p, "powershell.exe") == 0
938 )
939 {
940# if defined(FEAT_QUICKFIX)
941 if (do_sp)
942 {
943 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
944 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
945 }
946# endif
947 if (do_srr)
948 {
949 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
950 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
951 }
952 }
953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 else
Natanael Copa56318362021-05-06 18:46:35 +0200955 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 if ( fnamecmp(p, "sh") == 0
957 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200958 || fnamecmp(p, "mksh") == 0
959 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000961 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200963 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200964 || fnamecmp(p, "ash") == 0
965 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200966 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100967# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 || fnamecmp(p, "cmd") == 0
969 || fnamecmp(p, "sh.exe") == 0
970 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200971 || fnamecmp(p, "mksh.exe") == 0
972 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000974 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 || fnamecmp(p, "bash.exe") == 0
976 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200977 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200978 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100980 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100982# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (do_sp)
984 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100985# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100987# else
Mike Williamsa3d1b292021-06-30 20:56:00 +0200988 if (fnamecmp(p, "pwsh") == 0)
989 p_sp = (char_u *)">%s 2>&1";
990 else
991 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100992# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
994 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (do_srr)
997 {
998 p_srr = (char_u *)">%s 2>&1";
999 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1000 }
1001 }
1002 vim_free(p);
1003 }
1004#endif
1005
Bram Moolenaar4f974752019-02-17 17:44:42 +01001006#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001008 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1009 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001011 * set, but only if they have not been set before.
1012 * Default values depend on shell (cmd.exe is default shell):
1013 *
1014 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001015 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001016 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001017 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001018 * "sh" like shells - "-c" "\""
1019 *
1020 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 */
Mike Williams12795022021-06-28 20:53:58 +02001022 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1023 {
1024 int idx_opt;
1025
1026 idx_opt = findoption((char_u *)"shcf");
1027 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1028 {
1029 p_shcf = (char_u*)"-Command";
1030 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1031 }
1032
1033 idx_opt = findoption((char_u *)"sxq");
1034 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1035 {
1036 p_sxq = (char_u*)"\"";
1037 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1038 }
1039 }
1040 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
1042 int idx3;
1043
1044 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001045 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {
1047 p_shcf = (char_u *)"-c";
1048 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1049 }
1050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001051 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001053 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 {
1055 p_sxq = (char_u *)"\"";
1056 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001059 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1060 {
1061 int idx3;
1062
1063 /*
1064 * cmd.exe on Windows will strip the first and last double quote given
1065 * on the command line, e.g. most of the time things like:
1066 * cmd /c "my path/to/echo" "my args to echo"
1067 * become:
1068 * my path/to/echo" "my args to echo
1069 * when executed.
1070 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001071 * To avoid this, set shellxquote to surround the command in
1072 * parenthesis. This appears to make most commands work, without
1073 * breaking commands that worked previously, such as
1074 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001075 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001076 idx3 = findoption((char_u *)"sxq");
1077 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1078 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001079 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001080 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1081 }
1082
Bram Moolenaara64ba222012-02-12 23:23:31 +01001083 idx3 = findoption((char_u *)"shcf");
1084 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1085 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001086 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001087 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1088 }
1089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090#endif
1091
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001092 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001093 {
1094 int idx_ffs = findoption((char_u *)"ffs");
1095
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001096 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001097 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1098 set_fileformat(default_fileformat(), OPT_LOCAL);
1099 }
1100
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102}
1103
1104#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1105/*
1106 * When 'helplang' is still at its default value, set it to "lang".
1107 * Only the first two characters of "lang" are used.
1108 */
1109 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001110set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111{
1112 int idx;
1113
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001114 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 return;
1116 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001117 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
1119 if (options[idx].flags & P_ALLOCED)
1120 free_string_option(p_hlg);
1121 p_hlg = vim_strsave(lang);
1122 if (p_hlg == NULL)
1123 p_hlg = empty_option;
1124 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001125 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001126 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001127 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1128 {
1129 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1130 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1131 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001132 // any C like setting, such as C.UTF-8, becomes "en"
1133 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1134 {
1135 p_hlg[0] = 'e';
1136 p_hlg[1] = 'n';
1137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 options[idx].flags |= P_ALLOCED;
1141 }
1142}
1143#endif
1144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145/*
1146 * 'title' and 'icon' only default to true if they have not been set or reset
1147 * in .vimrc and we can read the old value.
1148 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1149 * they can be reset. This reduces startup time when using X on a remote
1150 * machine.
1151 */
1152 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001153set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154{
1155 int idx1;
1156 long val;
1157
1158 /*
1159 * If GUI is (going to be) used, we can always set the window title and
1160 * icon name. Saves a bit of time, because the X11 display server does
1161 * not need to be contacted.
1162 */
1163 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001164 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {
1166#ifdef FEAT_GUI
1167 if (gui.starting || gui.in_use)
1168 val = TRUE;
1169 else
1170#endif
1171 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001172 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 p_title = val;
1174 }
1175 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001176 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {
1178#ifdef FEAT_GUI
1179 if (gui.starting || gui.in_use)
1180 val = TRUE;
1181 else
1182#endif
1183 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001184 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 p_icon = val;
1186 }
1187}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001189 void
1190ex_set(exarg_T *eap)
1191{
1192 int flags = 0;
1193
1194 if (eap->cmdidx == CMD_setlocal)
1195 flags = OPT_LOCAL;
1196 else if (eap->cmdidx == CMD_setglobal)
1197 flags = OPT_GLOBAL;
1198#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001199 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001200 ex_options(eap);
1201 else
1202#endif
1203 {
1204 if (eap->forceit)
1205 flags |= OPT_ONECOLUMN;
1206 (void)do_set(eap->arg, flags);
1207 }
1208}
1209
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210/*
1211 * Parse 'arg' for option settings.
1212 *
1213 * 'arg' may be IObuff, but only when no errors can be present and option
1214 * does not need to be expanded with option_expand().
1215 * "opt_flags":
1216 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001217 * OPT_GLOBAL for ":setglobal"
1218 * OPT_LOCAL for ":setlocal" and a modeline
1219 * OPT_MODELINE for a modeline
1220 * OPT_WINONLY to only set window-local options
1221 * OPT_NOWIN to skip setting window-local options
1222 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 *
1224 * returns FAIL if an error is detected, OK otherwise
1225 */
1226 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001227do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02001228 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001229 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230{
Bram Moolenaar1594f312021-07-08 16:40:13 +02001231 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001233 char *errmsg;
1234 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001236 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1237 int nextchar; // next non-white char after option name
1238 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 int len;
1240 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001241 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001243 long_u flags; // flags for current option
1244 char_u *varp = NULL; // pointer to variable for current option
1245 int did_show = FALSE; // already showed one value
1246 int adding; // "opt+=arg"
1247 int prepending; // "opt^=arg"
1248 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 int cp_val = 0;
1250 char_u key_name[2];
1251
1252 if (*arg == NUL)
1253 {
1254 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001255 did_show = TRUE;
1256 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001259 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
1261 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001262 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001264 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1265 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 {
1267 /*
1268 * ":set all" show all options.
1269 * ":set all&" set all options to their default value.
1270 */
1271 arg += 3;
1272 if (*arg == '&')
1273 {
1274 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001275 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001277 didset_options();
1278 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001279 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
1281 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001284 did_show = TRUE;
1285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001287 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 {
1289 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001290 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001291 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 arg += 7;
1293 }
1294 else
1295 {
1296 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001297 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
1299 prefix = 0;
1300 arg += 2;
1301 }
1302 else if (STRNCMP(arg, "inv", 3) == 0)
1303 {
1304 prefix = 2;
1305 arg += 3;
1306 }
1307
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001308 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 key = 0;
1310 if (*arg == '<')
1311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001313 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1315 len = 5;
1316 else
1317 {
1318 len = 1;
1319 while (arg[len] != NUL && arg[len] != '>')
1320 ++len;
1321 }
1322 if (arg[len] != '>')
1323 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001324 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 goto skip;
1326 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001327 arg[len] = NUL; // put NUL after name
1328 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001330 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001332 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 }
1334 else
1335 {
1336 len = 0;
1337 /*
1338 * The two characters after "t_" may not be alphanumeric.
1339 */
1340 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1341 len = 4;
1342 else
1343 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1344 ++len;
1345 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001346 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001348 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001350 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 }
1352
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001353 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 afterchar = arg[len];
1355
Bram Moolenaardeb108b2021-07-08 17:35:36 +02001356 if (in_vim9script())
1357 {
1358 char_u *p = skipwhite(arg + len);
1359
1360 // disallow white space before =val, +=val, -=val, ^=val
1361 if (p > arg + len && (p[0] == '='
1362 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1363 && p[1] == '=')))
1364 {
1365 errmsg = e_no_white_space_allowed_between_option_and;
1366 arg = p;
1367 startarg = p;
1368 goto skip;
1369 }
1370 }
1371 else
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001372 // skip white space, allow ":set ai ?", ":set hlsearch !"
1373 while (VIM_ISWHITE(arg[len]))
1374 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
1376 adding = FALSE;
1377 prepending = FALSE;
1378 removing = FALSE;
1379 if (arg[len] != NUL && arg[len + 1] == '=')
1380 {
1381 if (arg[len] == '+')
1382 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001383 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 ++len;
1385 }
1386 else if (arg[len] == '^')
1387 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001388 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 ++len;
1390 }
1391 else if (arg[len] == '-')
1392 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001393 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 ++len;
1395 }
1396 }
1397 nextchar = arg[len];
1398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001399 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 {
Bram Moolenaar1594f312021-07-08 16:40:13 +02001401 if (in_vim9script() && arg > arg_start
1402 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1403 errmsg = e_no_white_space_allowed_between_option_and;
1404 else
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001405 errmsg = e_unknown_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 goto skip;
1407 }
1408
1409 if (opt_idx >= 0)
1410 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001411 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001413 // Only give an error message when requesting the value of
1414 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1416 && (!(options[opt_idx].flags & P_BOOL)
1417 || nextchar == '?'))
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001418 errmsg = e_option_not_supported;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 goto skip;
1420 }
1421
1422 flags = options[opt_idx].flags;
1423 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1424 }
1425 else
1426 {
1427 flags = P_STRING;
1428 if (key < 0)
1429 {
1430 key_name[0] = KEY2TERMCAP0(key);
1431 key_name[1] = KEY2TERMCAP1(key);
1432 }
1433 else
1434 {
1435 key_name[0] = KS_KEY;
1436 key_name[1] = (key & 0xff);
1437 }
1438 }
1439
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001440 // Skip all options that are not window-local (used when showing
1441 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001442 if ((opt_flags & OPT_WINONLY)
1443 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1444 goto skip;
1445
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001446 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001447 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1448 && options[opt_idx].var == VAR_WIN)
1449 goto skip;
1450
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001451 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001452 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001454 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001455 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001456 errmsg = e_not_allowed_in_modeline;
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001457 goto skip;
1458 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001459 if ((flags & P_MLE) && !p_mle)
1460 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001461 errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
Bram Moolenaar110289e2019-05-23 15:38:06 +02001462 goto skip;
1463 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001464#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001465 // In diff mode some options are overruled. This avoids that
1466 // 'foldmethod' becomes "marker" instead of "diff" and that
1467 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001468 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001469 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001470 && (
1471#ifdef FEAT_FOLDING
1472 options[opt_idx].indir == PV_FDM ||
1473#endif
1474 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001475 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 }
1478
1479#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001480 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001481 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001483 errmsg = e_not_allowed_in_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 goto skip;
1485 }
1486#endif
1487
1488 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1489 {
1490 arg += len;
1491 cp_val = p_cp;
1492 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1493 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001494 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
1496 cp_val = FALSE;
1497 arg += 3;
1498 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001499 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
1501 cp_val = TRUE;
1502 arg += 2;
1503 }
1504 }
1505 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001506 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001508 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 goto skip;
1510 }
1511 }
1512
1513 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001514 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001515 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 */
1517 if (nextchar == '?'
1518 || (prefix == 1
1519 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1520 && !(flags & P_BOOL)))
1521 {
1522 /*
1523 * print value
1524 */
1525 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001526 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 else
1528 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001529 gotocmdline(TRUE); // cursor at status line
1530 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532 if (opt_idx >= 0)
1533 {
1534 showoneopt(&options[opt_idx], opt_flags);
1535#ifdef FEAT_EVAL
1536 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001537 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001538 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001539 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001540 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001541 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001542 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001543 (int)options[opt_idx].indir & PV_MASK]);
1544 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001545 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001546 (int)options[opt_idx].indir & PV_MASK]);
1547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548#endif
1549 }
1550 else
1551 {
1552 char_u *p;
1553
1554 p = find_termcode(key_name);
1555 if (p == NULL)
1556 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001557 errmsg = e_key_code_not_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 goto skip;
1559 }
1560 else
1561 (void)show_one_termcode(key_name, p, TRUE);
1562 }
1563 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001564 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001565 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 }
1567 else
1568 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001569 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001570 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001571
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001572 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 {
1574 if (nextchar == '=' || nextchar == ':')
1575 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001576 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 goto skip;
1578 }
1579
1580 /*
1581 * ":set opt!": invert
1582 * ":set opt&": reset to default value
1583 * ":set opt<": reset to global value
1584 */
1585 if (nextchar == '!')
1586 value = *(int *)(varp) ^ 1;
1587 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001588 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 ((flags & P_VI_DEF) || cp_val)
1590 ? VI_DEFAULT : VIM_DEFAULT];
1591 else if (nextchar == '<')
1592 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001593 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if ((int *)varp == &curbuf->b_p_ar
1595 && opt_flags == OPT_LOCAL)
1596 value = -1;
1597 else
1598 value = *(int *)get_varp_scope(&(options[opt_idx]),
1599 OPT_GLOBAL);
1600 }
1601 else
1602 {
1603 /*
1604 * ":set invopt": invert
1605 * ":set opt" or ":set noopt": set or reset
1606 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001607 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001609 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 goto skip;
1611 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001612 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 value = *(int *)(varp) ^ 1;
1614 else
1615 value = prefix;
1616 }
1617
1618 errmsg = set_bool_option(opt_idx, varp, (int)value,
1619 opt_flags);
1620 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001621 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1624 || prefix != 1)
1625 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001626 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 goto skip;
1628 }
1629
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001630 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 {
1632 /*
1633 * Different ways to set a number option:
1634 * & set to default value
1635 * < set to global value
1636 * <xx> accept special key codes for 'wildchar'
1637 * c accept any non-digit for 'wildchar'
1638 * [-]0-9 set number
1639 * other error
1640 */
1641 ++arg;
1642 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001643 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 ((flags & P_VI_DEF) || cp_val)
1645 ? VI_DEFAULT : VIM_DEFAULT];
1646 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001647 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001648 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1649 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001650 if ((long *)varp == &curbuf->b_p_ul
1651 && opt_flags == OPT_LOCAL)
1652 value = NO_LOCAL_UNDOLEVEL;
1653 else
1654 value = *(long *)get_varp_scope(
1655 &(options[opt_idx]), OPT_GLOBAL);
1656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 else if (((long *)varp == &p_wc
1658 || (long *)varp == &p_wcm)
1659 && (*arg == '<'
1660 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001661 || (*arg != NUL
1662 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 && !VIM_ISDIGIT(*arg))))
1664 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001665 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (value == 0 && (long *)varp != &p_wcm)
1667 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001668 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 goto skip;
1670 }
1671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1673 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001674 // Allow negative (for 'undolevels'), octal and
1675 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001676 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001677 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001678 if (i == 0 || (arg[i] != NUL
1679 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001681 errmsg = e_number_required_after_equal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 goto skip;
1683 }
1684 }
1685 else
1686 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001687 errmsg = e_number_required_after_equal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 goto skip;
1689 }
1690
1691 if (adding)
1692 value = *(long *)varp + value;
1693 if (prepending)
1694 value = *(long *)varp * value;
1695 if (removing)
1696 value = *(long *)varp - value;
1697 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001698 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001700 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001702 char_u *save_arg = NULL;
1703 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001704 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001705 char_u *newval;
1706 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001707 char_u *origval_l = NULL;
1708 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001709#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001710 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001711 char_u *saved_origval_l = NULL;
1712 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001713 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001714#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001715 unsigned newlen;
1716 int comma;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001718 // When using ":set opt=val" for a global option
1719 // with a local value the local value will be
1720 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001722 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 varp = options[opt_idx].var;
1724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001725 // The old value is kept until we are sure that the
1726 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001728
Bram Moolenaard7c96872019-06-15 17:12:48 +02001729 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1730 {
1731 origval_l = *(char_u **)get_varp_scope(
1732 &(options[opt_idx]), OPT_LOCAL);
1733 origval_g = *(char_u **)get_varp_scope(
1734 &(options[opt_idx]), OPT_GLOBAL);
1735
1736 // A global-local string option might have an empty
1737 // option as value to indicate that the global
1738 // value should be used.
1739 if (((int)options[opt_idx].indir & PV_BOTH)
1740 && origval_l == empty_option)
1741 origval_l = origval_g;
1742 }
1743
1744 // When setting the local value of a global
1745 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001746 if (((int)options[opt_idx].indir & PV_BOTH)
1747 && (opt_flags & OPT_LOCAL))
1748 origval = *(char_u **)get_varp(
1749 &options[opt_idx]);
1750 else
1751 origval = oldval;
1752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001753 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 {
1755 newval = options[opt_idx].def_val[
1756 ((flags & P_VI_DEF) || cp_val)
1757 ? VI_DEFAULT : VIM_DEFAULT];
1758 if ((char_u **)varp == &p_bg)
1759 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001760 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761#ifdef FEAT_GUI
1762 if (gui.in_use)
1763 newval = gui_bg_default();
1764 else
1765#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001766 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001768 else if ((char_u **)varp == &p_fencs && enc_utf8)
1769 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001771 // expand environment variables and ~ since the
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001772 // default value was already expanded, only
1773 // required when an environment variable was set
1774 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 if (newval == NULL)
1776 newval = empty_option;
1777 else
1778 {
1779 s = option_expand(opt_idx, newval);
1780 if (s == NULL)
1781 s = newval;
1782 newval = vim_strsave(s);
1783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001785 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 {
1787 newval = vim_strsave(*(char_u **)get_varp_scope(
1788 &(options[opt_idx]), OPT_GLOBAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 }
1790 else
1791 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001792 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794 /*
1795 * Set 'keywordprg' to ":help" if an empty
1796 * value was passed to :set by the user.
1797 * Misuse errbuf[] for the resulting string.
1798 */
1799 if (varp == (char_u *)&p_kp
1800 && (*arg == NUL || *arg == ' '))
1801 {
1802 STRCPY(errbuf, ":help");
1803 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001804 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
1806 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001807 * Convert 'backspace' number to string, for
1808 * adding, prepending and removing string.
1809 */
1810 else if (varp == (char_u *)&p_bs
1811 && VIM_ISDIGIT(**(char_u **)varp))
1812 {
1813 i = getdigits((char_u **)varp);
1814 switch (i)
1815 {
1816 case 0:
1817 *(char_u **)varp = empty_option;
1818 break;
1819 case 1:
1820 *(char_u **)varp = vim_strsave(
1821 (char_u *)"indent,eol");
1822 break;
1823 case 2:
1824 *(char_u **)varp = vim_strsave(
1825 (char_u *)"indent,eol,start");
1826 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001827 case 3:
1828 *(char_u **)varp = vim_strsave(
1829 (char_u *)"indent,eol,nostop");
1830 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001831 }
1832 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001833 if (origval == oldval)
1834 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001835 if (origval_l == oldval)
1836 origval_l = *(char_u **)varp;
1837 if (origval_g == oldval)
1838 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001839 oldval = *(char_u **)varp;
1840 }
1841 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 * Convert 'whichwrap' number to string, for
1843 * backwards compatibility with Vim 3.0.
1844 * Misuse errbuf[] for the resulting string.
1845 */
1846 else if (varp == (char_u *)&p_ww
1847 && VIM_ISDIGIT(*arg))
1848 {
1849 *errbuf = NUL;
1850 i = getdigits(&arg);
1851 if (i & 1)
1852 STRCAT(errbuf, "b,");
1853 if (i & 2)
1854 STRCAT(errbuf, "s,");
1855 if (i & 4)
1856 STRCAT(errbuf, "h,l,");
1857 if (i & 8)
1858 STRCAT(errbuf, "<,>,");
1859 if (i & 16)
1860 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001861 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 errbuf[STRLEN(errbuf) - 1] = NUL;
1863 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001864 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866 /*
1867 * Remove '>' before 'dir' and 'bdir', for
1868 * backwards compatibility with version 3.0
1869 */
1870 else if ( *arg == '>'
1871 && (varp == (char_u *)&p_dir
1872 || varp == (char_u *)&p_bdir))
1873 {
1874 ++arg;
1875 }
1876
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 /*
1878 * Copy the new string into allocated memory.
1879 * Can't use set_string_option_direct(), because
1880 * we need to remove the backslashes.
1881 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001882 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 newlen = (unsigned)STRLEN(arg) + 1;
1884 if (adding || prepending || removing)
1885 newlen += (unsigned)STRLEN(origval) + 1;
1886 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001887 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 break;
1889 s = newval;
1890
1891 /*
1892 * Copy the string, skip over escaped chars.
1893 * For MS-DOS and WIN32 backslashes before normal
1894 * file name characters are not removed, and keep
1895 * backslash at start, for "\\machine\path", but
1896 * do remove it for "\\\\machine\\path".
1897 * The reverse is found in ExpandOldSetting().
1898 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001899 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
1901 if (*arg == '\\' && arg[1] != NUL
1902#ifdef BACKSLASH_IN_FILENAME
1903 && !((flags & P_EXPAND)
1904 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001905 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 && (arg[1] != '\\'
1907 || (s == newval
1908 && arg[2] != '\\')))
1909#endif
1910 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001911 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001913 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001915 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 mch_memmove(s, arg, (size_t)i);
1917 arg += i;
1918 s += i;
1919 }
1920 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 *s++ = *arg++;
1922 }
1923 *s = NUL;
1924
1925 /*
1926 * Expand environment variables and ~.
1927 * Don't do it when adding without inserting a
1928 * comma.
1929 */
1930 if (!(adding || prepending || removing)
1931 || (flags & P_COMMA))
1932 {
1933 s = option_expand(opt_idx, newval);
1934 if (s != NULL)
1935 {
1936 vim_free(newval);
1937 newlen = (unsigned)STRLEN(s) + 1;
1938 if (adding || prepending || removing)
1939 newlen += (unsigned)STRLEN(origval) + 1;
1940 newval = alloc(newlen);
1941 if (newval == NULL)
1942 break;
1943 STRCPY(newval, s);
1944 }
1945 }
1946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001947 // locate newval[] in origval[] when removing it
1948 // and when adding to avoid duplicates
1949 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (removing || (flags & P_NODUP))
1951 {
1952 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001953 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001955 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001956 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
1958 prepending = FALSE;
1959 adding = FALSE;
1960 STRCPY(newval, origval);
1961 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001962
1963 // if no duplicate, move pointer to end of
1964 // original value
1965 if (s == NULL)
1966 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
1968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001969 // concatenate the two strings; add a ',' if
1970 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 if (adding || prepending)
1972 {
1973 comma = ((flags & P_COMMA) && *origval != NUL
1974 && *newval != NUL);
1975 if (adding)
1976 {
1977 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001978 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001979 if (comma && i > 1
1980 && (flags & P_ONECOMMA) == P_ONECOMMA
1981 && origval[i - 1] == ','
1982 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001983 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 mch_memmove(newval + i + comma, newval,
1985 STRLEN(newval) + 1);
1986 mch_memmove(newval, origval, (size_t)i);
1987 }
1988 else
1989 {
1990 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001991 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 }
1993 if (comma)
1994 newval[i] = ',';
1995 }
1996
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001997 // Remove newval[] from origval[]. (Note: "i" has
1998 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 if (removing)
2000 {
2001 STRCPY(newval, origval);
2002 if (*s)
2003 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002004 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 if (flags & P_COMMA)
2006 {
2007 if (s == origval)
2008 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002009 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 if (s[i] == ',')
2011 ++i;
2012 }
2013 else
2014 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002015 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 --s;
2017 ++i;
2018 }
2019 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002020 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 }
2022 }
2023
2024 if (flags & P_FLAGLIST)
2025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002026 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002027 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002028 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002029 // if options have P_FLAGLIST and
2030 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002031 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002033 if (*s != ',' && *(s + 1) == ','
2034 && vim_strchr(s + 2, *s) != NULL)
2035 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002036 // Remove the duplicated value and
2037 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002038 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002039 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002042 else
2043 {
2044 if ((!(flags & P_COMMA) || *s != ',')
2045 && vim_strchr(s + 1, *s) != NULL)
2046 {
2047 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002048 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002049 }
2050 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002051 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
2054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002055 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 arg = save_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
2058
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002059 /*
2060 * Set the new value.
2061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 *(char_u **)(varp) = newval;
2063
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002064#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002065 if (!starting
2066# ifdef FEAT_CRYPT
2067 && options[opt_idx].indir != PV_KEY
2068# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002069 && origval != NULL && newval != NULL)
2070 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002071 // origval may be freed by
2072 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002073 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002074 // newval (and varp) may become invalid if the
2075 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002076 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002077 if (origval_l != NULL)
2078 saved_origval_l = vim_strsave(origval_l);
2079 if (origval_g != NULL)
2080 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002081 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002082#endif
2083
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002084 {
2085 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002086 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002087
2088 // When an option is set in the sandbox, from a
2089 // modeline or in secure mode, then deal with side
2090 // effects in secure mode. Also when the value was
2091 // set with the P_INSECURE flag and is not
2092 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002093 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002094#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002095 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002096#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002097 || (!value_is_replaced && (*p & P_INSECURE)))
2098 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002099
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002100 // Handle side effects, and set the global value
2101 // for ":set" on local options. Note: when setting
2102 // 'syntax' or 'filetype' autocommands may be
2103 // triggered that can cause havoc.
2104 errmsg = did_set_string_option(
zeertzjqf6782732022-07-27 18:26:03 +01002105 opt_idx, (char_u **)varp, oldval, errbuf,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002106 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002107
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002108 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002111#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002112 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002113 trigger_optionsset_string(
2114 opt_idx, opt_flags, saved_origval,
2115 saved_origval_l, saved_origval_g,
2116 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002117 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002118 vim_free(saved_origval_l);
2119 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002120 vim_free(saved_newval);
2121#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002122 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (errmsg != NULL)
2124 goto skip;
2125 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002126 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
2128 char_u *p;
2129
2130 if (nextchar == '&')
2131 {
2132 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002133 errmsg = e_not_found_in_termcap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135 else
2136 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002137 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002138 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (*p == '\\' && p[1] != NUL)
2140 ++p;
2141 nextchar = *p;
2142 *p = NUL;
2143 add_termcode(key_name, arg, FALSE);
2144 *p = nextchar;
2145 }
2146 if (full_screen)
2147 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002148 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 }
2150 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002151
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002153 did_set_option(
2154 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156
2157skip:
2158 /*
2159 * Advance to next argument.
2160 * - skip until a blank found, taking care of backslashes
2161 * - skip blanks
2162 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2163 */
2164 for (i = 0; i < 2 ; ++i)
2165 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002166 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 if (*arg++ == '\\' && *arg != NUL)
2168 ++arg;
2169 arg = skipwhite(arg);
2170 if (*arg != '=')
2171 break;
2172 }
2173 }
2174
2175 if (errmsg != NULL)
2176 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002177 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002178 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 if (i + (arg - startarg) < IOSIZE)
2180 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002181 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 STRCAT(IObuff, ": ");
2183 mch_memmove(IObuff + i, startarg, (arg - startarg));
2184 IObuff[i + (arg - startarg)] = NUL;
2185 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002186 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 trans_characters(IObuff, IOSIZE);
2188
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002189 ++no_wait_return; // wait_return done later
2190 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 --no_wait_return;
2192
2193 return FAIL;
2194 }
2195
2196 arg = skipwhite(arg);
2197 }
2198
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002199theend:
2200 if (silent_mode && did_show)
2201 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002202 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002203 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002204 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002205 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002206 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002207 out_flush();
2208 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002209 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002210 }
2211
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 return OK;
2213}
2214
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002215/*
2216 * Call this when an option has been given a new value through a user command.
2217 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2218 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002219 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002220did_set_option(
2221 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002222 int opt_flags, // possibly with OPT_MODELINE
2223 int new_value, // value was replaced completely
2224 int value_checked) // value was checked to be safe, no need to set the
2225 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002226{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002227 long_u *p;
2228
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002229 options[opt_idx].flags |= P_WAS_SET;
2230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002231 // When an option is set in the sandbox, from a modeline or in secure mode
2232 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2233 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002234 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002235 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002236#ifdef HAVE_SANDBOX
2237 || sandbox != 0
2238#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002239 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002240 *p = *p | P_INSECURE;
2241 else if (new_value)
2242 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002243}
2244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245/*
2246 * Convert a key name or string into a key value.
2247 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002248 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002250 int
2251string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252{
2253 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002254 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (*arg == '^')
2256 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002257 if (multi_byte)
2258 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 return *arg;
2260}
2261
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262/*
2263 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2264 * maketitle() to create and display it.
2265 * When switching the title or icon off, call mch_restore_title() to get
2266 * the old value back.
2267 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002268 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002269did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270{
2271 if (starting != NO_SCREEN
2272#ifdef FEAT_GUI
2273 && !gui.starting
2274#endif
2275 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
2279/*
2280 * set_options_bin - called when 'bin' changes value.
2281 */
2282 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002283set_options_bin(
2284 int oldval,
2285 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002286 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 /*
2289 * The option values that are changed when 'bin' changes are
2290 * copied when 'bin is set and restored when 'bin' is reset.
2291 */
2292 if (newval)
2293 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002294 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 if (!(opt_flags & OPT_GLOBAL))
2297 {
2298 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2299 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2300 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2301 curbuf->b_p_et_nobin = curbuf->b_p_et;
2302 }
2303 if (!(opt_flags & OPT_LOCAL))
2304 {
2305 p_tw_nobin = p_tw;
2306 p_wm_nobin = p_wm;
2307 p_ml_nobin = p_ml;
2308 p_et_nobin = p_et;
2309 }
2310 }
2311
2312 if (!(opt_flags & OPT_GLOBAL))
2313 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002314 curbuf->b_p_tw = 0; // no automatic line wrap
2315 curbuf->b_p_wm = 0; // no automatic line wrap
2316 curbuf->b_p_ml = 0; // no modelines
2317 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319 if (!(opt_flags & OPT_LOCAL))
2320 {
2321 p_tw = 0;
2322 p_wm = 0;
2323 p_ml = FALSE;
2324 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002325 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002328 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
2330 if (!(opt_flags & OPT_GLOBAL))
2331 {
2332 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2333 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2334 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2335 curbuf->b_p_et = curbuf->b_p_et_nobin;
2336 }
2337 if (!(opt_flags & OPT_LOCAL))
2338 {
2339 p_tw = p_tw_nobin;
2340 p_wm = p_wm_nobin;
2341 p_ml = p_ml_nobin;
2342 p_et = p_et_nobin;
2343 }
2344 }
2345}
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347/*
2348 * Expand environment variables for some string options.
2349 * These string options cannot be indirect!
2350 * If "val" is NULL expand the current value of the option.
2351 * Return pointer to NameBuff, or NULL when not expanded.
2352 */
2353 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002354option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002356 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2358 return NULL;
2359
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002360 // If val is longer than MAXPATHL no meaningful expansion can be done,
2361 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 if (val != NULL && STRLEN(val) > MAXPATHL)
2363 return NULL;
2364
2365 if (val == NULL)
2366 val = *(char_u **)options[opt_idx].var;
2367
2368 /*
2369 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2370 * Escape spaces when expanding 'tags', they are used to separate file
2371 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002372 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 */
2374 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002375 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002376#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002377 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2378#endif
2379 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002380 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 return NULL;
2382
2383 return NameBuff;
2384}
2385
2386/*
2387 * After setting various option values: recompute variables that depend on
2388 * option values.
2389 */
2390 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002391didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002393 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 (void)init_chartab();
2395
Bram Moolenaardac13472019-09-16 21:06:21 +02002396 didset_string_options();
2397
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002398#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002399 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002400 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002401 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002402 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002405 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 (void)check_cedit();
2407#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002408#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002409 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002410 fill_breakat_flags();
2411#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002412 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002413}
2414
2415/*
2416 * More side effects of setting options.
2417 */
2418 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002419didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002420{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002421 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002422 (void)highlight_changed();
2423
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002424 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002425 check_opt_wim();
2426
Bram Moolenaareed9d462021-02-15 20:38:25 +01002427 // Parse default for 'listchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002428 (void)set_chars_option(curwin, &curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002430 // Parse default for 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002431 (void)set_chars_option(curwin, &curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002432
2433#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002434 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002435 (void)check_clipboard_option();
2436#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002437#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002438 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002439 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002440 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002441 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443}
2444
2445/*
2446 * Check for string options that are NULL (normally only termcap options).
2447 */
2448 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002449check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450{
2451 int opt_idx;
2452
2453 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2454 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2455 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2456}
2457
2458/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002459 * Return the option index found by a pointer into term_strings[].
2460 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002462 int
2463get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002465 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2468 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002469 return opt_idx;
2470 return -1; // cannot happen: didn't find it!
2471}
2472
2473/*
2474 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2475 * Return the option index or -1 if not found.
2476 */
2477 int
2478set_term_option_alloced(char_u **p)
2479{
2480 int opt_idx = get_term_opt_idx(p);
2481
2482 if (opt_idx >= 0)
2483 options[opt_idx].flags |= P_ALLOCED;
2484 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485}
2486
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002487#if defined(FEAT_EVAL) || defined(PROTO)
2488/*
2489 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2490 * Return FALSE when it wasn't.
2491 * Return -1 for an unknown option.
2492 */
2493 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002494was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002495{
2496 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002497 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002498
2499 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002500 {
2501 flagp = insecure_flag(idx, opt_flags);
2502 return (*flagp & P_INSECURE) != 0;
2503 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002504 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002505 return -1;
2506}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002507
2508/*
2509 * Get a pointer to the flags used for the P_INSECURE flag of option
2510 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002511 * NOTE: Caller must make sure that "curwin" is set to the window from which
2512 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002513 */
2514 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002515insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002516{
2517 if (opt_flags & OPT_LOCAL)
2518 switch ((int)options[opt_idx].indir)
2519 {
2520#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002521 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002522#endif
2523#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002524# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002525 case PV_FDE: return &curwin->w_p_fde_flags;
2526 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002527# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002528# ifdef FEAT_BEVAL
2529 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2530# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002531 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002532 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002533# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002534 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002535# endif
2536#endif
2537 }
2538
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002539 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002540 return &options[opt_idx].flags;
2541}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002542#endif
2543
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002544/*
2545 * Redraw the window title and/or tab page text later.
2546 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002547void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002548{
2549 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002550 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002551}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002552
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002554 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2555 * characters or characters in "allowed".
2556 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002557 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002558valid_name(char_u *val, char *allowed)
2559{
2560 char_u *s;
2561
2562 for (s = val; *s != NUL; ++s)
2563 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2564 return FALSE;
2565 return TRUE;
2566}
2567
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002568#if defined(FEAT_EVAL) || defined(PROTO)
2569/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002570 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002571 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002572 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002573 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002574set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002575{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002576 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2577 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002578 sctx_T new_script_ctx = script_ctx;
2579
Bram Moolenaar51258742020-05-03 17:19:33 +02002580 // Modeline already has the line number set.
2581 if (!(opt_flags & OPT_MODELINE))
2582 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002583
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002584 // Remember where the option was set. For local options need to do that
2585 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002586 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002587 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002588 if (both || (opt_flags & OPT_LOCAL))
2589 {
2590 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002591 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002592 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002593 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002594 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002595 if (both)
2596 // also setting the "all buffers" value
2597 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2598 new_script_ctx;
2599 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002600 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002601}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002602
2603/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002604 * Get the script context of global option "name".
2605 *
2606 */
2607 sctx_T *
2608get_option_sctx(char *name)
2609{
2610 int idx = findoption((char_u *)name);
2611
2612 if (idx >= 0)
2613 return &options[idx].script_ctx;
2614 siemsg("no such option: %s", name);
2615 return NULL;
2616}
2617
2618/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002619 * Set the script_ctx for a termcap option.
2620 * "name" must be the two character code, e.g. "RV".
2621 * When "name" is NULL use "opt_idx".
2622 */
2623 void
2624set_term_option_sctx_idx(char *name, int opt_idx)
2625{
2626 char_u buf[5];
2627 int idx;
2628
2629 if (name == NULL)
2630 idx = opt_idx;
2631 else
2632 {
2633 buf[0] = 't';
2634 buf[1] = '_';
2635 buf[2] = name[0];
2636 buf[3] = name[1];
2637 buf[4] = 0;
2638 idx = findoption(buf);
2639 }
2640 if (idx >= 0)
2641 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2642}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002643#endif
2644
Bram Moolenaarf4140482020-02-15 23:06:45 +01002645#if defined(FEAT_EVAL)
2646/*
2647 * Apply the OptionSet autocommand.
2648 */
2649 static void
2650apply_optionset_autocmd(
2651 int opt_idx,
2652 long opt_flags,
2653 long oldval,
2654 long oldval_g,
2655 long newval,
2656 char *errmsg)
2657{
2658 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2659
2660 // Don't do this while starting up, failure or recursively.
2661 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2662 return;
2663
2664 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2665 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2666 oldval_g);
2667 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2668 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2669 (opt_flags & OPT_LOCAL) ? "local" : "global");
2670 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2671 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2672 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2673 if (opt_flags & OPT_LOCAL)
2674 {
2675 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2676 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2677 }
2678 if (opt_flags & OPT_GLOBAL)
2679 {
2680 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2681 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2682 }
2683 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2684 {
2685 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2686 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2687 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2688 }
2689 if (opt_flags & OPT_MODELINE)
2690 {
2691 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2692 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2693 }
2694 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2695 NULL, FALSE, NULL);
2696 reset_v_option_vars();
2697}
2698#endif
2699
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700/*
2701 * Set the value of a boolean option, and take care of side effects.
2702 * Returns NULL for success, or an error message for an error.
2703 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002704 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002705set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002706 int opt_idx, // index in options[] table
2707 char_u *varp, // pointer to the option variable
2708 int value, // new value
2709 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710{
2711 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002712#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002713 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002714#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002715 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002717 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if ((secure
2719#ifdef HAVE_SANDBOX
2720 || sandbox != 0
2721#endif
2722 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002723 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002725#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002726 // Save the global value before changing anything. This is needed as for
2727 // a global-only option setting the "local value" in fact sets the global
2728 // value (since there is only one value).
2729 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2730 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2731 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002732#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002733
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002734 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002737 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738#endif
2739
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002740#ifdef FEAT_GUI
2741 need_mouse_correct = TRUE;
2742#endif
2743
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002744 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2746 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2747
2748 /*
2749 * Handle side effects of changing a bool option.
2750 */
2751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002752 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
Bram Moolenaar920694c2016-08-21 17:45:02 +02002756#ifdef FEAT_LANGMAP
2757 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002758 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002759 p_lnr = !p_lrm;
2760 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002761 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002762 p_lrm = !p_lnr;
2763#endif
2764
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002765#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002766 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002767 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2768 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // Only take action when the option was set. When reset we do not
2770 // delete the undo file, the option may be set again without making
2771 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002772 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002773 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002774 char_u hash[UNDO_HASH_SIZE];
2775 buf_T *save_curbuf = curbuf;
2776
Bram Moolenaar29323592016-07-24 22:04:11 +02002777 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002778 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002779 // When 'undofile' is set globally: for every buffer, otherwise
2780 // only for the current buffer: Try to read in the undofile,
2781 // if one exists, the buffer wasn't changed and the buffer was
2782 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002783 if ((curbuf == save_curbuf
2784 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2785 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2786 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002787#ifdef FEAT_CRYPT
2788 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2789 continue;
2790#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002791 u_compute_hash(hash);
2792 u_read_undo(NULL, hash, curbuf->b_fname);
2793 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002794 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002795 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002796 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002797 }
2798#endif
2799
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 else if ((int *)varp == &curbuf->b_p_ro)
2801 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002802 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2804 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002805
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002806 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002807 if (curbuf->b_p_ro)
2808 curbuf->b_did_warn = FALSE;
2809
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002810 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812
Bram Moolenaar9c449722010-07-20 18:44:27 +02002813#ifdef FEAT_GUI
2814 else if ((int *)varp == &p_mh)
2815 {
2816 if (!p_mh)
2817 gui_mch_mousehide(FALSE);
2818 }
2819#endif
2820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002821 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002823 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002824# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002825 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002826 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2827 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002828 {
2829 curbuf->b_p_ma = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002830 return e_cannot_make_terminal_with_running_job_modifiable;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002831 }
2832# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002833 redraw_titles();
2834 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002835 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002837 {
2838 redraw_titles();
2839 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002840 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002841 else if ((int *)varp == &curbuf->b_p_fixeol)
2842 {
2843 redraw_titles();
2844 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002845 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002846 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002847 {
2848 redraw_titles();
2849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 else if ((int *)varp == &curbuf->b_p_bin)
2853 {
2854 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002855 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2860 {
2861 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2862 NULL, NULL, TRUE, curbuf);
2863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 else if ((int *)varp == &curbuf->b_p_swf)
2867 {
2868 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002869 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002871 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2872 // buf->b_p_swf
2873 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
2875
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002876 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 else if ((int *)varp == &p_terse)
2878 {
2879 char_u *p;
2880
2881 p = vim_strchr(p_shm, SHM_SEARCH);
2882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002883 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 if (p_terse && p == NULL)
2885 {
2886 STRCPY(IObuff, p_shm);
2887 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002888 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002890 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002892 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
2894
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002895 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 else if ((int *)varp == &p_paste)
2897 {
2898 paste_option_changed();
2899 }
2900
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002901 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 else if ((int *)varp == &p_im)
2903 {
2904 if (p_im)
2905 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002906 if ((State & MODE_INSERT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 need_start_insertmode = TRUE;
2908 stop_insert_mode = FALSE;
2909 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002911 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 {
2913 need_start_insertmode = FALSE;
2914 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002915 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 restart_edit = 0;
2918 }
2919 }
2920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002921 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 else if ((int *)varp == &p_ic && p_hls)
2923 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002924 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 }
2926
2927#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 else if ((int *)varp == &p_hls)
2930 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002931 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 }
2933#endif
2934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002935 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2936 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 else if ((int *)varp == &curwin->w_p_scb)
2938 {
2939 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002942 curwin->w_scbind_pos = curwin->w_topline;
2943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
Bram Moolenaar4033c552017-09-16 20:54:51 +02002946#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002947 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 else if ((int *)varp == &curwin->w_p_pvw)
2949 {
2950 if (curwin->w_p_pvw)
2951 {
2952 win_T *win;
2953
Bram Moolenaar29323592016-07-24 22:04:11 +02002954 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 if (win->w_p_pvw && win != curwin)
2956 {
2957 curwin->w_p_pvw = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002958 return e_preview_window_already_exists;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 }
2960 }
2961 }
2962#endif
2963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002964 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 else if ((int *)varp == &curbuf->b_p_tx)
2966 {
2967 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2968 }
2969
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002970 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 set_string_option_direct((char_u *)"ffs", -1,
2974 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002975 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977
2978 /*
2979 * When 'lisp' option changes include/exclude '-' in
2980 * keyword characters.
2981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2983 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002984 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002987 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002988 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002990 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992
2993 else if ((int *)varp == &curbuf->b_changed)
2994 {
2995 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002996 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002997 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 }
3000
3001#ifdef BACKSLASH_IN_FILENAME
3002 else if ((int *)varp == &p_ssl)
3003 {
3004 if (p_ssl)
3005 {
3006 psepc = '/';
3007 psepcN = '\\';
3008 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 }
3010 else
3011 {
3012 psepc = '\\';
3013 psepcN = '/';
3014 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 }
3016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003017 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 buflist_slash_adjust();
3019 alist_slash_adjust();
3020# ifdef FEAT_EVAL
3021 scriptnames_slash_adjust();
3022# endif
3023 }
3024#endif
3025
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003026 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 else if ((int *)varp == &curwin->w_p_wrap)
3028 {
3029 if (curwin->w_p_wrap)
3030 curwin->w_leftcol = 0;
3031 }
3032
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 else if ((int *)varp == &p_ea)
3034 {
3035 if (p_ea && !old_value)
3036 win_equal(curwin, FALSE, 0);
3037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
3039 else if ((int *)varp == &p_wiv)
3040 {
3041 /*
3042 * When 'weirdinvert' changed, set/reset 't_xs'.
3043 * Then set 'weirdinvert' according to value of 't_xs'.
3044 */
3045 if (p_wiv && !old_value)
3046 T_XS = (char_u *)"y";
3047 else if (!p_wiv && old_value)
3048 T_XS = empty_option;
3049 p_wiv = (*T_XS != NUL);
3050 }
3051
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003052#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 else if ((int *)varp == &p_beval)
3054 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003055 if (!balloonEvalForTerm)
3056 {
3057 if (p_beval && !old_value)
3058 gui_mch_enable_beval_area(balloonEval);
3059 else if (!p_beval && old_value)
3060 gui_mch_disable_beval_area(balloonEval);
3061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003063#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003064#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003065 else if ((int *)varp == &p_bevalterm)
3066 {
3067 mch_bevalterm_changed();
3068 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003071#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 else if ((int *)varp == &p_acd)
3073 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003074 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003075 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 }
3077#endif
3078
3079#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003080 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 else if ((int *)varp == &curwin->w_p_diff)
3082 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003083 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003084 diff_buf_adjust(curwin);
3085# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 if (foldmethodIsDiff(curwin))
3087 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 }
3090#endif
3091
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003092#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003093 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 else if ((int *)varp == &p_imdisable)
3095 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (p_imdisable)
3098 im_set_active(FALSE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003099 else if (State & MODE_INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003100 // When the option is set from an autocommand, it may need to take
3101 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003102 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
3104#endif
3105
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003106#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003107 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003108 else if ((int *)varp == &curwin->w_p_spell)
3109 {
3110 if (curwin->w_p_spell)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003111 errmsg = did_set_spelllang(curwin);
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003112 }
3113#endif
3114
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#ifdef FEAT_ARABIC
3116 if ((int *)varp == &curwin->w_p_arab)
3117 {
3118 if (curwin->w_p_arab)
3119 {
3120 /*
3121 * 'arabic' is set, handle various sub-settings.
3122 */
3123 if (!p_tbidi)
3124 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003125 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (!curwin->w_p_rl)
3127 {
3128 curwin->w_p_rl = TRUE;
3129 changed_window_setting();
3130 }
3131
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003132 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 if (!p_arshape)
3134 {
3135 p_arshape = TRUE;
3136 redraw_later_clear();
3137 }
3138 }
3139
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003140 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003141 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003143 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003144 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3145
Bram Moolenaar8820b482017-03-16 17:23:31 +01003146 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003147 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003148#ifdef FEAT_EVAL
3149 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3150#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003153 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
3156# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003157 // Force-set the necessary keymap for arabic
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003158 errmsg = set_option_value((char_u *)"keymap",
3159 0L, (char_u *)"arabic", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 }
3162 else
3163 {
3164 /*
3165 * 'arabic' is reset, handle various sub-settings.
3166 */
3167 if (!p_tbidi)
3168 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003169 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 if (curwin->w_p_rl)
3171 {
3172 curwin->w_p_rl = FALSE;
3173 changed_window_setting();
3174 }
3175
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003176 // 'arabicshape' isn't reset, it is a global option and
3177 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 }
3179
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003180 // 'delcombine' isn't reset, it is a global option and another
3181 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
3183# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003184 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 curbuf->b_p_iminsert = B_IMODE_NONE;
3186 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3187# endif
3188 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003189 }
3190
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003191#endif
3192
Bram Moolenaar44826212019-08-22 21:23:20 +02003193#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3194 else if (((int *)varp == &curwin->w_p_nu
3195 || (int *)varp == &curwin->w_p_rnu)
3196 && gui.in_use
3197 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3198 && curbuf->b_signlist != NULL)
3199 {
3200 // If the 'number' or 'relativenumber' options are modified and
3201 // 'signcolumn' is set to 'number', then clear the screen for a full
3202 // refresh. Otherwise the sign icons are not displayed properly in the
3203 // number column. If the 'number' option is set and only the
3204 // 'relativenumber' option is toggled, then don't refresh the screen
3205 // (optimization).
3206 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003207 redraw_all_later(UPD_CLEAR);
Bram Moolenaar44826212019-08-22 21:23:20 +02003208 }
3209#endif
3210
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003211#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003212 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003213 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003214 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003215# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003216 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003217 if (
3218# ifdef VIMDLL
3219 !gui.in_use && !gui.starting &&
3220# endif
3221 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003222 {
3223 p_tgc = 0;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003224 return e_24_bit_colors_are_not_supported_on_this_environment;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003225 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003226 if (is_term_win32())
3227 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003228# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003229# ifdef FEAT_GUI
3230 if (!gui.in_use && !gui.starting)
3231# endif
3232 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003233# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003234 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003235 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003236 {
3237 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003238 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003239 init_highlight(TRUE, FALSE);
3240 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003241# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003242# ifdef FEAT_TERMINAL
3243 term_update_colors_all();
LemonBoyb2b3acb2022-05-20 10:10:34 +01003244 term_update_palette_all();
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003245 term_update_wincolor_all();
3246# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003247 }
3248#endif
3249
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 /*
3251 * End of handling side effects for bool options.
3252 */
3253
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003254 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003255
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 options[opt_idx].flags |= P_WAS_SET;
3257
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003258#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003259 apply_optionset_autocmd(opt_idx, opt_flags,
3260 (long)(old_value ? TRUE : FALSE),
3261 (long)(old_global_value ? TRUE : FALSE),
3262 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003263#endif
3264
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003265 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003266 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003267 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003268 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003269
3270 if ((opt_flags & OPT_NO_REDRAW) == 0)
3271 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003273 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274}
3275
3276/*
3277 * Set the value of a number option, and take care of side effects.
3278 * Returns NULL for success, or an error message for an error.
3279 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003280 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003281set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003282 int opt_idx, // index in options[] table
3283 char_u *varp, // pointer to the option variable
3284 long value, // new value
3285 char *errbuf, // buffer for error messages
3286 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003287 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3288 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003290 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003292#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003293 long old_global_value = 0; // only used when setting a local and
3294 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003295#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003296 long old_Rows = Rows; // remember old Rows
3297 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 long *pp = (long *)varp;
3299
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003300 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003301 if ((secure
3302#ifdef HAVE_SANDBOX
3303 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003305 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003306 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003308#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003309 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003310 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003311 // value (since there is only one value).
3312 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003313 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3314 OPT_GLOBAL);
3315#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003316
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 *pp = value;
3318#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003319 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003320 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003322#ifdef FEAT_GUI
3323 need_mouse_correct = TRUE;
3324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar14f24742012-08-08 18:01:05 +02003326 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003328 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003329#ifdef FEAT_VARTABS
3330 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3331 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003332 ? tabstop_first(curbuf->b_p_vts_array)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003333 : curbuf->b_p_ts;
3334#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338
3339 /*
3340 * Number options that need some action when changed
3341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 if (pp == &p_wh || pp == &p_hh)
3343 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003344 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (p_wh < 1)
3346 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003347 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 p_wh = 1;
3349 }
3350 if (p_wmh > p_wh)
3351 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003352 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 p_wh = p_wmh;
3354 }
3355 if (p_hh < 0)
3356 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003357 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 p_hh = 0;
3359 }
3360
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003361 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003362 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
3364 if (pp == &p_wh && curwin->w_height < p_wh)
3365 win_setheight((int)p_wh);
3366 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3367 win_setheight((int)p_hh);
3368 }
3369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 else if (pp == &p_wmh)
3371 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003372 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 if (p_wmh < 0)
3374 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003375 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 p_wmh = 0;
3377 }
3378 if (p_wmh > p_wh)
3379 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003380 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 p_wmh = p_wh;
3382 }
3383 win_setminheight();
3384 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003385 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003387 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 if (p_wiw < 1)
3389 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003390 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 p_wiw = 1;
3392 }
3393 if (p_wmw > p_wiw)
3394 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003395 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 p_wiw = p_wmw;
3397 }
3398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003399 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003400 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 win_setwidth((int)p_wiw);
3402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 else if (pp == &p_wmw)
3404 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003405 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 if (p_wmw < 0)
3407 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003408 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 p_wmw = 0;
3410 }
3411 if (p_wmw > p_wiw)
3412 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003413 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 p_wmw = p_wiw;
3415 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003416 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003419 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 else if (pp == &p_ls)
3421 {
3422 last_status(FALSE);
3423 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003425 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003426 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003427 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003428 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431#ifdef FEAT_GUI
3432 else if (pp == &p_linespace)
3433 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003434 // Recompute gui.char_height and resize the Vim window to keep the
3435 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003436 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003437 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439#endif
3440
3441#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003442 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 else if (pp == &curwin->w_p_fdl)
3444 {
3445 if (curwin->w_p_fdl < 0)
3446 curwin->w_p_fdl = 0;
3447 newFoldLevel();
3448 }
3449
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003450 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 else if (pp == &curwin->w_p_fml)
3452 {
3453 foldUpdateAll(curwin);
3454 }
3455
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003456 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 else if (pp == &curwin->w_p_fdn)
3458 {
3459 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3460 foldUpdateAll(curwin);
3461 }
3462
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003463 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 else if (pp == &curwin->w_p_fdc)
3465 {
3466 if (curwin->w_p_fdc < 0)
3467 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003468 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 curwin->w_p_fdc = 0;
3470 }
3471 else if (curwin->w_p_fdc > 12)
3472 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003473 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 curwin->w_p_fdc = 12;
3475 }
3476 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003477#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003479 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3481 {
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003482#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (foldmethodIsIndent(curwin))
3484 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003485#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003486 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3487 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003488 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3489 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003492 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003493 else if (pp == &p_mco)
3494 {
3495 if (p_mco > MAX_MCO)
3496 p_mco = MAX_MCO;
3497 else if (p_mco < 0)
3498 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003499 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003500 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003501
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 else if (pp == &curbuf->b_p_iminsert)
3503 {
3504 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3505 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003506 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 curbuf->b_p_iminsert = B_IMODE_NONE;
3508 }
3509 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003510 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003512#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003513 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 status_redraw_curbuf();
3515#endif
3516 }
3517
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003518#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003519 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003520 else if (pp == &p_imst)
3521 {
3522 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003523 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003524 }
3525#endif
3526
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003527 else if (pp == &p_window)
3528 {
3529 if (p_window < 1)
3530 p_window = 1;
3531 else if (p_window >= Rows)
3532 p_window = Rows - 1;
3533 }
3534
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 else if (pp == &curbuf->b_p_imsearch)
3536 {
3537 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3538 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003539 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 curbuf->b_p_imsearch = B_IMODE_NONE;
3541 }
3542 p_imsearch = curbuf->b_p_imsearch;
3543 }
3544
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003545 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 else if (pp == &p_titlelen)
3547 {
3548 if (p_titlelen < 0)
3549 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003550 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 p_titlelen = 85;
3552 }
3553 if (starting != NO_SCREEN && old_value != p_titlelen)
3554 need_maketitle = TRUE;
3555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003557 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 else if (pp == &p_ch)
3559 {
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01003560 if (p_ch < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003562 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 p_ch = 1;
3564 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003565 if (p_ch > Rows - min_rows() + 1)
3566 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003568 // Only compute the new window layout when startup has been
3569 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 if (p_ch != old_value && full_screen
3571#ifdef FEAT_GUI
3572 && !gui.starting
3573#endif
3574 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003575 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003578 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 else if (pp == &p_uc)
3580 {
3581 if (p_uc < 0)
3582 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003583 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 p_uc = 100;
3585 }
3586 if (p_uc && !old_value)
3587 ml_open_files();
3588 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003590 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003591 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003592 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003593 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003594 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003595 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003596 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003597 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003598 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003599 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003600 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003601 }
3602 }
3603#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003604#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003605 else if (pp == &p_mzq)
3606 mzvim_reset_timer();
3607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003609#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003610 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003611 else if (pp == &p_pyx)
3612 {
3613 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003614 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003615 }
3616#endif
3617
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003618 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 else if (pp == &p_ul)
3620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003621 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003623 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 p_ul = value;
3625 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003626 else if (pp == &curbuf->b_p_ul)
3627 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003628 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003629 curbuf->b_p_ul = old_value;
3630 u_sync(TRUE);
3631 curbuf->b_p_ul = value;
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003634#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003635 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003636 else if (pp == &curwin->w_p_nuw)
3637 {
3638 if (curwin->w_p_nuw < 1)
3639 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003640 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003641 curwin->w_p_nuw = 1;
3642 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003643 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003644 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003645 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003646 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003647 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003648 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003649 }
3650#endif
3651
Bram Moolenaar1a384422010-07-14 19:53:30 +02003652 else if (pp == &curbuf->b_p_tw)
3653 {
3654 if (curbuf->b_p_tw < 0)
3655 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003656 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003657 curbuf->b_p_tw = 0;
3658 }
3659#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003660 {
3661 win_T *wp;
3662 tabpage_T *tp;
3663
3664 FOR_ALL_TAB_WINDOWS(tp, wp)
3665 check_colorcolumn(wp);
3666 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003667#endif
3668 }
3669
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 /*
3671 * Check the bounds for numeric options here
3672 */
3673 if (Rows < min_rows() && full_screen)
3674 {
3675 if (errbuf != NULL)
3676 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003677 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003678 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 errmsg = errbuf;
3680 }
3681 Rows = min_rows();
3682 }
3683 if (Columns < MIN_COLUMNS && full_screen)
3684 {
3685 if (errbuf != NULL)
3686 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003687 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003688 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 errmsg = errbuf;
3690 }
3691 Columns = MIN_COLUMNS;
3692 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003693 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 /*
3696 * If the screen (shell) height has been changed, assume it is the
3697 * physical screenheight.
3698 */
3699 if (old_Rows != Rows || old_Columns != Columns)
3700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003701 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 if (updating_screen)
3703 *pp = old_value;
3704 else if (full_screen
3705#ifdef FEAT_GUI
3706 && !gui.starting
3707#endif
3708 )
3709 set_shellsize((int)Columns, (int)Rows, TRUE);
3710 else
3711 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003712 // Postpone the resizing; check the size and cmdline position for
3713 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 check_shellsize();
3715 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3716 cmdline_row = Rows - p_ch;
3717 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003718 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003719 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 if (curbuf->b_p_ts <= 0)
3723 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003724 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 curbuf->b_p_ts = 8;
3726 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003727 else if (curbuf->b_p_ts > TABSTOP_MAX)
3728 {
3729 errmsg = e_invalid_argument;
3730 curbuf->b_p_ts = 8;
3731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 if (p_tm < 0)
3733 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003734 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 p_tm = 0;
3736 }
3737 if ((curwin->w_p_scr <= 0
3738 || (curwin->w_p_scr > curwin->w_height
3739 && curwin->w_height > 0))
3740 && full_screen)
3741 {
3742 if (pp == &(curwin->w_p_scr))
3743 {
3744 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003745 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 win_comp_scroll(curwin);
3747 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003748 // If 'scroll' became invalid because of a side effect silently adjust
3749 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 else if (curwin->w_p_scr <= 0)
3751 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003752 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 curwin->w_p_scr = curwin->w_height;
3754 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003755 if (p_hi < 0)
3756 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003757 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003758 p_hi = 0;
3759 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003760 else if (p_hi > 10000)
3761 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003762 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003763 p_hi = 10000;
3764 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003765 if (p_re < 0 || p_re > 2)
3766 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003767 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003768 p_re = 0;
3769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 if (p_report < 0)
3771 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003772 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 p_report = 1;
3774 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003775 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003777 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 p_sj = Rows / 2;
3779 else
3780 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003781 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 p_sj = 1;
3783 }
3784 }
3785 if (p_so < 0 && full_screen)
3786 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003787 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 p_so = 0;
3789 }
3790 if (p_siso < 0 && full_screen)
3791 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003792 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 p_siso = 0;
3794 }
3795#ifdef FEAT_CMDWIN
3796 if (p_cwh < 1)
3797 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003798 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 p_cwh = 1;
3800 }
3801#endif
3802 if (p_ut < 0)
3803 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003804 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 p_ut = 2000;
3806 }
3807 if (p_ss < 0)
3808 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003809 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 p_ss = 0;
3811 }
3812
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003813 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3815 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3816
3817 options[opt_idx].flags |= P_WAS_SET;
3818
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003819#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003820 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3821 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003822#endif
3823
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003824 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003825 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003826 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003827 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003828 if ((opt_flags & OPT_NO_REDRAW) == 0)
3829 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
3831 return errmsg;
3832}
3833
3834/*
3835 * Called after an option changed: check if something needs to be redrawn.
3836 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003838check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003840 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003841 int doclear = (flags & P_RCLR) == P_RCLR;
3842 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003844 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846
3847 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3848 changed_window_setting();
3849 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003850 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003851 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003852 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003853 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003854 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003856 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857}
3858
3859/*
3860 * Find index for option 'arg'.
3861 * Return -1 if not found.
3862 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003863 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003864findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
3866 int opt_idx;
3867 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003868 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 int is_term_opt;
3870
3871 /*
3872 * For first call: Initialize the quick-access table.
3873 * It contains the index for the first option that starts with a certain
3874 * letter. There are 26 letters, plus the first "t_" option.
3875 */
3876 if (quick_tab[1] == 0)
3877 {
3878 p = options[0].fullname;
3879 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3880 {
3881 if (s[0] != p[0])
3882 {
3883 if (s[0] == 't' && s[1] == '_')
3884 quick_tab[26] = opt_idx;
3885 else
3886 quick_tab[CharOrdLow(s[0])] = opt_idx;
3887 }
3888 p = s;
3889 }
3890 }
3891
3892 /*
3893 * Check for name starting with an illegal character.
3894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 return -1;
3897
3898 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3899 if (is_term_opt)
3900 opt_idx = quick_tab[26];
3901 else
3902 opt_idx = quick_tab[CharOrdLow(arg[0])];
3903 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3904 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003905 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 break;
3907 }
3908 if (s == NULL && !is_term_opt)
3909 {
3910 opt_idx = quick_tab[CharOrdLow(arg[0])];
3911 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3912 {
3913 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003914 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 break;
3916 s = NULL;
3917 }
3918 }
3919 if (s == NULL)
3920 opt_idx = -1;
3921 return opt_idx;
3922}
3923
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003924#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925/*
3926 * Get the value for an option.
3927 *
3928 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003929 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003930 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003931 * String option: gov_string, *stringval gets allocated string.
3932 * Hidden Number option: gov_hidden_number.
3933 * Hidden Toggle option: gov_hidden_bool.
3934 * Hidden String option: gov_hidden_string.
3935 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003936 *
3937 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003939 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003940get_option_value(
3941 char_u *name,
3942 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003943 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003944 int *flagsp,
3945 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946{
3947 int opt_idx;
3948 char_u *varp;
3949
3950 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003951 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003952 {
3953 int key;
3954
3955 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003956 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003957 {
3958 char_u key_name[2];
3959 char_u *p;
3960
Bram Moolenaar10c75c42021-12-28 20:53:30 +00003961 if (flagsp != NULL)
3962 *flagsp = 0; // terminal option has no flags
3963
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003964 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003965 if (key < 0)
3966 {
3967 key_name[0] = KEY2TERMCAP0(key);
3968 key_name[1] = KEY2TERMCAP1(key);
3969 }
3970 else
3971 {
3972 key_name[0] = KS_KEY;
3973 key_name[1] = (key & 0xff);
3974 }
3975 p = find_termcode(key_name);
3976 if (p != NULL)
3977 {
3978 if (stringval != NULL)
3979 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003980 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003981 }
3982 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003983 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003986 varp = get_varp_scope(&(options[opt_idx]), scope);
3987
3988 if (flagsp != NULL)
3989 // Return the P_xxxx option flags.
3990 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
3992 if (options[opt_idx].flags & P_STRING)
3993 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003994 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003995 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (stringval != NULL)
3997 {
zeertzjq51f0bc32022-05-09 13:33:39 +01003998 if ((char_u **)varp == &p_pt) // 'pastetoggle'
3999 *stringval = str2special_save(*(char_u **)(varp), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004001 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004002 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004003 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004006 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 *stringval = vim_strsave(*(char_u **)(varp));
4008 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004009 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 }
4011
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004012 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004013 return (options[opt_idx].flags & P_NUM)
4014 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 if (options[opt_idx].flags & P_NUM)
4016 *numval = *(long *)varp;
4017 else
4018 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004019 // Special case: 'modified' is b_changed, but we also want to consider
4020 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 if ((int *)varp == &curbuf->b_changed)
4022 *numval = curbufIsChanged();
4023 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004024 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004026 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027}
4028#endif
4029
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004030#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004031/*
4032 * Returns the option attributes and its value. Unlike the above function it
4033 * will return either global value or local value of the option depending on
4034 * what was requested, but it will never return global value if it was
4035 * requested to return local one and vice versa. Neither it will return
4036 * buffer-local value if it was requested to return window-local one.
4037 *
4038 * Pretends that option is absent if it is not present in the requested scope
4039 * (i.e. has no global, window-local or buffer-local value depending on
4040 * opt_type). Uses
4041 *
4042 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004043 * 0 hidden or unknown option, also option that does not have requested
4044 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004045 * see SOPT_* in vim.h for other flags
4046 *
4047 * Possible opt_type values: see SREQ_* in vim.h
4048 */
4049 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004050get_option_value_strict(
4051 char_u *name,
4052 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004053 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004054 int opt_type,
4055 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004056{
4057 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004058 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004059 struct vimoption *p;
4060 int r = 0;
4061
4062 opt_idx = findoption(name);
4063 if (opt_idx < 0)
4064 return 0;
4065
4066 p = &(options[opt_idx]);
4067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004068 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004069 if (p->var == NULL)
4070 return 0;
4071
4072 if (p->flags & P_BOOL)
4073 r |= SOPT_BOOL;
4074 else if (p->flags & P_NUM)
4075 r |= SOPT_NUM;
4076 else if (p->flags & P_STRING)
4077 r |= SOPT_STRING;
4078
4079 if (p->indir == PV_NONE)
4080 {
4081 if (opt_type == SREQ_GLOBAL)
4082 r |= SOPT_GLOBAL;
4083 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004084 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004085 }
4086 else
4087 {
4088 if (p->indir & PV_BOTH)
4089 r |= SOPT_GLOBAL;
4090 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004091 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004092
4093 if (p->indir & PV_WIN)
4094 {
4095 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004096 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004097 else
4098 r |= SOPT_WIN;
4099 }
4100 else if (p->indir & PV_BUF)
4101 {
4102 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004103 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004104 else
4105 r |= SOPT_BUF;
4106 }
4107 }
4108
4109 if (stringval == NULL)
4110 return r;
4111
4112 if (opt_type == SREQ_GLOBAL)
4113 varp = p->var;
4114 else
4115 {
4116 if (opt_type == SREQ_BUF)
4117 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004118 // Special case: 'modified' is b_changed, but we also want to
4119 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004120 if (p->indir == PV_MOD)
4121 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004122 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004123 varp = NULL;
4124 }
4125#ifdef FEAT_CRYPT
4126 else if (p->indir == PV_KEY)
4127 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004128 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004129 *stringval = NULL;
4130 varp = NULL;
4131 }
4132#endif
4133 else
4134 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004135 buf_T *save_curbuf = curbuf;
4136
4137 // only getting a pointer, no need to use aucmd_prepbuf()
4138 curbuf = (buf_T *)from;
4139 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004140 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004141 curbuf = save_curbuf;
4142 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004143 }
4144 }
4145 else if (opt_type == SREQ_WIN)
4146 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004147 win_T *save_curwin = curwin;
4148
4149 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004150 curbuf = curwin->w_buffer;
4151 varp = get_varp(p);
4152 curwin = save_curwin;
4153 curbuf = curwin->w_buffer;
4154 }
4155 if (varp == p->var)
4156 return (r | SOPT_UNSET);
4157 }
4158
4159 if (varp != NULL)
4160 {
4161 if (p->flags & P_STRING)
4162 *stringval = vim_strsave(*(char_u **)(varp));
4163 else if (p->flags & P_NUM)
4164 *numval = *(long *) varp;
4165 else
4166 *numval = *(int *)varp;
4167 }
4168
4169 return r;
4170}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004171
4172/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004173 * Iterate over options. First argument is a pointer to a pointer to a
4174 * structure inside options[] array, second is option type like in the above
4175 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004176 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004177 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004178 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004179 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004180 * first argument to NULL.
4181 *
4182 * Returns full option name for current option on each call.
4183 */
4184 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004185option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004186{
4187 struct vimoption *ret = NULL;
4188 do
4189 {
4190 if (*option == NULL)
4191 *option = (void *) options;
4192 else if (((struct vimoption *) (*option))->fullname == NULL)
4193 {
4194 *option = NULL;
4195 return NULL;
4196 }
4197 else
4198 *option = (void *) (((struct vimoption *) (*option)) + 1);
4199
4200 ret = ((struct vimoption *) (*option));
4201
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004202 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004203 if (ret->var == NULL)
4204 {
4205 ret = NULL;
4206 continue;
4207 }
4208
4209 switch (opt_type)
4210 {
4211 case SREQ_GLOBAL:
4212 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4213 ret = NULL;
4214 break;
4215 case SREQ_BUF:
4216 if (!(ret->indir & PV_BUF))
4217 ret = NULL;
4218 break;
4219 case SREQ_WIN:
4220 if (!(ret->indir & PV_WIN))
4221 ret = NULL;
4222 break;
4223 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004224 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004225 return NULL;
4226 }
4227 }
4228 while (ret == NULL);
4229
4230 return (char_u *)ret->fullname;
4231}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004232#endif
4233
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004235 * Return the flags for the option at 'opt_idx'.
4236 */
4237 long_u
4238get_option_flags(int opt_idx)
4239{
4240 return options[opt_idx].flags;
4241}
4242
4243/*
4244 * Set a flag for the option at 'opt_idx'.
4245 */
4246 void
4247set_option_flag(int opt_idx, long_u flag)
4248{
4249 options[opt_idx].flags |= flag;
4250}
4251
4252/*
4253 * Clear a flag for the option at 'opt_idx'.
4254 */
4255 void
4256clear_option_flag(int opt_idx, long_u flag)
4257{
4258 options[opt_idx].flags &= ~flag;
4259}
4260
4261/*
4262 * Returns TRUE if the option at 'opt_idx' is a global option
4263 */
4264 int
4265is_global_option(int opt_idx)
4266{
4267 return options[opt_idx].indir == PV_NONE;
4268}
4269
4270/*
4271 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4272 * local value.
4273 */
4274 int
4275is_global_local_option(int opt_idx)
4276{
4277 return options[opt_idx].indir & PV_BOTH;
4278}
4279
4280/*
4281 * Returns TRUE if the option at 'opt_idx' is a window-local option
4282 */
4283 int
4284is_window_local_option(int opt_idx)
4285{
4286 return options[opt_idx].var == VAR_WIN;
4287}
4288
4289/*
4290 * Returns TRUE if the option at 'opt_idx' is a hidden option
4291 */
4292 int
4293is_hidden_option(int opt_idx)
4294{
4295 return options[opt_idx].var == NULL;
4296}
4297
4298#if defined(FEAT_CRYPT) || defined(PROTO)
4299/*
4300 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4301 */
4302 int
4303is_crypt_key_option(int opt_idx)
4304{
4305 return options[opt_idx].indir == PV_KEY;
4306}
4307#endif
4308
4309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 * Set the value of option "name".
4311 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004312 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004313 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004315 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004316set_option_value(
4317 char_u *name,
4318 long number,
4319 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004320 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321{
4322 int opt_idx;
4323 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004324 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
4326 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004327 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004328 {
4329 int key;
4330
4331 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004332 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004333 {
4334 char_u key_name[2];
4335
4336 if (key < 0)
4337 {
4338 key_name[0] = KEY2TERMCAP0(key);
4339 key_name[1] = KEY2TERMCAP1(key);
4340 }
4341 else
4342 {
4343 key_name[0] = KS_KEY;
4344 key_name[1] = (key & 0xff);
4345 }
4346 add_termcode(key_name, string, FALSE);
4347 if (full_screen)
4348 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004349 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01004350 return NULL;
4351 }
4352
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004353 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 else
4356 {
4357 flags = options[opt_idx].flags;
4358#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004359 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004361 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004362 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004363 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004366 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004367 return set_string_option(opt_idx, string, opt_flags);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004368
4369 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
4370 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004372 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004374 int idx;
4375
4376 // Either we are given a string or we are setting option
4377 // to zero.
4378 for (idx = 0; string[idx] == '0'; ++idx)
4379 ;
4380 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004381 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004382 // There's another character after zeros or the string
4383 // is empty. In both cases, we are trying to set a
4384 // num option using a string.
4385 semsg(_(e_number_required_after_str_equal_str),
4386 name, string);
4387 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004388
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004391 if (flags & P_NUM)
4392 return set_num_option(opt_idx, varp, number,
4393 NULL, 0, opt_flags);
4394 else
4395 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004397
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004399 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400}
4401
4402/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004403 * Call set_option_value() and when an error is returned report it.
4404 */
4405 void
4406set_option_value_give_err(
4407 char_u *name,
4408 long number,
4409 char_u *string,
4410 int opt_flags) // OPT_LOCAL or 0 (both)
4411{
4412 char *errmsg = set_option_value(name, number, string, opt_flags);
4413
4414 if (errmsg != NULL)
4415 emsg(_(errmsg));
4416}
4417
4418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 * Get the terminal code for a terminal option.
4420 * Returns NULL when not found.
4421 */
4422 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004423get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424{
4425 int opt_idx;
4426 char_u *varp;
4427
4428 if (tname[0] != 't' || tname[1] != '_' ||
4429 tname[2] == NUL || tname[3] == NUL)
4430 return NULL;
4431 if ((opt_idx = findoption(tname)) >= 0)
4432 {
4433 varp = get_varp(&(options[opt_idx]));
4434 if (varp != NULL)
4435 varp = *(char_u **)(varp);
4436 return varp;
4437 }
4438 return find_termcode(tname + 2);
4439}
4440
4441 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004442get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
4444 int i;
4445
4446 i = findoption((char_u *)"hl");
4447 if (i >= 0)
4448 return options[i].def_val[VI_DEFAULT];
4449 return (char_u *)NULL;
4450}
4451
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004452 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004453get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004454{
4455 int i;
4456
4457 i = findoption((char_u *)"enc");
4458 if (i >= 0)
4459 return options[i].def_val[VI_DEFAULT];
4460 return (char_u *)NULL;
4461}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004462
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004463#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004464 int
4465is_option_allocated(char *name)
4466{
4467 int idx = findoption((char_u *)name);
4468
4469 return idx >= 0 && (options[idx].flags & P_ALLOCED);
4470}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004471#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004472
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004473#if defined(FEAT_EVAL) || defined(PROTO)
4474/*
4475 * Return TRUE if "name" is a string option.
4476 * Returns FALSE if option "name" does not exist.
4477 */
4478 int
4479is_string_option(char_u *name)
4480{
4481 int idx = findoption(name);
4482
4483 return idx >= 0 && (options[idx].flags & P_STRING);
4484}
4485#endif
4486
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487/*
4488 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004489 * When "has_lt" is true there is a '<' before "*arg_arg".
4490 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 */
4492 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004493find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004495 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004497 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
4499 /*
4500 * Don't use get_special_key_code() for t_xx, we don't want it to call
4501 * add_termcap_entry().
4502 */
4503 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4504 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004505 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004507 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004509 key = find_special_key(&arg, &modifiers,
4510 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004511 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 key = 0;
4513 }
4514 return key;
4515}
4516
4517/*
4518 * if 'all' == 0: show changed options
4519 * if 'all' == 1: show all normal options
4520 * if 'all' == 2: show all terminal options
4521 */
4522 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004523showoptions(
4524 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004525 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526{
4527 struct vimoption *p;
4528 int col;
4529 int isterm;
4530 char_u *varp;
4531 struct vimoption **items;
4532 int item_count;
4533 int run;
4534 int row, rows;
4535 int cols;
4536 int i;
4537 int len;
4538
4539#define INC 20
4540#define GAP 3
4541
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004542 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (items == NULL)
4544 return;
4545
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004546 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004548 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004550 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004552 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004554 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
4556 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004557 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 * 1. display the short items
4559 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004560 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 */
4562 for (run = 1; run <= 2 && !got_int; ++run)
4563 {
4564 /*
4565 * collect the items in items[]
4566 */
4567 item_count = 0;
4568 for (p = &options[0]; p->fullname != NULL; p++)
4569 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004570 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004571 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004572 continue;
4573
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 varp = NULL;
4575 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004576 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 {
4578 if (p->indir != PV_NONE && !isterm)
4579 varp = get_varp_scope(p, opt_flags);
4580 }
4581 else
4582 varp = get_varp(p);
4583 if (varp != NULL
4584 && ((all == 2 && isterm)
4585 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004586 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004588 if (opt_flags & OPT_ONECOLUMN)
4589 len = Columns;
4590 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004591 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 else
4593 {
4594 option_value2string(p, opt_flags);
4595 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4596 }
4597 if ((len <= INC - GAP && run == 1) ||
4598 (len > INC - GAP && run == 2))
4599 items[item_count++] = p;
4600 }
4601 }
4602
4603 /*
4604 * display the items
4605 */
4606 if (run == 1)
4607 {
4608 cols = (Columns + GAP - 3) / INC;
4609 if (cols == 0)
4610 cols = 1;
4611 rows = (item_count + cols - 1) / cols;
4612 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004613 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 rows = item_count;
4615 for (row = 0; row < rows && !got_int; ++row)
4616 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004617 msg_putchar('\n'); // go to next line
4618 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 break;
4620 col = 0;
4621 for (i = row; i < item_count; i += rows)
4622 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004623 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 showoneopt(items[i], opt_flags);
4625 col += INC;
4626 }
4627 out_flush();
4628 ui_breakcheck();
4629 }
4630 }
4631 vim_free(items);
4632}
4633
4634/*
4635 * Return TRUE if option "p" has its default value.
4636 */
4637 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004638optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639{
4640 int dvi;
4641
4642 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004643 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004644 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004646 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004648 // the cast to long is required for Manx C, long_i is
4649 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004650 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004651 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4653}
4654
4655/*
4656 * showoneopt: show the value of one option
4657 * must not be called with a hidden option!
4658 */
4659 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004660showoneopt(
4661 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004662 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004664 char_u *varp;
4665 int save_silent = silent_mode;
4666
4667 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004668 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
4670 varp = get_varp_scope(p, opt_flags);
4671
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004672 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4674 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004675 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004677 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004679 msg_puts(" ");
4680 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 if (!(p->flags & P_BOOL))
4682 {
4683 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004684 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 option_value2string(p, opt_flags);
4686 msg_outtrans(NameBuff);
4687 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004688
4689 silent_mode = save_silent;
4690 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691}
4692
4693/*
4694 * Write modified options as ":set" commands to a file.
4695 *
4696 * There are three values for "opt_flags":
4697 * OPT_GLOBAL: Write global option values and fresh values of
4698 * buffer-local options (used for start of a session
4699 * file).
4700 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4701 * curwin (used for a vimrc file).
4702 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4703 * and local values for window-local options of
4704 * curwin. Local values are also written when at the
4705 * default value, because a modeline or autocommand
4706 * may have set them when doing ":edit file" and the
4707 * user has set them back at the default or fresh
4708 * value.
4709 * When "local_only" is TRUE, don't write fresh
4710 * values, only local values (for ":mkview").
4711 * (fresh value = value used for a new buffer or window for a local option).
4712 *
4713 * Return FAIL on error, OK otherwise.
4714 */
4715 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004716makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717{
4718 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004719 char_u *varp; // currently used value
4720 char_u *varp_fresh; // local value
4721 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 char *cmd;
4723 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004724 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
4726 /*
4727 * The options that don't have a default (terminal name, columns, lines)
4728 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004729 * Do the loop over "options[]" twice: once for options with the
4730 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004732 for (pri = 1; pri >= 0; --pri)
4733 {
4734 for (p = &options[0]; !istermoption(p); p++)
4735 if (!(p->flags & P_NO_MKRC)
4736 && !istermoption(p)
4737 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004739 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4741 continue;
4742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004743 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4744 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4746 continue;
4747
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004748 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004750 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 continue;
4752
Bram Moolenaard23b7142021-04-17 21:04:34 +02004753 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4754 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004755 continue;
4756
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 round = 2;
4758 if (p->indir != PV_NONE)
4759 {
4760 if (p->var == VAR_WIN)
4761 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004762 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 if (!(opt_flags & OPT_LOCAL))
4764 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004765 // When fresh value of window-local option is not at the
4766 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4768 {
4769 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004770 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 {
4772 round = 1;
4773 varp_local = varp;
4774 varp = varp_fresh;
4775 }
4776 }
4777 }
4778 }
4779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004780 // Round 1: fresh value for window-local options.
4781 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 for ( ; round <= 2; varp = varp_local, ++round)
4783 {
4784 if (round == 1 || (opt_flags & OPT_GLOBAL))
4785 cmd = "set";
4786 else
4787 cmd = "setlocal";
4788
4789 if (p->flags & P_BOOL)
4790 {
4791 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4792 return FAIL;
4793 }
4794 else if (p->flags & P_NUM)
4795 {
4796 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4797 return FAIL;
4798 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004799 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004801 int do_endif = FALSE;
4802
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004803 // Don't set 'syntax' and 'filetype' again if the value is
4804 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004805 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004806#if defined(FEAT_SYN_HL)
4807 p->indir == PV_SYN ||
4808#endif
4809 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
4811 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4812 *(char_u **)(varp)) < 0
4813 || put_eol(fd) < 0)
4814 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004815 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 }
4817 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004818 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004820 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 {
4822 if (put_line(fd, "endif") == FAIL)
4823 return FAIL;
4824 }
4825 }
4826 }
4827 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 return OK;
4830}
4831
4832#if defined(FEAT_FOLDING) || defined(PROTO)
4833/*
4834 * Generate set commands for the local fold options only. Used when
4835 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4836 */
4837 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004838makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004840 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004842 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 == FAIL
4844# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004845 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004847 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 == FAIL
4849 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4850 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4851 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4852 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4853 )
4854 return FAIL;
4855
4856 return OK;
4857}
4858#endif
4859
4860 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004861put_setstring(
4862 FILE *fd,
4863 char *cmd,
4864 char *name,
4865 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004866 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867{
4868 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004869 char_u *buf = NULL;
4870 char_u *part = NULL;
4871 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872
4873 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4874 return FAIL;
4875 if (*valuep != NULL)
4876 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004877 // Output 'pastetoggle' as key names. For other
4878 // options some characters have to be escaped with
4879 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 if (valuep == &p_pt)
4881 {
4882 s = *valuep;
4883 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004884 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 return FAIL;
4886 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004887 // expand the option value, replace $HOME by ~
4888 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004890 int size = (int)STRLEN(*valuep) + 1;
4891
4892 // replace home directory in the whole option value into "buf"
4893 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004894 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004895 goto fail;
4896 home_replace(NULL, *valuep, buf, size, FALSE);
4897
4898 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004899 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004900 // can be expanded when read back.
4901 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4902 && vim_strchr(*valuep, ',') != NULL)
4903 {
4904 part = alloc(size);
4905 if (part == NULL)
4906 goto fail;
4907
4908 // write line break to clear the option, e.g. ':set rtp='
4909 if (put_eol(fd) == FAIL)
4910 goto fail;
4911
4912 p = buf;
4913 while (*p != NUL)
4914 {
4915 // for each comma separated option part, append value to
4916 // the option, :set rtp+=value
4917 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4918 goto fail;
4919 (void)copy_option_part(&p, part, size, ",");
4920 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4921 goto fail;
4922 }
4923 vim_free(buf);
4924 vim_free(part);
4925 return OK;
4926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004928 {
4929 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004931 }
4932 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 else if (put_escstr(fd, *valuep, 2) == FAIL)
4935 return FAIL;
4936 }
4937 if (put_eol(fd) < 0)
4938 return FAIL;
4939 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004940fail:
4941 vim_free(buf);
4942 vim_free(part);
4943 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944}
4945
4946 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004947put_setnum(
4948 FILE *fd,
4949 char *cmd,
4950 char *name,
4951 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
4953 long wc;
4954
4955 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4956 return FAIL;
4957 if (wc_use_keyname((char_u *)valuep, &wc))
4958 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004959 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4961 return FAIL;
4962 }
4963 else if (fprintf(fd, "%ld", *valuep) < 0)
4964 return FAIL;
4965 if (put_eol(fd) < 0)
4966 return FAIL;
4967 return OK;
4968}
4969
4970 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004971put_setbool(
4972 FILE *fd,
4973 char *cmd,
4974 char *name,
4975 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004977 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004978 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4980 || put_eol(fd) < 0)
4981 return FAIL;
4982 return OK;
4983}
4984
4985/*
4986 * Clear all the terminal options.
4987 * If the option has been allocated, free the memory.
4988 * Terminal options are never hidden or indirect.
4989 */
4990 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004991clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 /*
4994 * Reset a few things before clearing the old options. This may cause
4995 * outputting a few things that the terminal doesn't understand, but the
4996 * screen will be cleared later, so this is OK.
4997 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004998 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004999 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005001 // When starting the GUI close the display opened for the clipboard.
5002 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 if (gui.starting)
5004 clear_xterm_clip();
5005#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005006 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005008 free_termoptions();
5009}
5010
5011 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005012free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005013{
5014 struct vimoption *p;
5015
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005016 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 if (istermoption(p))
5018 {
5019 if (p->flags & P_ALLOCED)
5020 free_string_option(*(char_u **)(p->var));
5021 if (p->flags & P_DEF_ALLOCED)
5022 free_string_option(p->def_val[VI_DEFAULT]);
5023 *(char_u **)(p->var) = empty_option;
5024 p->def_val[VI_DEFAULT] = empty_option;
5025 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005026#ifdef FEAT_EVAL
5027 // remember where the option was cleared
5028 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
5031 clear_termcodes();
5032}
5033
5034/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005035 * Free the string for one term option, if it was allocated.
5036 * Set the string to empty_option and clear allocated flag.
5037 * "var" points to the option value.
5038 */
5039 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005040free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005041{
5042 struct vimoption *p;
5043
5044 for (p = &options[0]; p->fullname != NULL; p++)
5045 if (p->var == var)
5046 {
5047 if (p->flags & P_ALLOCED)
5048 free_string_option(*(char_u **)(p->var));
5049 *(char_u **)(p->var) = empty_option;
5050 p->flags &= ~P_ALLOCED;
5051 break;
5052 }
5053}
5054
5055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 * Set the terminal option defaults to the current value.
5057 * Used after setting the terminal name.
5058 */
5059 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005060set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061{
5062 struct vimoption *p;
5063
5064 for (p = &options[0]; p->fullname != NULL; p++)
5065 {
5066 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5067 {
5068 if (p->flags & P_DEF_ALLOCED)
5069 {
5070 free_string_option(p->def_val[VI_DEFAULT]);
5071 p->flags &= ~P_DEF_ALLOCED;
5072 }
5073 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5074 if (p->flags & P_ALLOCED)
5075 {
5076 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005077 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079 }
5080 }
5081}
5082
5083/*
5084 * return TRUE if 'p' starts with 't_'
5085 */
5086 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005087istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088{
5089 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5090}
5091
Bram Moolenaardac13472019-09-16 21:06:21 +02005092/*
5093 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5094 */
5095 int
5096istermoption_idx(int opt_idx)
5097{
5098 return istermoption(&options[opt_idx]);
5099}
5100
Bram Moolenaar113e1072019-01-20 15:30:40 +01005101#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005103 * Unset local option value, similar to ":set opt<".
5104 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005105 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005106unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005107{
5108 struct vimoption *p;
5109 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005110 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005111
5112 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005113 if (opt_idx < 0)
5114 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005115 p = &(options[opt_idx]);
5116
5117 switch ((int)p->indir)
5118 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005119 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005120 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005121 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005122 break;
5123 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005124 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005125 break;
5126 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005127 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005128 break;
5129 case PV_AR:
5130 buf->b_p_ar = -1;
5131 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005132 case PV_BKC:
5133 clear_string_option(&buf->b_p_bkc);
5134 buf->b_bkc_flags = 0;
5135 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005136 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005137 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005138 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005139 case PV_TC:
5140 clear_string_option(&buf->b_p_tc);
5141 buf->b_tc_flags = 0;
5142 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005143 case PV_SISO:
5144 curwin->w_p_siso = -1;
5145 break;
5146 case PV_SO:
5147 curwin->w_p_so = -1;
5148 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005149#ifdef FEAT_FIND_ID
5150 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005151 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005152 break;
5153 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005154 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005155 break;
5156#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005157 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005158 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005159 break;
5160 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005161 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005162 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005163#ifdef FEAT_COMPL_FUNC
5164 case PV_TSRFU:
5165 clear_string_option(&buf->b_p_tsrfu);
5166 break;
5167#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005168 case PV_FP:
5169 clear_string_option(&buf->b_p_fp);
5170 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005171#ifdef FEAT_QUICKFIX
5172 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005173 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005174 break;
5175 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005176 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005177 break;
5178 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005179 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005180 break;
5181#endif
5182#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5183 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005184 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005185 break;
5186#endif
5187#if defined(FEAT_CRYPT)
5188 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005189 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005190 break;
5191#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005192#ifdef FEAT_LINEBREAK
5193 case PV_SBR:
5194 clear_string_option(&((win_T *)from)->w_p_sbr);
5195 break;
5196#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005197#ifdef FEAT_STL_OPT
5198 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005199 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005200 break;
5201#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005202 case PV_UL:
5203 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5204 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005205 case PV_LW:
5206 clear_string_option(&buf->b_p_lw);
5207 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005208 case PV_MENC:
5209 clear_string_option(&buf->b_p_menc);
5210 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005211 case PV_LCS:
5212 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005213 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005214 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005215 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005216 case PV_FCS:
5217 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005218 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005219 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005220 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005221 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005222 clear_string_option(&((win_T *)from)->w_p_ve);
5223 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005224 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005225 }
5226}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005227#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005228
5229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005231 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 */
5233 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005234get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005236 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
5238 if (p->var == VAR_WIN)
5239 return (char_u *)GLOBAL_WO(get_varp(p));
5240 return p->var;
5241 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005242 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
5244 switch ((int)p->indir)
5245 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005246 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005248 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5249 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5250 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005252 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5253 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5254 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005255 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005256 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005257 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005258 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5259 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005261 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5262 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005264 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5265 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005266#ifdef FEAT_COMPL_FUNC
5267 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5268#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005269#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5270 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5271#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005272#if defined(FEAT_CRYPT)
5273 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5274#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005275#ifdef FEAT_LINEBREAK
5276 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5277#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005278#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005279 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005280#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005281 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005282 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005283 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005284 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005285 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005286 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005287 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005288
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005290 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292 return get_varp(p);
5293}
5294
5295/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005296 * Get pointer to option variable at 'opt_idx', depending on local or global
5297 * scope.
5298 */
5299 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005300get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005301{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005302 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005303}
5304
5305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 * Get pointer to option variable.
5307 */
5308 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005309get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005311 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 if (p->var == NULL)
5313 return NULL;
5314
5315 switch ((int)p->indir)
5316 {
5317 case PV_NONE: return p->var;
5318
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005319 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005320 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005322 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005324 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005326 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005328 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005330 case PV_TC: return *curbuf->b_p_tc != NUL
5331 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005332 case PV_BKC: return *curbuf->b_p_bkc != NUL
5333 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005334 case PV_SISO: return curwin->w_p_siso >= 0
5335 ? (char_u *)&(curwin->w_p_siso) : p->var;
5336 case PV_SO: return curwin->w_p_so >= 0
5337 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005339 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005341 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5343#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005344 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005346 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005348#ifdef FEAT_COMPL_FUNC
5349 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5350 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5351#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005352 case PV_FP: return *curbuf->b_p_fp != NUL
5353 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005355 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005357 case PV_GP: return *curbuf->b_p_gp != NUL
5358 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5359 case PV_MP: return *curbuf->b_p_mp != NUL
5360 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005362#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5363 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5364 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5365#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005366#if defined(FEAT_CRYPT)
5367 case PV_CM: return *curbuf->b_p_cm != NUL
5368 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5369#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005370#ifdef FEAT_LINEBREAK
5371 case PV_SBR: return *curwin->w_p_sbr != NUL
5372 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5373#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005374#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005375 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005376 ? (char_u *)&(curwin->w_p_stl) : p->var;
5377#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005378 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5379 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005380 case PV_LW: return *curbuf->b_p_lw != NUL
5381 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005382 case PV_MENC: return *curbuf->b_p_menc != NUL
5383 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384#ifdef FEAT_ARABIC
5385 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5386#endif
5387 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005388 case PV_LCS: return *curwin->w_p_lcs != NUL
5389 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005390 case PV_FCS: return *curwin->w_p_fcs != NUL
5391 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005392 case PV_VE: return *curwin->w_p_ve != NUL
5393 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005394#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005395 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5396#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005397#ifdef FEAT_SYN_HL
5398 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5399 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005400 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005401 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403#ifdef FEAT_DIFF
5404 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5405#endif
5406#ifdef FEAT_FOLDING
5407 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5408 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5409 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5410 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5411 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5412 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5413 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5414# ifdef FEAT_EVAL
5415 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5416 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5417# endif
5418 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5419#endif
5420 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005421 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005422#ifdef FEAT_LINEBREAK
5423 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005426 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005427#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5429#endif
5430#ifdef FEAT_RIGHTLEFT
5431 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5432 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5433#endif
5434 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5435 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5436#ifdef FEAT_LINEBREAK
5437 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005438 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5439 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005441 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005443 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005444#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005445 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5446 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5447#endif
5448#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005449 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5450 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5451 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
5454 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5455 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5458 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5460 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5462 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5463 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01005464 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467#ifdef FEAT_FOLDING
5468 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005471#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005472 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005474#ifdef FEAT_COMPL_FUNC
5475 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005476 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005477#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005478#ifdef FEAT_EVAL
5479 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005482 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005488 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5490 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5491 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5492 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5493#ifdef FEAT_FIND_ID
5494# ifdef FEAT_EVAL
5495 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5496# endif
5497#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01005498#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5500 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5501#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005502#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005503 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505#ifdef FEAT_CRYPT
5506 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5510 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5511 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5512 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5513 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005515 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5522#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005523 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005524 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5525#endif
5526#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005527 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5528 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5529 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005530 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531#endif
5532 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5533 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5534 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5535 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005536#ifdef FEAT_PERSISTENT_UNDO
5537 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5540#ifdef FEAT_KEYMAP
5541 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5542#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005543#ifdef FEAT_SIGNS
5544 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5545#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005546#ifdef FEAT_VARTABS
5547 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5548 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5549#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005550 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005552 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 return (char_u *)&(curbuf->b_p_wm);
5554}
5555
5556/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005557 * Return a pointer to the variable for option at 'opt_idx'
5558 */
5559 char_u *
5560get_option_var(int opt_idx)
5561{
5562 return options[opt_idx].var;
5563}
5564
Dominique Pelle748b3082022-01-08 12:41:16 +00005565#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005566/*
5567 * Return the full name of the option at 'opt_idx'
5568 */
5569 char_u *
5570get_option_fullname(int opt_idx)
5571{
5572 return (char_u *)options[opt_idx].fullname;
5573}
Dominique Pelle748b3082022-01-08 12:41:16 +00005574#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005575
5576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 * Get the value of 'equalprg', either the buffer-local one or the global one.
5578 */
5579 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005580get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 if (*curbuf->b_p_ep == NUL)
5583 return p_ep;
5584 return curbuf->b_p_ep;
5585}
5586
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587/*
5588 * Copy options from one window to another.
5589 * Used when splitting a window.
5590 */
5591 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005592win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5595 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005596 after_copy_winopt(wp_to);
5597}
5598
5599/*
5600 * After copying window options: update variables depending on options.
5601 */
5602 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00005603after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005604{
5605#ifdef FEAT_LINEBREAK
5606 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005607#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005608#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005609 fill_culopt_flags(NULL, wp);
5610 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005611#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005612 set_chars_option(wp, &wp->w_p_lcs, TRUE);
5613 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005614}
5615
5616 static char_u *
5617copy_option_val(char_u *val)
5618{
5619 if (val == empty_option)
5620 return empty_option; // no need to allocate memory
5621 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
5624/*
5625 * Copy the options from one winopt_T to another.
5626 * Doesn't free the old option values in "to", use clear_winopt() for that.
5627 * The 'scroll' option is not copied, because it depends on the window height.
5628 * The 'previewwindow' option is reset, there can be only one preview window.
5629 */
5630 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005631copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633#ifdef FEAT_ARABIC
5634 to->wo_arab = from->wo_arab;
5635#endif
5636 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005637 to->wo_lcs = copy_option_val(from->wo_lcs);
5638 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005640 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005641 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02005642 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005643#ifdef FEAT_LINEBREAK
5644 to->wo_nuw = from->wo_nuw;
5645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646#ifdef FEAT_RIGHTLEFT
5647 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005648 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005650#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005651 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01005652#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005653#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005654 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005657#ifdef FEAT_DIFF
5658 to->wo_wrap_save = from->wo_wrap_save;
5659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660#ifdef FEAT_LINEBREAK
5661 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005662 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005663 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005665 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005667 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005668 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005669 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005670#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005671 to->wo_spell = from->wo_spell;
5672#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005673#ifdef FEAT_SYN_HL
5674 to->wo_cuc = from->wo_cuc;
5675 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005676 to->wo_culopt = copy_option_val(from->wo_culopt);
5677 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679#ifdef FEAT_DIFF
5680 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005681 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005683#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005684 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005685 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005686#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005687#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005688 to->wo_twk = copy_option_val(from->wo_twk);
5689 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691#ifdef FEAT_FOLDING
5692 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005693 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005695 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005696 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 to->wo_fml = from->wo_fml;
5698 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005699 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005700 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005701 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005702 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 to->wo_fdn = from->wo_fdn;
5704# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005705 to->wo_fde = copy_option_val(from->wo_fde);
5706 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005708 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005710#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005711 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005712#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005713
5714#ifdef FEAT_EVAL
5715 // Copy the script context so that we know where the value was last set.
5716 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5717 sizeof(to->wo_script_ctx));
5718#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005719 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720}
5721
5722/*
5723 * Check string options in a window for a NULL value.
5724 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005725 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005726check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
5728 check_winopt(&win->w_onebuf_opt);
5729 check_winopt(&win->w_allbuf_opt);
5730}
5731
5732/*
5733 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5734 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005735 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005736check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
5738#ifdef FEAT_FOLDING
5739 check_string_option(&wop->wo_fdi);
5740 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005741 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742# ifdef FEAT_EVAL
5743 check_string_option(&wop->wo_fde);
5744 check_string_option(&wop->wo_fdt);
5745# endif
5746 check_string_option(&wop->wo_fmr);
5747#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005748#ifdef FEAT_SIGNS
5749 check_string_option(&wop->wo_scl);
5750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751#ifdef FEAT_RIGHTLEFT
5752 check_string_option(&wop->wo_rlc);
5753#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005754#ifdef FEAT_LINEBREAK
5755 check_string_option(&wop->wo_sbr);
5756#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005757#ifdef FEAT_STL_OPT
5758 check_string_option(&wop->wo_stl);
5759#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005760#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005761 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005762 check_string_option(&wop->wo_cc);
5763#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005764#ifdef FEAT_CONCEAL
5765 check_string_option(&wop->wo_cocu);
5766#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005767#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005768 check_string_option(&wop->wo_twk);
5769 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005770#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005771#ifdef FEAT_LINEBREAK
5772 check_string_option(&wop->wo_briopt);
5773#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005774 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005775 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005776 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005777 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778}
5779
5780/*
5781 * Free the allocated memory inside a winopt_T.
5782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005784clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
5786#ifdef FEAT_FOLDING
5787 clear_string_option(&wop->wo_fdi);
5788 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005789 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790# ifdef FEAT_EVAL
5791 clear_string_option(&wop->wo_fde);
5792 clear_string_option(&wop->wo_fdt);
5793# endif
5794 clear_string_option(&wop->wo_fmr);
5795#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005796#ifdef FEAT_SIGNS
5797 clear_string_option(&wop->wo_scl);
5798#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005799#ifdef FEAT_LINEBREAK
5800 clear_string_option(&wop->wo_briopt);
5801#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005802 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803#ifdef FEAT_RIGHTLEFT
5804 clear_string_option(&wop->wo_rlc);
5805#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005806#ifdef FEAT_LINEBREAK
5807 clear_string_option(&wop->wo_sbr);
5808#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005809#ifdef FEAT_STL_OPT
5810 clear_string_option(&wop->wo_stl);
5811#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005812#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005813 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005814 clear_string_option(&wop->wo_cc);
5815#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005816#ifdef FEAT_CONCEAL
5817 clear_string_option(&wop->wo_cocu);
5818#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005819#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005820 clear_string_option(&wop->wo_twk);
5821 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005822#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005823 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005824 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005825 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826}
5827
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005828#ifdef FEAT_EVAL
5829// Index into the options table for a buffer-local option enum.
5830static int buf_opt_idx[BV_COUNT];
5831# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5832
5833/*
5834 * Initialize buf_opt_idx[] if not done already.
5835 */
5836 static void
5837init_buf_opt_idx(void)
5838{
5839 static int did_init_buf_opt_idx = FALSE;
5840 int i;
5841
5842 if (did_init_buf_opt_idx)
5843 return;
5844 did_init_buf_opt_idx = TRUE;
5845 for (i = 0; !istermoption_idx(i); i++)
5846 if (options[i].indir & PV_BUF)
5847 buf_opt_idx[options[i].indir & PV_MASK] = i;
5848}
5849#else
5850# define COPY_OPT_SCTX(buf, bv)
5851#endif
5852
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853/*
5854 * Copy global option values to local options for one buffer.
5855 * Used when creating a new buffer and sometimes when entering a buffer.
5856 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005857 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5859 * appropriate.
5860 * BCO_NOHELP Don't copy the values to a help buffer.
5861 */
5862 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005863buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864{
5865 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005866 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 int dont_do_help;
5868 int did_isk = FALSE;
5869
5870 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 * Skip this when the option defaults have not been set yet. Happens when
5872 * main() allocates the first buffer.
5873 */
5874 if (p_cpo != NULL)
5875 {
5876 /*
5877 * Always copy when entering and 'cpo' contains 'S'.
5878 * Don't copy when already initialized.
5879 * Don't copy when 'cpo' contains 's' and not entering.
5880 * 'S' BCO_ENTER initialized 's' should_copy
5881 * yes yes X X TRUE
5882 * yes no yes X FALSE
5883 * no X yes X FALSE
5884 * X no no yes FALSE
5885 * X no no no TRUE
5886 * no yes no X TRUE
5887 */
5888 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5889 && (buf->b_p_initialized
5890 || (!(flags & BCO_ENTER)
5891 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5892 should_copy = FALSE;
5893
5894 if (should_copy || (flags & BCO_ALWAYS))
5895 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005896#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005897 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005898 init_buf_opt_idx();
5899#endif
5900 // Don't copy the options specific to a help buffer when
5901 // BCO_NOHELP is given or the options were initialized already
5902 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5904 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005905 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 {
5907 save_p_isk = buf->b_p_isk;
5908 buf->b_p_isk = NULL;
5909 }
5910 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005911 * Always free the allocated strings. If not already initialized,
5912 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 */
5914 if (!buf->b_p_initialized)
5915 {
5916 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005917 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005920 switch (*p_ffs)
5921 {
5922 case 'm':
5923 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5924 case 'd':
5925 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5926 case 'u':
5927 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5928 default:
5929 buf->b_p_ff = vim_strsave(p_ff);
5930 }
5931 if (buf->b_p_ff != NULL)
5932 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 buf->b_p_bh = empty_option;
5934 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 }
5936 else
5937 free_buf_options(buf, FALSE);
5938
5939 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005940 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 buf->b_p_ai_nopaste = p_ai_nopaste;
5942 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005943 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005945 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 buf->b_p_tw_nopaste = p_tw_nopaste;
5947 buf->b_p_tw_nobin = p_tw_nobin;
5948 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005949 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 buf->b_p_wm_nopaste = p_wm_nopaste;
5951 buf->b_p_wm_nobin = p_wm_nobin;
5952 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005953 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005954 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005955 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005956 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005957 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005959 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005961 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005963 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 buf->b_p_ml_nobin = p_ml_nobin;
5965 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005966 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005967 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005968 buf->b_p_swf = FALSE;
5969 else
5970 {
5971 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00005972 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005975 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005976#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005977 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005978 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005980#ifdef FEAT_COMPL_FUNC
5981 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005982 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005983 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005984 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005985 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005986 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005987#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005988#ifdef FEAT_EVAL
5989 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005990 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005991 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005994 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005996#ifdef FEAT_VARTABS
5997 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005998 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005999 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006000 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006001 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006002 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006003 buf->b_p_vsts_nopaste = p_vsts_nopaste
6004 ? vim_strsave(p_vsts_nopaste) : NULL;
6005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006007 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006009 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010#ifdef FEAT_FOLDING
6011 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006012 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013#endif
6014 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006015 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006016 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006017 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006018 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6019 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006021 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006023 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006025 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006027 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006028
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006030 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006032 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006034 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006035 buf->b_p_cinsd = vim_strsave(p_cinsd);
6036 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006037
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006038 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006041 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006043 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006045 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006047 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006049 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006050 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006051 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006052#endif
6053#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006054 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006055 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006056 (void)compile_cap_prog(&buf->b_s);
6057 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006058 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006059 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006060 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006061 buf->b_s.b_p_spo = vim_strsave(p_spo);
6062 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006064#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006066 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006068 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006070 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006071#if defined(FEAT_EVAL)
6072 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006073 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075#ifdef FEAT_CRYPT
6076 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006077 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006080 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081#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 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006128 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006129#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6130 buf->b_p_bexpr = empty_option;
6131#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006132#if defined(FEAT_CRYPT)
6133 buf->b_p_cm = empty_option;
6134#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006135#ifdef FEAT_PERSISTENT_UNDO
6136 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006137 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006138#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006139 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006140 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141
6142 /*
6143 * Don't copy the options set by ex_help(), use the saved values,
6144 * when going from a help buffer to a non-help buffer.
6145 * Don't touch these at all when BCO_NOHELP is used and going from
6146 * or to a help buffer.
6147 */
6148 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006149 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006151#ifdef FEAT_VARTABS
6152 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006153 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006154 else
6155 buf->b_p_vts_array = NULL;
6156#endif
6157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 else
6159 {
6160 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006161 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 did_isk = TRUE;
6163 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006164 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006165#ifdef FEAT_VARTABS
6166 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006167 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006168 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006169 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006170 else
6171 buf->b_p_vts_array = NULL;
6172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 if (buf->b_p_bt[0] == 'h')
6175 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006177 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 }
6179 }
6180
6181 /*
6182 * When the options should be copied (ignoring BCO_ALWAYS), set the
6183 * flag that indicates that the options have been initialized.
6184 */
6185 if (should_copy)
6186 buf->b_p_initialized = TRUE;
6187 }
6188
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006189 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 if (did_isk)
6191 (void)buf_init_chartab(buf, FALSE);
6192}
6193
6194/*
6195 * Reset the 'modifiable' option and its default value.
6196 */
6197 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006198reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
6200 int opt_idx;
6201
6202 curbuf->b_p_ma = FALSE;
6203 p_ma = FALSE;
6204 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006205 if (opt_idx >= 0)
6206 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207}
6208
6209/*
6210 * Set the global value for 'iminsert' to the local value.
6211 */
6212 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006213set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214{
6215 p_iminsert = curbuf->b_p_iminsert;
6216}
6217
6218/*
6219 * Set the global value for 'imsearch' to the local value.
6220 */
6221 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006222set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223{
6224 p_imsearch = curbuf->b_p_imsearch;
6225}
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227static int expand_option_idx = -1;
6228static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6229static int expand_option_flags = 0;
6230
6231 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006232set_context_in_set_cmd(
6233 expand_T *xp,
6234 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006235 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
6237 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006238 long_u flags = 0; // init for GCC
6239 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 char_u *p;
6241 char_u *s;
6242 int is_term_option = FALSE;
6243 int key;
6244
6245 expand_option_flags = opt_flags;
6246
6247 xp->xp_context = EXPAND_SETTINGS;
6248 if (*arg == NUL)
6249 {
6250 xp->xp_pattern = arg;
6251 return;
6252 }
6253 p = arg + STRLEN(arg) - 1;
6254 if (*p == ' ' && *(p - 1) != '\\')
6255 {
6256 xp->xp_pattern = p + 1;
6257 return;
6258 }
6259 while (p > arg)
6260 {
6261 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006262 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 if (*p == ' ' || *p == ',')
6264 {
6265 while (s > arg && *(s - 1) == '\\')
6266 --s;
6267 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006268 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 if (*p == ' ' && ((p - s) & 1) == 0)
6270 {
6271 ++p;
6272 break;
6273 }
6274 --p;
6275 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006276 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 {
6278 xp->xp_context = EXPAND_BOOL_SETTINGS;
6279 p += 2;
6280 }
6281 if (STRNCMP(p, "inv", 3) == 0)
6282 {
6283 xp->xp_context = EXPAND_BOOL_SETTINGS;
6284 p += 3;
6285 }
6286 xp->xp_pattern = arg = p;
6287 if (*arg == '<')
6288 {
6289 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006290 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 return;
6292 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006293 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 {
6295 xp->xp_context = EXPAND_NOTHING;
6296 return;
6297 }
6298 nextchar = *++p;
6299 is_term_option = TRUE;
6300 expand_option_name[2] = KEY2TERMCAP0(key);
6301 expand_option_name[3] = KEY2TERMCAP1(key);
6302 }
6303 else
6304 {
6305 if (p[0] == 't' && p[1] == '_')
6306 {
6307 p += 2;
6308 if (*p != NUL)
6309 ++p;
6310 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006311 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 nextchar = *++p;
6313 is_term_option = TRUE;
6314 expand_option_name[2] = p[-2];
6315 expand_option_name[3] = p[-1];
6316 }
6317 else
6318 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006319 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6321 p++;
6322 if (*p == NUL)
6323 return;
6324 nextchar = *p;
6325 *p = NUL;
6326 opt_idx = findoption(arg);
6327 *p = nextchar;
6328 if (opt_idx == -1 || options[opt_idx].var == NULL)
6329 {
6330 xp->xp_context = EXPAND_NOTHING;
6331 return;
6332 }
6333 flags = options[opt_idx].flags;
6334 if (flags & P_BOOL)
6335 {
6336 xp->xp_context = EXPAND_NOTHING;
6337 return;
6338 }
6339 }
6340 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006341 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6343 {
6344 ++p;
6345 nextchar = '=';
6346 }
6347 if ((nextchar != '=' && nextchar != ':')
6348 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6349 {
6350 xp->xp_context = EXPAND_UNSUCCESSFUL;
6351 return;
6352 }
6353 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6354 {
6355 xp->xp_context = EXPAND_OLD_SETTING;
6356 if (is_term_option)
6357 expand_option_idx = -1;
6358 else
6359 expand_option_idx = opt_idx;
6360 xp->xp_pattern = p + 1;
6361 return;
6362 }
6363 xp->xp_context = EXPAND_NOTHING;
6364 if (is_term_option || (flags & P_NUM))
6365 return;
6366
6367 xp->xp_pattern = p + 1;
6368
6369 if (flags & P_EXPAND)
6370 {
6371 p = options[opt_idx].var;
6372 if (p == (char_u *)&p_bdir
6373 || p == (char_u *)&p_dir
6374 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006375 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378#ifdef FEAT_SESSION
6379 || p == (char_u *)&p_vdir
6380#endif
6381 )
6382 {
6383 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006384 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 xp->xp_backslash = XP_BS_THREE;
6386 else
6387 xp->xp_backslash = XP_BS_ONE;
6388 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006389 else if (p == (char_u *)&p_ft)
6390 {
6391 xp->xp_context = EXPAND_FILETYPE;
6392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 else
6394 {
6395 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006396 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 if (p == (char_u *)&p_tags)
6398 xp->xp_backslash = XP_BS_THREE;
6399 else
6400 xp->xp_backslash = XP_BS_ONE;
6401 }
6402 }
6403
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006404 // For an option that is a list of file names, find the start of the
6405 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6407 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006408 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 if (*p == ' ' || *p == ',')
6410 {
6411 s = p;
6412 while (s > xp->xp_pattern && *(s - 1) == '\\')
6413 --s;
6414 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6415 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6416 {
6417 xp->xp_pattern = p + 1;
6418 break;
6419 }
6420 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006421
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006422#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006423 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006424 if (options[opt_idx].var == (char_u *)&p_sps
6425 && STRNCMP(p, "file:", 5) == 0)
6426 {
6427 xp->xp_pattern = p + 5;
6428 break;
6429 }
6430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432}
6433
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006434/*
6435 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
6436 *
6437 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
6438 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
6439 *
6440 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
6441 * regular expression 'regmatch', then stores the match in matches[idx] and
6442 * returns TRUE.
6443 *
6444 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6445 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
6446 *
6447 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6448 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
6449 */
6450 static int
6451match_str(
6452 char_u *str,
6453 regmatch_T *regmatch,
6454 char_u **matches,
6455 int idx,
6456 int test_only,
6457 int fuzzy,
6458 char_u *fuzzystr,
6459 fuzmatch_str_T *fuzmatch)
6460{
6461 if (!fuzzy)
6462 {
6463 if (vim_regexec(regmatch, str, (colnr_T)0))
6464 {
6465 if (!test_only)
6466 matches[idx] = vim_strsave(str);
6467 return TRUE;
6468 }
6469 }
6470 else
6471 {
6472 int score;
6473
6474 score = fuzzy_match_str(str, fuzzystr);
6475 if (score != 0)
6476 {
6477 if (!test_only)
6478 {
6479 fuzmatch[idx].idx = idx;
6480 fuzmatch[idx].str = vim_strsave(str);
6481 fuzmatch[idx].score = score;
6482 }
6483
6484 return TRUE;
6485 }
6486 }
6487
6488 return FALSE;
6489}
6490
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006492ExpandSettings(
6493 expand_T *xp,
6494 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006495 char_u *fuzzystr,
6496 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01006497 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006498 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006500 int num_normal = 0; // Nr of matching non-term-code settings
6501 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 int opt_idx;
6503 int match;
6504 int count = 0;
6505 char_u *str;
6506 int loop;
6507 int is_term_opt;
6508 char_u name_buf[MAX_KEY_NAME_LEN];
6509 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006510 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006511 int fuzzy;
6512 fuzmatch_str_T *fuzmatch = NULL;
6513
Christian Brabandtcb747892022-05-08 21:10:56 +01006514 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006516 // do this loop twice:
6517 // loop == 0: count the number of matching options
6518 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 for (loop = 0; loop <= 1; ++loop)
6520 {
6521 regmatch->rm_ic = ic;
6522 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6523 {
K.Takataeeec2542021-06-02 13:28:16 +02006524 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006525 {
6526 if (match_str((char_u *)names[match], regmatch, *matches,
6527 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
6529 if (loop == 0)
6530 num_normal++;
6531 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006532 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 }
6536 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6537 opt_idx++)
6538 {
6539 if (options[opt_idx].var == NULL)
6540 continue;
6541 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6542 && !(options[opt_idx].flags & P_BOOL))
6543 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006544 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 if (is_term_opt && num_normal > 0)
6546 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006547
6548 if (match_str(str, regmatch, *matches, count, (loop == 0),
6549 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {
6551 if (loop == 0)
6552 {
6553 if (is_term_opt)
6554 num_term++;
6555 else
6556 num_normal++;
6557 }
6558 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006559 count++;
6560 }
6561 else if (!fuzzy && options[opt_idx].shortname != NULL
6562 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006563 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006564 {
6565 // Compare against the abbreviated option name (for regular
6566 // expression match). Fuzzy matching (previous if) already
6567 // matches against both the expanded and abbreviated names.
6568 if (loop == 0)
6569 {
6570 if (is_term_opt)
6571 num_term++;
6572 else
6573 num_normal++;
6574 }
6575 else
6576 (*matches)[count++] = vim_strsave(str);
6577 }
6578 else if (is_term_opt)
6579 {
6580 name_buf[0] = '<';
6581 name_buf[1] = 't';
6582 name_buf[2] = '_';
6583 name_buf[3] = str[2];
6584 name_buf[4] = str[3];
6585 name_buf[5] = '>';
6586 name_buf[6] = NUL;
6587
6588 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6589 fuzzy, fuzzystr, fuzmatch))
6590 {
6591 if (loop == 0)
6592 num_term++;
6593 else
6594 count++;
6595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 }
6597 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006598
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 /*
6600 * Check terminal key codes, these are not in the option table
6601 */
6602 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6603 {
6604 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6605 {
6606 if (!isprint(str[0]) || !isprint(str[1]))
6607 continue;
6608
6609 name_buf[0] = 't';
6610 name_buf[1] = '_';
6611 name_buf[2] = str[0];
6612 name_buf[3] = str[1];
6613 name_buf[4] = NUL;
6614
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006615 if (match_str(name_buf, regmatch, *matches, count,
6616 (loop == 0), fuzzy, fuzzystr, fuzmatch))
6617 {
6618 if (loop == 0)
6619 num_term++;
6620 else
6621 count++;
6622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 else
6624 {
6625 name_buf[0] = '<';
6626 name_buf[1] = 't';
6627 name_buf[2] = '_';
6628 name_buf[3] = str[0];
6629 name_buf[4] = str[1];
6630 name_buf[5] = '>';
6631 name_buf[6] = NUL;
6632
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006633 if (match_str(name_buf, regmatch, *matches, count,
6634 (loop == 0), fuzzy, fuzzystr,
6635 fuzmatch))
6636 {
6637 if (loop == 0)
6638 num_term++;
6639 else
6640 count++;
6641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 }
6643 }
6644
6645 /*
6646 * Check special key names.
6647 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006648 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6650 {
6651 name_buf[0] = '<';
6652 STRCPY(name_buf + 1, str);
6653 STRCAT(name_buf, ">");
6654
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006655 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6656 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 {
6658 if (loop == 0)
6659 num_term++;
6660 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006661 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 }
6663 }
6664 }
6665 if (loop == 0)
6666 {
6667 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006668 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006670 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 else
6672 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006673 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006675 *matches = ALLOC_MULT(char_u *, *numMatches);
6676 if (*matches == NULL)
6677 {
6678 *matches = (char_u **)"";
6679 return FAIL;
6680 }
6681 }
6682 else
6683 {
6684 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
6685 if (fuzmatch == NULL)
6686 {
6687 *matches = (char_u **)"";
6688 return FAIL;
6689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 }
6691 }
6692 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006693
6694 if (fuzzy &&
6695 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
6696 return FAIL;
6697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 return OK;
6699}
6700
6701 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006702ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006704 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 char_u *buf;
6706
6707 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006708 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 if (*file == NULL)
6710 return FAIL;
6711
6712 /*
6713 * For a terminal key code expand_option_idx is < 0.
6714 */
6715 if (expand_option_idx < 0)
6716 {
6717 var = find_termcode(expand_option_name + 2);
6718 if (var == NULL)
6719 expand_option_idx = findoption(expand_option_name);
6720 }
6721
6722 if (expand_option_idx >= 0)
6723 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006724 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 option_value2string(&options[expand_option_idx], expand_option_flags);
6726 var = NameBuff;
6727 }
6728 else if (var == NULL)
6729 var = (char_u *)"";
6730
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006731 // A backslash is required before some characters. This is the reverse of
6732 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 buf = vim_strsave_escaped(var, escape_chars);
6734
6735 if (buf == NULL)
6736 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006737 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 return FAIL;
6739 }
6740
6741#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006742 // For MS-Windows et al. we don't double backslashes at the start and
6743 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006744 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 if (var[0] == '\\' && var[1] == '\\'
6746 && expand_option_idx >= 0
6747 && (options[expand_option_idx].flags & P_EXPAND)
6748 && vim_isfilec(var[2])
6749 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006750 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751#endif
6752
6753 *file[0] = buf;
6754 *num_file = 1;
6755 return OK;
6756}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
6758/*
6759 * Get the value for the numeric or string option *opp in a nice format into
6760 * NameBuff[]. Must not be called with a hidden option!
6761 */
6762 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006763option_value2string(
6764 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006765 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766{
6767 char_u *varp;
6768
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006769 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770
6771 if (opp->flags & P_NUM)
6772 {
6773 long wc = 0;
6774
6775 if (wc_use_keyname(varp, &wc))
6776 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6777 else if (wc != 0)
6778 STRCPY(NameBuff, transchar((int)wc));
6779 else
6780 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6781 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006782 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {
6784 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006785 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 NameBuff[0] = NUL;
6787#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006788 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006789 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 STRCPY(NameBuff, "*****");
6791#endif
6792 else if (opp->flags & P_EXPAND)
6793 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006794 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 else if ((char_u **)opp->var == &p_pt)
6796 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6797 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006798 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 }
6800}
6801
6802/*
6803 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6804 * printed as a keyname.
6805 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6806 */
6807 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006808wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809{
6810 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6811 {
6812 *wcp = *(long *)varp;
6813 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6814 return TRUE;
6815 }
6816 return FALSE;
6817}
6818
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 * Return TRUE if "x" is present in 'shortmess' option, or
6821 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6822 */
6823 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006824shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006826 return p_shm != NULL &&
6827 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 || (vim_strchr(p_shm, 'a') != NULL
6829 && vim_strchr((char_u *)SHM_A, x) != NULL));
6830}
6831
6832/*
6833 * paste_option_changed() - Called after p_paste was set or reset.
6834 */
6835 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006836paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837{
6838 static int old_p_paste = FALSE;
6839 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006840 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841#ifdef FEAT_CMDL_INFO
6842 static int save_ru = 0;
6843#endif
6844#ifdef FEAT_RIGHTLEFT
6845 static int save_ri = 0;
6846 static int save_hkmap = 0;
6847#endif
6848 buf_T *buf;
6849
6850 if (p_paste)
6851 {
6852 /*
6853 * Paste switched from off to on.
6854 * Save the current values, so they can be restored later.
6855 */
6856 if (!old_p_paste)
6857 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006858 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006859 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 {
6861 buf->b_p_tw_nopaste = buf->b_p_tw;
6862 buf->b_p_wm_nopaste = buf->b_p_wm;
6863 buf->b_p_sts_nopaste = buf->b_p_sts;
6864 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006865 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006866#ifdef FEAT_VARTABS
6867 if (buf->b_p_vsts_nopaste)
6868 vim_free(buf->b_p_vsts_nopaste);
6869 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6870 ? vim_strsave(buf->b_p_vsts) : NULL;
6871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 }
6873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006874 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006876 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877#ifdef FEAT_CMDL_INFO
6878 save_ru = p_ru;
6879#endif
6880#ifdef FEAT_RIGHTLEFT
6881 save_ri = p_ri;
6882 save_hkmap = p_hkmap;
6883#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006884 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006885 p_ai_nopaste = p_ai;
6886 p_et_nopaste = p_et;
6887 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 p_tw_nopaste = p_tw;
6889 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006890#ifdef FEAT_VARTABS
6891 if (p_vsts_nopaste)
6892 vim_free(p_vsts_nopaste);
6893 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 }
6896
6897 /*
6898 * Always set the option values, also when 'paste' is set when it is
6899 * already on.
6900 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006901 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006902 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006904 buf->b_p_tw = 0; // textwidth is 0
6905 buf->b_p_wm = 0; // wrapmargin is 0
6906 buf->b_p_sts = 0; // softtabstop is 0
6907 buf->b_p_ai = 0; // no auto-indent
6908 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006909#ifdef FEAT_VARTABS
6910 if (buf->b_p_vsts)
6911 free_string_option(buf->b_p_vsts);
6912 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006913 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 }
6916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006917 // set global options
6918 p_sm = 0; // no showmatch
6919 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006922 status_redraw_all(); // redraw to remove the ruler
6923 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924#endif
6925#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006926 p_ri = 0; // no reverse insert
6927 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006929 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 p_tw = 0;
6931 p_wm = 0;
6932 p_sts = 0;
6933 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006934#ifdef FEAT_VARTABS
6935 if (p_vsts)
6936 free_string_option(p_vsts);
6937 p_vsts = empty_option;
6938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 }
6940
6941 /*
6942 * Paste switched from on to off: Restore saved values.
6943 */
6944 else if (old_p_paste)
6945 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006946 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006947 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 {
6949 buf->b_p_tw = buf->b_p_tw_nopaste;
6950 buf->b_p_wm = buf->b_p_wm_nopaste;
6951 buf->b_p_sts = buf->b_p_sts_nopaste;
6952 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006953 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006954#ifdef FEAT_VARTABS
6955 if (buf->b_p_vsts)
6956 free_string_option(buf->b_p_vsts);
6957 buf->b_p_vsts = buf->b_p_vsts_nopaste
6958 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006959 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006960 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006961 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006962 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006963 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 }
6966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006967 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006969 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006972 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 p_ru = save_ru;
6974#endif
6975#ifdef FEAT_RIGHTLEFT
6976 p_ri = save_ri;
6977 p_hkmap = save_hkmap;
6978#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006979 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006980 p_ai = p_ai_nopaste;
6981 p_et = p_et_nopaste;
6982 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 p_tw = p_tw_nopaste;
6984 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006985#ifdef FEAT_VARTABS
6986 if (p_vsts)
6987 free_string_option(p_vsts);
6988 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 }
6991
6992 old_p_paste = p_paste;
6993}
6994
6995/*
6996 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6997 *
6998 * Reset 'compatible' and set the values for options that didn't get set yet
6999 * to the Vim defaults.
7000 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007001 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 */
7003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007004vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007006 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007007 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007008 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009
7010 if (!option_was_set((char_u *)"cp"))
7011 {
7012 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007013 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7015 set_option_default(opt_idx, OPT_FREE, FALSE);
7016 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007017 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007019
7020 if (fname != NULL)
7021 {
7022 p = vim_getenv(envname, &dofree);
7023 if (p == NULL)
7024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007025 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007026 p = FullName_save(fname, FALSE);
7027 if (p != NULL)
7028 {
7029 vim_setenv(envname, p);
7030 vim_free(p);
7031 }
7032 }
7033 else if (dofree)
7034 vim_free(p);
7035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036}
7037
7038/*
7039 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7040 */
7041 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007042change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007044 int opt_idx;
7045
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 if (p_cp != on)
7047 {
7048 p_cp = on;
7049 compatible_set();
7050 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007051 opt_idx = findoption((char_u *)"cp");
7052 if (opt_idx >= 0)
7053 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054}
7055
7056/*
7057 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007058 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 */
7060 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007061option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062{
7063 int idx;
7064
7065 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007066 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 return FALSE;
7068 if (options[idx].flags & P_WAS_SET)
7069 return TRUE;
7070 return FALSE;
7071}
7072
7073/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007074 * Reset the flag indicating option "name" was set.
7075 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007076 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007077reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007078{
7079 int idx = findoption(name);
7080
7081 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007082 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007083 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007084 return OK;
7085 }
7086 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007087}
7088
7089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 * compatible_set() - Called when 'compatible' has been set or unset.
7091 *
7092 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7093 * flag) to a Vi compatible value.
7094 * When 'compatible' is unset: Set all options that have a different default
7095 * for Vim (without the P_VI_DEF flag) to that default.
7096 */
7097 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007098compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099{
7100 int opt_idx;
7101
Bram Moolenaardac13472019-09-16 21:06:21 +02007102 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7104 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7105 set_option_default(opt_idx, OPT_FREE, p_cp);
7106 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007107 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108}
7109
Bram Moolenaardac13472019-09-16 21:06:21 +02007110#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112/*
7113 * fill_breakat_flags() -- called when 'breakat' changes value.
7114 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007115 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007116fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007118 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 int i;
7120
7121 for (i = 0; i < 256; i++)
7122 breakat_flags[i] = FALSE;
7123
7124 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007125 for (p = p_breakat; *p; p++)
7126 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128#endif
7129
7130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 * Check if backspacing over something is allowed.
7132 */
7133 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007134can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007135 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007137#ifdef FEAT_JOB_CHANNEL
7138 if (what == BS_START && bt_prompt(curbuf))
7139 return FALSE;
7140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 switch (*p_bs)
7142 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007143 case '3': return TRUE;
7144 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 case '1': return (what != BS_START);
7146 case '0': return FALSE;
7147 }
7148 return vim_strchr(p_bs, what) != NULL;
7149}
7150
7151/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007152 * Return the effective 'scrolloff' value for the current window, using the
7153 * global value when appropriate.
7154 */
7155 long
7156get_scrolloff_value(void)
7157{
7158 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7159}
7160
7161/*
7162 * Return the effective 'sidescrolloff' value for the current window, using the
7163 * global value when appropriate.
7164 */
7165 long
7166get_sidescrolloff_value(void)
7167{
7168 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7169}
7170
7171/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007172 * Get the local or global value of 'backupcopy'.
7173 */
7174 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007175get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007176{
7177 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7178}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007179
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007180#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007181/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007182 * Get the local or global value of 'formatlistpat'.
7183 */
7184 char_u *
7185get_flp_value(buf_T *buf)
7186{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007187 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7188 return p_flp;
7189 return buf->b_p_flp;
7190}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007191#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007192
7193/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007194 * Get the local or global value of the 'virtualedit' flags.
7195 */
7196 unsigned int
7197get_ve_flags(void)
7198{
Gary Johnson51ad8502021-08-03 18:33:08 +02007199 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7200 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007201}
7202
Bram Moolenaaree857022019-11-09 23:26:40 +01007203#if defined(FEAT_LINEBREAK) || defined(PROTO)
7204/*
7205 * Get the local or global value of 'showbreak'.
7206 */
7207 char_u *
7208get_showbreak_value(win_T *win)
7209{
7210 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7211 return p_sbr;
7212 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7213 return empty_option;
7214 return win->w_p_sbr;
7215}
7216#endif
7217
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007218#if defined(FEAT_EVAL) || defined(PROTO)
7219/*
7220 * Get window or buffer local options.
7221 */
7222 dict_T *
7223get_winbuf_options(int bufopt)
7224{
7225 dict_T *d;
7226 int opt_idx;
7227
7228 d = dict_alloc();
7229 if (d == NULL)
7230 return NULL;
7231
Bram Moolenaardac13472019-09-16 21:06:21 +02007232 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007233 {
7234 struct vimoption *opt = &options[opt_idx];
7235
7236 if ((bufopt && (opt->indir & PV_BUF))
7237 || (!bufopt && (opt->indir & PV_WIN)))
7238 {
7239 char_u *varp = get_varp(opt);
7240
7241 if (varp != NULL)
7242 {
7243 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007244 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007245 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007246 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007247 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007248 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007249 }
7250 }
7251 }
7252
7253 return d;
7254}
7255#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007256
Bram Moolenaardac13472019-09-16 21:06:21 +02007257#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007258/*
7259 * This is called when 'culopt' is changed
7260 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007261 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007262fill_culopt_flags(char_u *val, win_T *wp)
7263{
7264 char_u *p;
7265 char_u culopt_flags_new = 0;
7266
7267 if (val == NULL)
7268 p = wp->w_p_culopt;
7269 else
7270 p = val;
7271 while (*p != NUL)
7272 {
7273 if (STRNCMP(p, "line", 4) == 0)
7274 {
7275 p += 4;
7276 culopt_flags_new |= CULOPT_LINE;
7277 }
7278 else if (STRNCMP(p, "both", 4) == 0)
7279 {
7280 p += 4;
7281 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7282 }
7283 else if (STRNCMP(p, "number", 6) == 0)
7284 {
7285 p += 6;
7286 culopt_flags_new |= CULOPT_NBR;
7287 }
7288 else if (STRNCMP(p, "screenline", 10) == 0)
7289 {
7290 p += 10;
7291 culopt_flags_new |= CULOPT_SCRLINE;
7292 }
7293
7294 if (*p != ',' && *p != NUL)
7295 return FAIL;
7296 if (*p == ',')
7297 ++p;
7298 }
7299
7300 // Can't have both "line" and "screenline".
7301 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7302 return FAIL;
7303 wp->w_p_culopt_flags = culopt_flags_new;
7304
7305 return OK;
7306}
7307#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007308
7309/*
7310 * Get the value of 'magic' adjusted for Vim9 script.
7311 */
7312 int
7313magic_isset(void)
7314{
7315 switch (magic_overruled)
7316 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007317 case OPTION_MAGIC_ON: return TRUE;
7318 case OPTION_MAGIC_OFF: return FALSE;
7319 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007320 }
7321#ifdef FEAT_EVAL
7322 if (in_vim9script())
7323 return TRUE;
7324#endif
7325 return p_magic;
7326}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007327
7328/*
7329 * Set the callback function value for an option that accepts a function name,
7330 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7331 * Returns OK if the option is successfully set to a function, otherwise
7332 * returns FAIL.
7333 */
7334 int
7335option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7336{
7337#ifdef FEAT_EVAL
7338 typval_T *tv;
7339 callback_T cb;
7340
7341 if (optval == NULL || *optval == NUL)
7342 {
7343 free_callback(optcb);
7344 return OK;
7345 }
7346
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007347 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007348 || (STRNCMP(optval, "function(", 9) == 0)
7349 || (STRNCMP(optval, "funcref(", 8) == 0))
7350 // Lambda expression or a funcref
7351 tv = eval_expr(optval, NULL);
7352 else
7353 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007354 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007355 if (tv == NULL)
7356 return FAIL;
7357
7358 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007359 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007360 {
7361 free_tv(tv);
7362 return FAIL;
7363 }
7364
7365 free_callback(optcb);
7366 set_callback(optcb, &cb);
7367 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007368
7369 // when using Vim9 style "import.funcname" it needs to be expanded to
7370 // "import#funcname".
7371 expand_autload_callback(optcb);
7372
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007373 return OK;
7374#else
7375 return FAIL;
7376#endif
7377}