blob: 4a15c51b9897a33b1122e9b154657dd3889f0385 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
13 * - Put it in the options array below (copy an existing entry).
14 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
16 * - Add a PV_XX entry to the enum below.
17 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
19 * - For a buffer option, add some code to buf_copy_options().
20 * - For a buffer string option, add code to check_buf_options().
21 * - If it's a numeric option, add any necessary bounds checks to do_set().
22 * - If it's a list of flags, add some code in do_set(), search for WW_ALL.
23 * - When adding an option with expansion (P_EXPAND), but with a different
24 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020025 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 * options.txt, and any other related places.
27 * - Add an entry in runtime/optwin.vim.
28 * When making changes:
29 * - Adjust the help for the option in doc/option.txt.
30 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
31 * comment at the help for the 'compatible' option.
32 */
33
34#define IN_OPTION_C
35#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020036#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010039static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020040static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010041static char_u *option_expand(int opt_idx, char_u *val);
42static void didset_options(void);
43static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000044#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000046#else
47# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
48#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010049static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
50static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020051static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020053static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010054static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010055static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
57static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020058static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000059static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020061static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000062static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010063static void check_winopt(winopt_T *wop);
64static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010065static void paste_option_changed(void);
66static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/*
69 * Initialize the options, first part.
70 *
71 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010072 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000073 */
74 void
Bram Moolenaar07268702018-03-01 21:57:32 +010075set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
77 char_u *p;
78 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000079 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81#ifdef FEAT_LANGMAP
82 langmap_init();
83#endif
84
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010085 // Be Vi compatible by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 p_cp = TRUE;
87
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010088 // Use POSIX compatibility when $VIM_POSIX is set.
Bram Moolenaar4399ef42005-02-12 14:29:27 +000089 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +000090 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +000091 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020092 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000093 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000094
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 /*
96 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +000097 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 */
Bram Moolenaar7c626922005-02-07 22:01:03 +000099 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100100#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100102 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000104 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200105#if defined(MSWIN)
106 {
107 // For MS-Windows put the path in quotes instead of escaping spaces.
108 char_u *cmd;
109 size_t len;
110
111 if (vim_strchr(p, ' ') != NULL)
112 {
113 len = STRLEN(p) + 3; // two quotes and a trailing NUL
114 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200115 if (cmd != NULL)
116 {
117 vim_snprintf((char *)cmd, len, "\"%s\"", p);
118 set_string_default("sh", cmd);
119 vim_free(cmd);
120 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200121 }
122 else
123 set_string_default("sh", p);
124 }
125#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100126 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_WILDIGN
130 /*
131 * Set the default for 'backupskip' to include environment variables for
132 * temp files.
133 */
134 {
135# ifdef UNIX
136 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
137# else
138 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
139# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000140 int len;
141 garray_T ga;
142 int mustfree;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200143 char_u *item;
144
145 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
147 ga_init2(&ga, 1, 100);
K.Takataeeec2542021-06-02 13:28:16 +0200148 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000150 mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# ifdef UNIX
152 if (*names[n] == NUL)
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200153# ifdef MACOS_X
154 p = (char_u *)"/private/tmp";
155# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 p = (char_u *)"/tmp";
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 else
159# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000160 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (p != NULL && *p != NUL)
162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100163 // First time count the NUL, otherwise count the ','.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000164 len = (int)STRLEN(p) + 3;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200165 item = alloc(len);
166 STRCPY(item, p);
167 add_pathsep(item);
168 STRCAT(item, "*");
169 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
170 == NULL
171 && ga_grow(&ga, len) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 if (ga.ga_len > 0)
174 STRCAT(ga.ga_data, ",");
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200175 STRCAT(ga.ga_data, item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 ga.ga_len += len;
177 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200178 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 if (mustfree)
181 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 }
183 if (ga.ga_data != NULL)
184 {
185 set_string_default("bsk", ga.ga_data);
186 vim_free(ga.ga_data);
187 }
188 }
189#endif
190
191 /*
192 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
193 */
194 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000195 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000197#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
198 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
199#endif
200 {
ichizok02560422022-04-05 14:18:44 +0100201#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100202 // Use amount of memory available at this moment.
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200203 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100204#elif defined(HAVE_TOTAL_MEM)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100205 // Use amount of memory available to Vim.
Bram Moolenaar914572a2007-05-01 11:37:47 +0000206 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100207#else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000208 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000211 opt_idx = findoption((char_u *)"maxmem");
212 if (opt_idx >= 0)
213 {
214#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100215 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
216 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000217#endif
218 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
219 }
220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 }
222
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#ifdef FEAT_SEARCHPATH
224 {
225 char_u *cdpath;
226 char_u *buf;
227 int i;
228 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000229 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100231 // Initialize the 'cdpath' option's default value.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000232 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 if (cdpath != NULL)
234 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200235 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 if (buf != NULL)
237 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100238 buf[0] = ','; // start with ",", current dir first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 j = 1;
240 for (i = 0; cdpath[i] != NUL; ++i)
241 {
242 if (vim_ispathlistsep(cdpath[i]))
243 buf[j++] = ',';
244 else
245 {
246 if (cdpath[i] == ' ' || cdpath[i] == ',')
247 buf[j++] = '\\';
248 buf[j++] = cdpath[i];
249 }
250 }
251 buf[j] = NUL;
252 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000253 if (opt_idx >= 0)
254 {
255 options[opt_idx].def_val[VI_DEFAULT] = buf;
256 options[opt_idx].flags |= P_DEF_ALLOCED;
257 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200258 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100259 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000261 if (mustfree)
262 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 }
264 }
265#endif
266
ichizok02560422022-04-05 14:18:44 +0100267#if defined(FEAT_POSTSCRIPT) && \
268 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100269 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100271# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100273# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100275# elif defined(MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100277# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# endif
280 );
281#endif
282
283#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100284 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100286# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000287 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100288# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
290
ichizok02560422022-04-05 14:18:44 +0100291# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293# endif
294 );
295#endif
296
297 /*
298 * Set all the options (except the terminal options) to their default
299 * value. Also set the global value for local options.
300 */
301 set_options_default(0);
302
matveytadbb1bf2022-02-01 17:26:12 +0000303#ifdef UNIX
304 // Force restricted-mode on for "nologin" or "false" $SHELL
305 p = get_isolated_shell_name();
306 if (p != NULL)
307 {
308 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
309 restricted = TRUE;
310 vim_free(p);
311 }
312#endif
313
Bram Moolenaar07268702018-03-01 21:57:32 +0100314#ifdef CLEAN_RUNTIMEPATH
315 if (clean_arg)
316 {
317 opt_idx = findoption((char_u *)"runtimepath");
318 if (opt_idx >= 0)
319 {
320 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
321 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
322 }
323 opt_idx = findoption((char_u *)"packpath");
324 if (opt_idx >= 0)
325 {
326 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
327 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
328 }
329 }
330#endif
331
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#ifdef FEAT_GUI
333 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100334 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#endif
336
337 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100338 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100339 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 check_buf_options(curbuf);
341 check_win_options(curwin);
342 check_options();
343
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100344 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 didset_options();
346
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000347#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100348 // Use the current chartab for the generic chartab. This is not in
349 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000350 init_spell_chartab();
351#endif
352
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /*
354 * Expand environment variables and things like "~" for the defaults.
355 * If option_expand() returns non-NULL the variable is expanded. This can
356 * only happen for non-indirect options.
357 * Also set the default to the expanded value, so ":set" does not list
358 * them.
359 * Don't set the P_ALLOCED flag, because we don't want to free the
360 * default.
361 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200362 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
364 if ((options[opt_idx].flags & P_GETTEXT)
365 && options[opt_idx].var != NULL)
366 p = (char_u *)_(*(char **)options[opt_idx].var);
367 else
368 p = option_expand(opt_idx, NULL);
369 if (p != NULL && (p = vim_strsave(p)) != NULL)
370 {
371 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100372 // VIMEXP
373 // Defaults for all expanded options are currently the same for Vi
374 // and Vim. When this changes, add some code here! Also need to
375 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (options[opt_idx].flags & P_DEF_ALLOCED)
377 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
378 options[opt_idx].def_val[VI_DEFAULT] = p;
379 options[opt_idx].flags |= P_DEF_ALLOCED;
380 }
381 }
382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386 // Detect use of mlterm.
387 // Mlterm is a terminal emulator akin to xterm that has some special
388 // abilities (bidi namely).
389 // NOTE: mlterm's author is being asked to 'set' a variable
390 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100392 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#endif
394
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200395 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Bram Moolenaar4f974752019-02-17 17:44:42 +0100397# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 /*
399 * If $LANG isn't set, try to get a good value for it. This makes the
400 * right language be used automatically. Don't do this for English.
401 */
402 if (mch_getenv((char_u *)"LANG") == NULL)
403 {
404 char buf[20];
405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100406 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
407 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
408 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
410 (LPTSTR)buf, 20);
411 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
415 STRCPY(buf, "zh_TW");
416 else if (STRNICMP(buf, "chs", 3) == 0
417 || STRNICMP(buf, "zhc", 3) == 0)
418 STRCPY(buf, "zh_CN");
419 else if (STRNICMP(buf, "jp", 2) == 0)
420 STRCPY(buf, "ja");
421 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100423 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425 }
ichizok02560422022-04-05 14:18:44 +0100426# elif defined(MACOS_CONVERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100427 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000428 mac_lang_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429# endif
430
K.Takataf883d902021-05-30 18:04:19 +0200431# ifdef MSWIN
432 // MS-Windows has builtin support for conversion to and from Unicode, using
433 // "utf-8" for 'encoding' should work best for most users.
434 p = vim_strsave((char_u *)ENC_DFLT);
435# else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100436 // enc_locale() will try to find the encoding of the current locale.
K.Takataf883d902021-05-30 18:04:19 +0200437 // This works best for properly configured systems, old and new.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 p = enc_locale();
K.Takataf883d902021-05-30 18:04:19 +0200439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 if (p != NULL)
441 {
442 char_u *save_enc;
443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100444 // Try setting 'encoding' and check if the value is valid.
K.Takataf883d902021-05-30 18:04:19 +0200445 // If not, go back to the default encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 save_enc = p_enc;
447 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000448 if (STRCMP(p_enc, "gb18030") == 0)
449 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100450 // We don't support "gb18030", but "cp936" is a good substitute
451 // for practical purposes, thus use that. It's not an alias to
452 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000453 p_enc = vim_strsave((char_u *)"cp936");
454 vim_free(p);
455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 if (mb_init() == NULL)
457 {
458 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000459 if (opt_idx >= 0)
460 {
461 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
462 options[opt_idx].flags |= P_DEF_ALLOCED;
463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464
Bram Moolenaard0573012017-10-28 21:11:06 +0200465#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100466 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000467 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100468 // Adjust the default for 'isprint' and 'iskeyword' to match
469 // latin1. Also set the defaults for when 'nocompatible' is
470 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000471 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000472 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000473 set_string_option_direct((char_u *)"isk", -1,
474 ISK_LATIN1, OPT_FREE, SID_NONE);
475 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000476 if (opt_idx >= 0)
477 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000478 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000479 if (opt_idx >= 0)
480 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000481 (void)init_chartab();
482 }
483#endif
484
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200485#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100486 // Win32 console: When GetACP() returns a different value from
487 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200488 if (
489# ifdef VIMDLL
490 (!gui.in_use && !gui.starting) &&
491# endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100492 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 {
494 char buf[50];
495
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100496 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
497 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100498 if (GetConsoleCP() == 0)
499 sprintf(buf, "cp%ld", (long)GetACP());
500 else
501 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 p_tenc = vim_strsave((char_u *)buf);
503 if (p_tenc != NULL)
504 {
505 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000506 if (opt_idx >= 0)
507 {
508 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
509 options[opt_idx].flags |= P_DEF_ALLOCED;
510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 convert_setup(&input_conv, p_tenc, p_enc);
512 convert_setup(&output_conv, p_enc, p_tenc);
513 }
514 else
515 p_tenc = empty_option;
516 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100517#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100518#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100519 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000520 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 }
523 else
524 {
525 vim_free(p_enc);
526 p_enc = save_enc;
527 }
528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
530#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100531 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 set_helplang_default(get_mess_lang());
533#endif
534}
535
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200536static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
537
538/*
539 * Set the "fileencodings" option to the default value for when 'encoding' is
540 * utf-8.
541 */
542 void
543set_fencs_unicode()
544{
545 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
546 OPT_FREE, 0);
547}
548
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549/*
550 * Set an option to its default value.
551 * This does not take care of side effects!
552 */
553 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100554set_option_default(
555 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100556 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
557 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100559 char_u *varp; // pointer to variable for current option
560 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000562 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
564
565 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
566 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100567 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
569 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
570 if (flags & P_STRING)
571 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200572 // 'fencs' default value depends on 'encoding'
573 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
574 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100575 // Use set_string_option_direct() for local options to handle
576 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200577 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200578 set_string_option_direct(NULL, opt_idx,
579 options[opt_idx].def_val[dvi], opt_flags, 0);
580 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200582 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
583 free_string_option(*(char_u **)(varp));
584 *(char_u **)varp = options[opt_idx].def_val[dvi];
585 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587 }
588 else if (flags & P_NUM)
589 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000590 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 win_comp_scroll(curwin);
592 else
593 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100594 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
595
596 if ((long *)varp == &curwin->w_p_so
597 || (long *)varp == &curwin->w_p_siso)
598 // 'scrolloff' and 'sidescrolloff' local values have a
599 // different default value than the global default.
600 *(long *)varp = -1;
601 else
602 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100603 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 if (both)
605 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100606 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
608 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100609 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 // the cast to long is required for Manx C, long_i is needed for
612 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000613 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000614#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100615 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000616 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
617 *(int *)varp = FALSE;
618#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100619 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (both)
621 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
622 *(int *)varp;
623 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100625 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000626 flagsp = insecure_flag(opt_idx, opt_flags);
627 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
629
630#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200631 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#endif
633}
634
635/*
636 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200637 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 */
639 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100641 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000645 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
Bram Moolenaardac13472019-09-16 21:06:21 +0200647 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200648 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200649 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100650 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200651# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200652 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200653 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200654# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100655 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 set_option_default(i, opt_flags, p_cp);
657
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100658 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000659 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200661 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662}
663
664/*
665 * Set the Vi-default value of a string option.
666 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100667 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100669 static void
670set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
672 char_u *p;
673 int opt_idx;
674
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100675 if (escape && vim_strchr(val, ' ') != NULL)
676 p = vim_strsave_escaped(val, (char_u *)" ");
677 else
678 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100679 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {
681 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000682 if (opt_idx >= 0)
683 {
684 if (options[opt_idx].flags & P_DEF_ALLOCED)
685 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
686 options[opt_idx].def_val[VI_DEFAULT] = p;
687 options[opt_idx].flags |= P_DEF_ALLOCED;
688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
690}
691
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100692 void
693set_string_default(char *name, char_u *val)
694{
695 set_string_default_esc(name, val, FALSE);
696}
697
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200699 * For an option value that contains comma separated items, find "newval" in
700 * "origval". Return NULL if not found.
701 */
702 static char_u *
703find_dup_item(char_u *origval, char_u *newval, long_u flags)
704{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200705 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200706 size_t newlen;
707 char_u *s;
708
709 if (origval == NULL)
710 return NULL;
711
712 newlen = STRLEN(newval);
713 for (s = origval; *s != NUL; ++s)
714 {
715 if ((!(flags & P_COMMA)
716 || s == origval
717 || (s[-1] == ',' && !(bs & 1)))
718 && STRNCMP(s, newval, newlen) == 0
719 && (!(flags & P_COMMA)
720 || s[newlen] == ','
721 || s[newlen] == NUL))
722 return s;
723 // Count backslashes. Only a comma with an even number of backslashes
724 // or a single backslash preceded by a comma before it is recognized as
725 // a separator.
726 if ((s > origval + 1
727 && s[-1] == '\\'
728 && s[-2] != ',')
729 || (s == origval + 1
730 && s[-1] == '\\'))
731 ++bs;
732 else
733 bs = 0;
734 }
735 return NULL;
736}
737
738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 * Set the Vi-default value of a number option.
740 * Used for 'lines' and 'columns'.
741 */
742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100743set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000745 int opt_idx;
746
747 opt_idx = findoption((char_u *)name);
748 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000749 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750}
751
Dominique Pelle748b3082022-01-08 12:41:16 +0000752#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200753/*
754 * Set all window-local and buffer-local options to the Vim default.
755 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200756 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200757 */
758 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200759set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200760{
761 win_T *save_curwin = curwin;
762 int i;
763
764 curwin = wp;
765 curbuf = curwin->w_buffer;
766 block_autocmds();
767
Bram Moolenaardac13472019-09-16 21:06:21 +0200768 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200769 {
770 struct vimoption *p = &(options[i]);
771 char_u *varp = get_varp_scope(p, OPT_LOCAL);
772
773 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200774 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200775 && !(options[i].flags & P_NODEFAULT)
776 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200777 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200778 }
779
780 unblock_autocmds();
781 curwin = save_curwin;
782 curbuf = curwin->w_buffer;
783}
Dominique Pelle748b3082022-01-08 12:41:16 +0000784#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200785
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000786#if defined(EXITFREE) || defined(PROTO)
787/*
788 * Free all options.
789 */
790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100791free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000792{
793 int i;
794
Bram Moolenaardac13472019-09-16 21:06:21 +0200795 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000796 {
797 if (options[i].indir == PV_NONE)
798 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100799 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100800 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000801 free_string_option(*(char_u **)options[i].var);
802 if (options[i].flags & P_DEF_ALLOCED)
803 free_string_option(options[i].def_val[VI_DEFAULT]);
804 }
805 else if (options[i].var != VAR_WIN
806 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100807 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200808 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000809 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000810 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000811 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000812}
813#endif
814
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816/*
817 * Initialize the options, part two: After getting Rows and Columns and
818 * setting 'term'.
819 */
820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100821set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000823 int idx;
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100826 * 'scroll' defaults to half the window height. The stored default is zero,
827 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000829 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000830 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000831 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 comp_col();
833
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000834 /*
835 * 'window' is only for backwards compatibility with Vi.
836 * Default is Rows - 1.
837 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000838 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000839 p_window = Rows - 1;
840 set_number_default("window", Rows - 1);
841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100842 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100843#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000844 /*
845 * If 'background' wasn't set by the user, try guessing the value,
846 * depending on the terminal name. Only need to check for terminals
847 * with a dark background, that can handle color.
848 */
849 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000850 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
851 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000853 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 // don't mark it as set, when starting the GUI it may be
855 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000856 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000859
860#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000862#endif
863#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000865#endif
866#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100867 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869}
870
871/*
872 * Initialize the options, part three: After reading the .vimrc
873 */
874 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100875set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100877#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878/*
879 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
880 * This is done after other initializations, where 'shell' might have been
881 * set, but only if they have not been set before.
882 */
883 char_u *p;
884 int idx_srr;
885 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100886# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 int idx_sp;
888 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
891 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000892 if (idx_srr < 0)
893 do_srr = FALSE;
894 else
895 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100896# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000898 if (idx_sp < 0)
899 do_sp = FALSE;
900 else
901 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100902# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200903 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (p != NULL)
905 {
906 /*
907 * Default for p_sp is "| tee", for p_srr is ">".
908 * For known shells it is changed here to include stderr.
909 */
910 if ( fnamecmp(p, "csh") == 0
911 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100912# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 || fnamecmp(p, "csh.exe") == 0
914 || fnamecmp(p, "tcsh.exe") == 0
915# endif
916 )
917 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100918# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (do_sp)
920 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100921# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100923# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
927 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (do_srr)
930 {
931 p_srr = (char_u *)">&";
932 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
933 }
934 }
Mike Williams12795022021-06-28 20:53:58 +0200935# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200936 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
937 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200938 else if ( fnamecmp(p, "powershell") == 0
939 || fnamecmp(p, "powershell.exe") == 0
940 )
941 {
942# if defined(FEAT_QUICKFIX)
943 if (do_sp)
944 {
945 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
946 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
947 }
948# endif
949 if (do_srr)
950 {
951 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
952 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
953 }
954 }
955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 else
Natanael Copa56318362021-05-06 18:46:35 +0200957 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if ( fnamecmp(p, "sh") == 0
959 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200960 || fnamecmp(p, "mksh") == 0
961 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000963 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200965 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200966 || fnamecmp(p, "ash") == 0
967 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200968 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100969# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 || fnamecmp(p, "cmd") == 0
971 || fnamecmp(p, "sh.exe") == 0
972 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200973 || fnamecmp(p, "mksh.exe") == 0
974 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000976 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 || fnamecmp(p, "bash.exe") == 0
978 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200979 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200980 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100982 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100984# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (do_sp)
986 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100987# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100989# else
Mike Williamsa3d1b292021-06-30 20:56:00 +0200990 if (fnamecmp(p, "pwsh") == 0)
991 p_sp = (char_u *)">%s 2>&1";
992 else
993 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
996 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (do_srr)
999 {
1000 p_srr = (char_u *)">%s 2>&1";
1001 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1002 }
1003 }
1004 vim_free(p);
1005 }
1006#endif
1007
Bram Moolenaar4f974752019-02-17 17:44:42 +01001008#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001010 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1011 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001013 * set, but only if they have not been set before.
1014 * Default values depend on shell (cmd.exe is default shell):
1015 *
1016 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001017 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001018 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001019 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001020 * "sh" like shells - "-c" "\""
1021 *
1022 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 */
Mike Williams12795022021-06-28 20:53:58 +02001024 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1025 {
1026 int idx_opt;
1027
1028 idx_opt = findoption((char_u *)"shcf");
1029 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1030 {
1031 p_shcf = (char_u*)"-Command";
1032 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1033 }
1034
1035 idx_opt = findoption((char_u *)"sxq");
1036 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1037 {
1038 p_sxq = (char_u*)"\"";
1039 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1040 }
1041 }
1042 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 int idx3;
1045
1046 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001047 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
1049 p_shcf = (char_u *)"-c";
1050 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1051 }
1052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001053 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001055 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
1057 p_sxq = (char_u *)"\"";
1058 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001061 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1062 {
1063 int idx3;
1064
1065 /*
1066 * cmd.exe on Windows will strip the first and last double quote given
1067 * on the command line, e.g. most of the time things like:
1068 * cmd /c "my path/to/echo" "my args to echo"
1069 * become:
1070 * my path/to/echo" "my args to echo
1071 * when executed.
1072 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001073 * To avoid this, set shellxquote to surround the command in
1074 * parenthesis. This appears to make most commands work, without
1075 * breaking commands that worked previously, such as
1076 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001077 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001078 idx3 = findoption((char_u *)"sxq");
1079 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1080 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001081 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001082 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1083 }
1084
Bram Moolenaara64ba222012-02-12 23:23:31 +01001085 idx3 = findoption((char_u *)"shcf");
1086 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1087 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001088 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001089 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1090 }
1091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#endif
1093
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001094 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001095 {
1096 int idx_ffs = findoption((char_u *)"ffs");
1097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001098 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001099 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1100 set_fileformat(default_fileformat(), OPT_LOCAL);
1101 }
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104}
1105
1106#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1107/*
1108 * When 'helplang' is still at its default value, set it to "lang".
1109 * Only the first two characters of "lang" are used.
1110 */
1111 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001112set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
1114 int idx;
1115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001116 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 return;
1118 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001119 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
1121 if (options[idx].flags & P_ALLOCED)
1122 free_string_option(p_hlg);
1123 p_hlg = vim_strsave(lang);
1124 if (p_hlg == NULL)
1125 p_hlg = empty_option;
1126 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001127 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001128 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001129 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1130 {
1131 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1132 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1133 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001134 // any C like setting, such as C.UTF-8, becomes "en"
1135 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1136 {
1137 p_hlg[0] = 'e';
1138 p_hlg[1] = 'n';
1139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 options[idx].flags |= P_ALLOCED;
1143 }
1144}
1145#endif
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147/*
1148 * 'title' and 'icon' only default to true if they have not been set or reset
1149 * in .vimrc and we can read the old value.
1150 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1151 * they can be reset. This reduces startup time when using X on a remote
1152 * machine.
1153 */
1154 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001155set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
1157 int idx1;
1158 long val;
1159
1160 /*
1161 * If GUI is (going to be) used, we can always set the window title and
1162 * icon name. Saves a bit of time, because the X11 display server does
1163 * not need to be contacted.
1164 */
1165 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001166 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
1168#ifdef FEAT_GUI
1169 if (gui.starting || gui.in_use)
1170 val = TRUE;
1171 else
1172#endif
1173 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001174 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 p_title = val;
1176 }
1177 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001178 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
1180#ifdef FEAT_GUI
1181 if (gui.starting || gui.in_use)
1182 val = TRUE;
1183 else
1184#endif
1185 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001186 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 p_icon = val;
1188 }
1189}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001191 void
1192ex_set(exarg_T *eap)
1193{
1194 int flags = 0;
1195
1196 if (eap->cmdidx == CMD_setlocal)
1197 flags = OPT_LOCAL;
1198 else if (eap->cmdidx == CMD_setglobal)
1199 flags = OPT_GLOBAL;
1200#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001201 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001202 ex_options(eap);
1203 else
1204#endif
1205 {
1206 if (eap->forceit)
1207 flags |= OPT_ONECOLUMN;
1208 (void)do_set(eap->arg, flags);
1209 }
1210}
1211
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212/*
1213 * Parse 'arg' for option settings.
1214 *
1215 * 'arg' may be IObuff, but only when no errors can be present and option
1216 * does not need to be expanded with option_expand().
1217 * "opt_flags":
1218 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001219 * OPT_GLOBAL for ":setglobal"
1220 * OPT_LOCAL for ":setlocal" and a modeline
1221 * OPT_MODELINE for a modeline
1222 * OPT_WINONLY to only set window-local options
1223 * OPT_NOWIN to skip setting window-local options
1224 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 *
1226 * returns FAIL if an error is detected, OK otherwise
1227 */
1228 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001229do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02001230 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001231 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232{
Bram Moolenaar1594f312021-07-08 16:40:13 +02001233 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001235 char *errmsg;
1236 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001238 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1239 int nextchar; // next non-white char after option name
1240 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 int len;
1242 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001243 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001245 long_u flags; // flags for current option
1246 char_u *varp = NULL; // pointer to variable for current option
1247 int did_show = FALSE; // already showed one value
1248 int adding; // "opt+=arg"
1249 int prepending; // "opt^=arg"
1250 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 int cp_val = 0;
1252 char_u key_name[2];
1253
1254 if (*arg == NUL)
1255 {
1256 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001257 did_show = TRUE;
1258 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 }
1260
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001261 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
1263 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001264 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001266 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1267 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
1269 /*
1270 * ":set all" show all options.
1271 * ":set all&" set all options to their default value.
1272 */
1273 arg += 3;
1274 if (*arg == '&')
1275 {
1276 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001277 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001279 didset_options();
1280 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001281 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001286 did_show = TRUE;
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001289 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {
1291 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001292 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001293 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 arg += 7;
1295 }
1296 else
1297 {
1298 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001299 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {
1301 prefix = 0;
1302 arg += 2;
1303 }
1304 else if (STRNCMP(arg, "inv", 3) == 0)
1305 {
1306 prefix = 2;
1307 arg += 3;
1308 }
1309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001310 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 key = 0;
1312 if (*arg == '<')
1313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001315 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1317 len = 5;
1318 else
1319 {
1320 len = 1;
1321 while (arg[len] != NUL && arg[len] != '>')
1322 ++len;
1323 }
1324 if (arg[len] != '>')
1325 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001326 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 goto skip;
1328 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001329 arg[len] = NUL; // put NUL after name
1330 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001332 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001334 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336 else
1337 {
1338 len = 0;
1339 /*
1340 * The two characters after "t_" may not be alphanumeric.
1341 */
1342 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1343 len = 4;
1344 else
1345 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1346 ++len;
1347 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001348 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001350 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001352 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001355 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 afterchar = arg[len];
1357
Bram Moolenaardeb108b2021-07-08 17:35:36 +02001358 if (in_vim9script())
1359 {
1360 char_u *p = skipwhite(arg + len);
1361
1362 // disallow white space before =val, +=val, -=val, ^=val
1363 if (p > arg + len && (p[0] == '='
1364 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1365 && p[1] == '=')))
1366 {
1367 errmsg = e_no_white_space_allowed_between_option_and;
1368 arg = p;
1369 startarg = p;
1370 goto skip;
1371 }
1372 }
1373 else
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001374 // skip white space, allow ":set ai ?", ":set hlsearch !"
1375 while (VIM_ISWHITE(arg[len]))
1376 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
1378 adding = FALSE;
1379 prepending = FALSE;
1380 removing = FALSE;
1381 if (arg[len] != NUL && arg[len + 1] == '=')
1382 {
1383 if (arg[len] == '+')
1384 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001385 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 ++len;
1387 }
1388 else if (arg[len] == '^')
1389 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001390 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 ++len;
1392 }
1393 else if (arg[len] == '-')
1394 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001395 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 ++len;
1397 }
1398 }
1399 nextchar = arg[len];
1400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001401 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 {
Bram Moolenaar1594f312021-07-08 16:40:13 +02001403 if (in_vim9script() && arg > arg_start
1404 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1405 errmsg = e_no_white_space_allowed_between_option_and;
1406 else
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001407 errmsg = e_unknown_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 goto skip;
1409 }
1410
1411 if (opt_idx >= 0)
1412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001413 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001415 // Only give an error message when requesting the value of
1416 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1418 && (!(options[opt_idx].flags & P_BOOL)
1419 || nextchar == '?'))
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001420 errmsg = e_option_not_supported;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 goto skip;
1422 }
1423
1424 flags = options[opt_idx].flags;
1425 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1426 }
1427 else
1428 {
1429 flags = P_STRING;
1430 if (key < 0)
1431 {
1432 key_name[0] = KEY2TERMCAP0(key);
1433 key_name[1] = KEY2TERMCAP1(key);
1434 }
1435 else
1436 {
1437 key_name[0] = KS_KEY;
1438 key_name[1] = (key & 0xff);
1439 }
1440 }
1441
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001442 // Skip all options that are not window-local (used when showing
1443 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001444 if ((opt_flags & OPT_WINONLY)
1445 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1446 goto skip;
1447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001448 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001449 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1450 && options[opt_idx].var == VAR_WIN)
1451 goto skip;
1452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001453 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001454 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001456 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001457 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001458 errmsg = e_not_allowed_in_modeline;
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001459 goto skip;
1460 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001461 if ((flags & P_MLE) && !p_mle)
1462 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001463 errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
Bram Moolenaar110289e2019-05-23 15:38:06 +02001464 goto skip;
1465 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001466#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001467 // In diff mode some options are overruled. This avoids that
1468 // 'foldmethod' becomes "marker" instead of "diff" and that
1469 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001470 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001471 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001472 && (
1473#ifdef FEAT_FOLDING
1474 options[opt_idx].indir == PV_FDM ||
1475#endif
1476 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001477 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 }
1480
1481#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001482 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001483 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001485 errmsg = e_not_allowed_in_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 goto skip;
1487 }
1488#endif
1489
1490 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1491 {
1492 arg += len;
1493 cp_val = p_cp;
1494 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1495 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001496 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
1498 cp_val = FALSE;
1499 arg += 3;
1500 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001501 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
1503 cp_val = TRUE;
1504 arg += 2;
1505 }
1506 }
1507 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001508 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001510 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 goto skip;
1512 }
1513 }
1514
1515 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001516 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001517 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 */
1519 if (nextchar == '?'
1520 || (prefix == 1
1521 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1522 && !(flags & P_BOOL)))
1523 {
1524 /*
1525 * print value
1526 */
1527 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001528 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 else
1530 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001531 gotocmdline(TRUE); // cursor at status line
1532 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534 if (opt_idx >= 0)
1535 {
1536 showoneopt(&options[opt_idx], opt_flags);
1537#ifdef FEAT_EVAL
1538 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001539 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001540 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001541 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001542 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001543 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001544 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001545 (int)options[opt_idx].indir & PV_MASK]);
1546 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001547 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001548 (int)options[opt_idx].indir & PV_MASK]);
1549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550#endif
1551 }
1552 else
1553 {
1554 char_u *p;
1555
1556 p = find_termcode(key_name);
1557 if (p == NULL)
1558 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001559 errmsg = e_key_code_not_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 goto skip;
1561 }
1562 else
1563 (void)show_one_termcode(key_name, p, TRUE);
1564 }
1565 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001566 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001567 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
1569 else
1570 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001571 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001572 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001573
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001574 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 {
1576 if (nextchar == '=' || nextchar == ':')
1577 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001578 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 goto skip;
1580 }
1581
1582 /*
1583 * ":set opt!": invert
1584 * ":set opt&": reset to default value
1585 * ":set opt<": reset to global value
1586 */
1587 if (nextchar == '!')
1588 value = *(int *)(varp) ^ 1;
1589 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001590 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 ((flags & P_VI_DEF) || cp_val)
1592 ? VI_DEFAULT : VIM_DEFAULT];
1593 else if (nextchar == '<')
1594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001595 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if ((int *)varp == &curbuf->b_p_ar
1597 && opt_flags == OPT_LOCAL)
1598 value = -1;
1599 else
1600 value = *(int *)get_varp_scope(&(options[opt_idx]),
1601 OPT_GLOBAL);
1602 }
1603 else
1604 {
1605 /*
1606 * ":set invopt": invert
1607 * ":set opt" or ":set noopt": set or reset
1608 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001609 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001611 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 goto skip;
1613 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001614 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 value = *(int *)(varp) ^ 1;
1616 else
1617 value = prefix;
1618 }
1619
1620 errmsg = set_bool_option(opt_idx, varp, (int)value,
1621 opt_flags);
1622 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001623 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1626 || prefix != 1)
1627 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001628 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 goto skip;
1630 }
1631
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001632 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
1634 /*
1635 * Different ways to set a number option:
1636 * & set to default value
1637 * < set to global value
1638 * <xx> accept special key codes for 'wildchar'
1639 * c accept any non-digit for 'wildchar'
1640 * [-]0-9 set number
1641 * other error
1642 */
1643 ++arg;
1644 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001645 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 ((flags & P_VI_DEF) || cp_val)
1647 ? VI_DEFAULT : VIM_DEFAULT];
1648 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001649 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001650 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1651 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001652 if ((long *)varp == &curbuf->b_p_ul
1653 && opt_flags == OPT_LOCAL)
1654 value = NO_LOCAL_UNDOLEVEL;
1655 else
1656 value = *(long *)get_varp_scope(
1657 &(options[opt_idx]), OPT_GLOBAL);
1658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 else if (((long *)varp == &p_wc
1660 || (long *)varp == &p_wcm)
1661 && (*arg == '<'
1662 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001663 || (*arg != NUL
1664 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 && !VIM_ISDIGIT(*arg))))
1666 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001667 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if (value == 0 && (long *)varp != &p_wcm)
1669 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001670 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 goto skip;
1672 }
1673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1675 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001676 // Allow negative (for 'undolevels'), octal and
1677 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001678 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001679 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001680 if (i == 0 || (arg[i] != NUL
1681 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001683 errmsg = e_number_required_after_equal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 goto skip;
1685 }
1686 }
1687 else
1688 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001689 errmsg = e_number_required_after_equal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 goto skip;
1691 }
1692
1693 if (adding)
1694 value = *(long *)varp + value;
1695 if (prepending)
1696 value = *(long *)varp * value;
1697 if (removing)
1698 value = *(long *)varp - value;
1699 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001700 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001702 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001704 char_u *save_arg = NULL;
1705 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001706 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001707 char_u *newval;
1708 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001709 char_u *origval_l = NULL;
1710 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001711#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001712 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001713 char_u *saved_origval_l = NULL;
1714 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001715 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001716#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001717 unsigned newlen;
1718 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001719 int new_value_alloced; // new string option
1720 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001722 // When using ":set opt=val" for a global option
1723 // with a local value the local value will be
1724 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001726 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 varp = options[opt_idx].var;
1728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001729 // The old value is kept until we are sure that the
1730 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001732
Bram Moolenaard7c96872019-06-15 17:12:48 +02001733 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1734 {
1735 origval_l = *(char_u **)get_varp_scope(
1736 &(options[opt_idx]), OPT_LOCAL);
1737 origval_g = *(char_u **)get_varp_scope(
1738 &(options[opt_idx]), OPT_GLOBAL);
1739
1740 // A global-local string option might have an empty
1741 // option as value to indicate that the global
1742 // value should be used.
1743 if (((int)options[opt_idx].indir & PV_BOTH)
1744 && origval_l == empty_option)
1745 origval_l = origval_g;
1746 }
1747
1748 // When setting the local value of a global
1749 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001750 if (((int)options[opt_idx].indir & PV_BOTH)
1751 && (opt_flags & OPT_LOCAL))
1752 origval = *(char_u **)get_varp(
1753 &options[opt_idx]);
1754 else
1755 origval = oldval;
1756
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001757 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
1759 newval = options[opt_idx].def_val[
1760 ((flags & P_VI_DEF) || cp_val)
1761 ? VI_DEFAULT : VIM_DEFAULT];
1762 if ((char_u **)varp == &p_bg)
1763 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001764 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#ifdef FEAT_GUI
1766 if (gui.in_use)
1767 newval = gui_bg_default();
1768 else
1769#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001770 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001772 else if ((char_u **)varp == &p_fencs && enc_utf8)
1773 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001775 // expand environment variables and ~ since the
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001776 // default value was already expanded, only
1777 // required when an environment variable was set
1778 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 if (newval == NULL)
1780 newval = empty_option;
1781 else
1782 {
1783 s = option_expand(opt_idx, newval);
1784 if (s == NULL)
1785 s = newval;
1786 newval = vim_strsave(s);
1787 }
1788 new_value_alloced = TRUE;
1789 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001790 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
1792 newval = vim_strsave(*(char_u **)get_varp_scope(
1793 &(options[opt_idx]), OPT_GLOBAL));
1794 new_value_alloced = TRUE;
1795 }
1796 else
1797 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001798 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
1800 /*
1801 * Set 'keywordprg' to ":help" if an empty
1802 * value was passed to :set by the user.
1803 * Misuse errbuf[] for the resulting string.
1804 */
1805 if (varp == (char_u *)&p_kp
1806 && (*arg == NUL || *arg == ' '))
1807 {
1808 STRCPY(errbuf, ":help");
1809 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001810 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001813 * Convert 'backspace' number to string, for
1814 * adding, prepending and removing string.
1815 */
1816 else if (varp == (char_u *)&p_bs
1817 && VIM_ISDIGIT(**(char_u **)varp))
1818 {
1819 i = getdigits((char_u **)varp);
1820 switch (i)
1821 {
1822 case 0:
1823 *(char_u **)varp = empty_option;
1824 break;
1825 case 1:
1826 *(char_u **)varp = vim_strsave(
1827 (char_u *)"indent,eol");
1828 break;
1829 case 2:
1830 *(char_u **)varp = vim_strsave(
1831 (char_u *)"indent,eol,start");
1832 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001833 case 3:
1834 *(char_u **)varp = vim_strsave(
1835 (char_u *)"indent,eol,nostop");
1836 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001837 }
1838 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001839 if (origval == oldval)
1840 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001841 if (origval_l == oldval)
1842 origval_l = *(char_u **)varp;
1843 if (origval_g == oldval)
1844 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001845 oldval = *(char_u **)varp;
1846 }
1847 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 * Convert 'whichwrap' number to string, for
1849 * backwards compatibility with Vim 3.0.
1850 * Misuse errbuf[] for the resulting string.
1851 */
1852 else if (varp == (char_u *)&p_ww
1853 && VIM_ISDIGIT(*arg))
1854 {
1855 *errbuf = NUL;
1856 i = getdigits(&arg);
1857 if (i & 1)
1858 STRCAT(errbuf, "b,");
1859 if (i & 2)
1860 STRCAT(errbuf, "s,");
1861 if (i & 4)
1862 STRCAT(errbuf, "h,l,");
1863 if (i & 8)
1864 STRCAT(errbuf, "<,>,");
1865 if (i & 16)
1866 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001867 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 errbuf[STRLEN(errbuf) - 1] = NUL;
1869 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001870 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 /*
1873 * Remove '>' before 'dir' and 'bdir', for
1874 * backwards compatibility with version 3.0
1875 */
1876 else if ( *arg == '>'
1877 && (varp == (char_u *)&p_dir
1878 || varp == (char_u *)&p_bdir))
1879 {
1880 ++arg;
1881 }
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 /*
1884 * Copy the new string into allocated memory.
1885 * Can't use set_string_option_direct(), because
1886 * we need to remove the backslashes.
1887 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001888 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 newlen = (unsigned)STRLEN(arg) + 1;
1890 if (adding || prepending || removing)
1891 newlen += (unsigned)STRLEN(origval) + 1;
1892 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001893 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 break;
1895 s = newval;
1896
1897 /*
1898 * Copy the string, skip over escaped chars.
1899 * For MS-DOS and WIN32 backslashes before normal
1900 * file name characters are not removed, and keep
1901 * backslash at start, for "\\machine\path", but
1902 * do remove it for "\\\\machine\\path".
1903 * The reverse is found in ExpandOldSetting().
1904 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001905 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
1907 if (*arg == '\\' && arg[1] != NUL
1908#ifdef BACKSLASH_IN_FILENAME
1909 && !((flags & P_EXPAND)
1910 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001911 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 && (arg[1] != '\\'
1913 || (s == newval
1914 && arg[2] != '\\')))
1915#endif
1916 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001917 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001919 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001921 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 mch_memmove(s, arg, (size_t)i);
1923 arg += i;
1924 s += i;
1925 }
1926 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 *s++ = *arg++;
1928 }
1929 *s = NUL;
1930
1931 /*
1932 * Expand environment variables and ~.
1933 * Don't do it when adding without inserting a
1934 * comma.
1935 */
1936 if (!(adding || prepending || removing)
1937 || (flags & P_COMMA))
1938 {
1939 s = option_expand(opt_idx, newval);
1940 if (s != NULL)
1941 {
1942 vim_free(newval);
1943 newlen = (unsigned)STRLEN(s) + 1;
1944 if (adding || prepending || removing)
1945 newlen += (unsigned)STRLEN(origval) + 1;
1946 newval = alloc(newlen);
1947 if (newval == NULL)
1948 break;
1949 STRCPY(newval, s);
1950 }
1951 }
1952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001953 // locate newval[] in origval[] when removing it
1954 // and when adding to avoid duplicates
1955 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (removing || (flags & P_NODUP))
1957 {
1958 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001959 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001961 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001962 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
1964 prepending = FALSE;
1965 adding = FALSE;
1966 STRCPY(newval, origval);
1967 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001968
1969 // if no duplicate, move pointer to end of
1970 // original value
1971 if (s == NULL)
1972 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 }
1974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001975 // concatenate the two strings; add a ',' if
1976 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 if (adding || prepending)
1978 {
1979 comma = ((flags & P_COMMA) && *origval != NUL
1980 && *newval != NUL);
1981 if (adding)
1982 {
1983 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001984 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001985 if (comma && i > 1
1986 && (flags & P_ONECOMMA) == P_ONECOMMA
1987 && origval[i - 1] == ','
1988 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001989 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 mch_memmove(newval + i + comma, newval,
1991 STRLEN(newval) + 1);
1992 mch_memmove(newval, origval, (size_t)i);
1993 }
1994 else
1995 {
1996 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001997 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 if (comma)
2000 newval[i] = ',';
2001 }
2002
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002003 // Remove newval[] from origval[]. (Note: "i" has
2004 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 if (removing)
2006 {
2007 STRCPY(newval, origval);
2008 if (*s)
2009 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002010 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if (flags & P_COMMA)
2012 {
2013 if (s == origval)
2014 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002015 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (s[i] == ',')
2017 ++i;
2018 }
2019 else
2020 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002021 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 --s;
2023 ++i;
2024 }
2025 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002026 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
2028 }
2029
2030 if (flags & P_FLAGLIST)
2031 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002032 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002033 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002034 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002035 // if options have P_FLAGLIST and
2036 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002037 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002039 if (*s != ',' && *(s + 1) == ','
2040 && vim_strchr(s + 2, *s) != NULL)
2041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002042 // Remove the duplicated value and
2043 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002044 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002045 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002048 else
2049 {
2050 if ((!(flags & P_COMMA) || *s != ',')
2051 && vim_strchr(s + 1, *s) != NULL)
2052 {
2053 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002054 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002055 }
2056 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002057 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002061 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 arg = save_arg;
2063 new_value_alloced = TRUE;
2064 }
2065
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002066 /*
2067 * Set the new value.
2068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 *(char_u **)(varp) = newval;
2070
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002071#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002072 if (!starting
2073# ifdef FEAT_CRYPT
2074 && options[opt_idx].indir != PV_KEY
2075# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002076 && origval != NULL && newval != NULL)
2077 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002078 // origval may be freed by
2079 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002080 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002081 // newval (and varp) may become invalid if the
2082 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002083 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002084 if (origval_l != NULL)
2085 saved_origval_l = vim_strsave(origval_l);
2086 if (origval_g != NULL)
2087 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002088 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002089#endif
2090
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002091 {
2092 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002093 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002094
2095 // When an option is set in the sandbox, from a
2096 // modeline or in secure mode, then deal with side
2097 // effects in secure mode. Also when the value was
2098 // set with the P_INSECURE flag and is not
2099 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002100 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002101#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002102 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002103#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002104 || (!value_is_replaced && (*p & P_INSECURE)))
2105 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002106
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002107 // Handle side effects, and set the global value
2108 // for ":set" on local options. Note: when setting
2109 // 'syntax' or 'filetype' autocommands may be
2110 // triggered that can cause havoc.
2111 errmsg = did_set_string_option(
2112 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002113 new_value_alloced, oldval, errbuf,
2114 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002115
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002116 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002119#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002120 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002121 trigger_optionsset_string(
2122 opt_idx, opt_flags, saved_origval,
2123 saved_origval_l, saved_origval_g,
2124 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002125 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002126 vim_free(saved_origval_l);
2127 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002128 vim_free(saved_newval);
2129#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002130 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 if (errmsg != NULL)
2132 goto skip;
2133 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002134 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
2136 char_u *p;
2137
2138 if (nextchar == '&')
2139 {
2140 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002141 errmsg = e_not_found_in_termcap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143 else
2144 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002145 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002146 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (*p == '\\' && p[1] != NUL)
2148 ++p;
2149 nextchar = *p;
2150 *p = NUL;
2151 add_termcode(key_name, arg, FALSE);
2152 *p = nextchar;
2153 }
2154 if (full_screen)
2155 ttest(FALSE);
2156 redraw_all_later(CLEAR);
2157 }
2158 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002159
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002161 did_set_option(
2162 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164
2165skip:
2166 /*
2167 * Advance to next argument.
2168 * - skip until a blank found, taking care of backslashes
2169 * - skip blanks
2170 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2171 */
2172 for (i = 0; i < 2 ; ++i)
2173 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002174 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (*arg++ == '\\' && *arg != NUL)
2176 ++arg;
2177 arg = skipwhite(arg);
2178 if (*arg != '=')
2179 break;
2180 }
2181 }
2182
2183 if (errmsg != NULL)
2184 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002185 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002186 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (i + (arg - startarg) < IOSIZE)
2188 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002189 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 STRCAT(IObuff, ": ");
2191 mch_memmove(IObuff + i, startarg, (arg - startarg));
2192 IObuff[i + (arg - startarg)] = NUL;
2193 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002194 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 trans_characters(IObuff, IOSIZE);
2196
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002197 ++no_wait_return; // wait_return done later
2198 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 --no_wait_return;
2200
2201 return FAIL;
2202 }
2203
2204 arg = skipwhite(arg);
2205 }
2206
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002207theend:
2208 if (silent_mode && did_show)
2209 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002210 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002211 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002212 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002213 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002214 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002215 out_flush();
2216 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002217 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002218 }
2219
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 return OK;
2221}
2222
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002223/*
2224 * Call this when an option has been given a new value through a user command.
2225 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2226 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002227 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002228did_set_option(
2229 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002230 int opt_flags, // possibly with OPT_MODELINE
2231 int new_value, // value was replaced completely
2232 int value_checked) // value was checked to be safe, no need to set the
2233 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002234{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002235 long_u *p;
2236
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002237 options[opt_idx].flags |= P_WAS_SET;
2238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002239 // When an option is set in the sandbox, from a modeline or in secure mode
2240 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2241 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002242 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002243 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002244#ifdef HAVE_SANDBOX
2245 || sandbox != 0
2246#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002247 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002248 *p = *p | P_INSECURE;
2249 else if (new_value)
2250 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002251}
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253/*
2254 * Convert a key name or string into a key value.
2255 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002256 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002258 int
2259string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260{
2261 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002262 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 if (*arg == '^')
2264 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002265 if (multi_byte)
2266 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 return *arg;
2268}
2269
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270/*
2271 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2272 * maketitle() to create and display it.
2273 * When switching the title or icon off, call mch_restore_title() to get
2274 * the old value back.
2275 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002276 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002277did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
2279 if (starting != NO_SCREEN
2280#ifdef FEAT_GUI
2281 && !gui.starting
2282#endif
2283 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287/*
2288 * set_options_bin - called when 'bin' changes value.
2289 */
2290 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002291set_options_bin(
2292 int oldval,
2293 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002294 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
2296 /*
2297 * The option values that are changed when 'bin' changes are
2298 * copied when 'bin is set and restored when 'bin' is reset.
2299 */
2300 if (newval)
2301 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002302 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 if (!(opt_flags & OPT_GLOBAL))
2305 {
2306 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2307 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2308 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2309 curbuf->b_p_et_nobin = curbuf->b_p_et;
2310 }
2311 if (!(opt_flags & OPT_LOCAL))
2312 {
2313 p_tw_nobin = p_tw;
2314 p_wm_nobin = p_wm;
2315 p_ml_nobin = p_ml;
2316 p_et_nobin = p_et;
2317 }
2318 }
2319
2320 if (!(opt_flags & OPT_GLOBAL))
2321 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002322 curbuf->b_p_tw = 0; // no automatic line wrap
2323 curbuf->b_p_wm = 0; // no automatic line wrap
2324 curbuf->b_p_ml = 0; // no modelines
2325 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327 if (!(opt_flags & OPT_LOCAL))
2328 {
2329 p_tw = 0;
2330 p_wm = 0;
2331 p_ml = FALSE;
2332 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002333 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002336 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
2338 if (!(opt_flags & OPT_GLOBAL))
2339 {
2340 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2341 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2342 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2343 curbuf->b_p_et = curbuf->b_p_et_nobin;
2344 }
2345 if (!(opt_flags & OPT_LOCAL))
2346 {
2347 p_tw = p_tw_nobin;
2348 p_wm = p_wm_nobin;
2349 p_ml = p_ml_nobin;
2350 p_et = p_et_nobin;
2351 }
2352 }
2353}
2354
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355/*
2356 * Expand environment variables for some string options.
2357 * These string options cannot be indirect!
2358 * If "val" is NULL expand the current value of the option.
2359 * Return pointer to NameBuff, or NULL when not expanded.
2360 */
2361 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002362option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002364 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2366 return NULL;
2367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002368 // If val is longer than MAXPATHL no meaningful expansion can be done,
2369 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 if (val != NULL && STRLEN(val) > MAXPATHL)
2371 return NULL;
2372
2373 if (val == NULL)
2374 val = *(char_u **)options[opt_idx].var;
2375
2376 /*
2377 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2378 * Escape spaces when expanding 'tags', they are used to separate file
2379 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002380 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 */
2382 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002383 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002384#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002385 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2386#endif
2387 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002388 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 return NULL;
2390
2391 return NameBuff;
2392}
2393
2394/*
2395 * After setting various option values: recompute variables that depend on
2396 * option values.
2397 */
2398 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002399didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002401 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 (void)init_chartab();
2403
Bram Moolenaardac13472019-09-16 21:06:21 +02002404 didset_string_options();
2405
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002406#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002407 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002408 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002409 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002410 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002413 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 (void)check_cedit();
2415#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002416#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002417 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002418 fill_breakat_flags();
2419#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002420 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002421}
2422
2423/*
2424 * More side effects of setting options.
2425 */
2426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002427didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002428{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002429 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002430 (void)highlight_changed();
2431
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002432 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002433 check_opt_wim();
2434
Bram Moolenaareed9d462021-02-15 20:38:25 +01002435 // Parse default for 'listchars'.
2436 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002438 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002439 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002440
2441#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002442 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002443 (void)check_clipboard_option();
2444#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002445#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002446 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002447 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002448 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002449 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451}
2452
2453/*
2454 * Check for string options that are NULL (normally only termcap options).
2455 */
2456 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002457check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
2459 int opt_idx;
2460
2461 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2462 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2463 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2464}
2465
2466/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002467 * Return the option index found by a pointer into term_strings[].
2468 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002470 int
2471get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002473 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2476 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002477 return opt_idx;
2478 return -1; // cannot happen: didn't find it!
2479}
2480
2481/*
2482 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2483 * Return the option index or -1 if not found.
2484 */
2485 int
2486set_term_option_alloced(char_u **p)
2487{
2488 int opt_idx = get_term_opt_idx(p);
2489
2490 if (opt_idx >= 0)
2491 options[opt_idx].flags |= P_ALLOCED;
2492 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493}
2494
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002495#if defined(FEAT_EVAL) || defined(PROTO)
2496/*
2497 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2498 * Return FALSE when it wasn't.
2499 * Return -1 for an unknown option.
2500 */
2501 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002502was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002503{
2504 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002505 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002506
2507 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002508 {
2509 flagp = insecure_flag(idx, opt_flags);
2510 return (*flagp & P_INSECURE) != 0;
2511 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002512 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002513 return -1;
2514}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002515
2516/*
2517 * Get a pointer to the flags used for the P_INSECURE flag of option
2518 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002519 * NOTE: Caller must make sure that "curwin" is set to the window from which
2520 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002521 */
2522 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002523insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002524{
2525 if (opt_flags & OPT_LOCAL)
2526 switch ((int)options[opt_idx].indir)
2527 {
2528#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002529 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002530#endif
2531#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002532# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002533 case PV_FDE: return &curwin->w_p_fde_flags;
2534 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002535# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002536# ifdef FEAT_BEVAL
2537 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2538# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002539 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002540 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002541# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002542 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002543# endif
2544#endif
2545 }
2546
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002547 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002548 return &options[opt_idx].flags;
2549}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002550#endif
2551
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002552/*
2553 * Redraw the window title and/or tab page text later.
2554 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002555void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002556{
2557 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002558 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002559}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002562 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2563 * characters or characters in "allowed".
2564 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002565 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002566valid_name(char_u *val, char *allowed)
2567{
2568 char_u *s;
2569
2570 for (s = val; *s != NUL; ++s)
2571 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2572 return FALSE;
2573 return TRUE;
2574}
2575
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002576#if defined(FEAT_EVAL) || defined(PROTO)
2577/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002578 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002579 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002580 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002581 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002582set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002583{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002584 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2585 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002586 sctx_T new_script_ctx = script_ctx;
2587
Bram Moolenaar51258742020-05-03 17:19:33 +02002588 // Modeline already has the line number set.
2589 if (!(opt_flags & OPT_MODELINE))
2590 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002591
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002592 // Remember where the option was set. For local options need to do that
2593 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002594 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002595 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002596 if (both || (opt_flags & OPT_LOCAL))
2597 {
2598 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002599 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002600 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002601 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002602 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002603 if (both)
2604 // also setting the "all buffers" value
2605 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2606 new_script_ctx;
2607 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002608 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002609}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002610
2611/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002612 * Get the script context of global option "name".
2613 *
2614 */
2615 sctx_T *
2616get_option_sctx(char *name)
2617{
2618 int idx = findoption((char_u *)name);
2619
2620 if (idx >= 0)
2621 return &options[idx].script_ctx;
2622 siemsg("no such option: %s", name);
2623 return NULL;
2624}
2625
2626/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002627 * Set the script_ctx for a termcap option.
2628 * "name" must be the two character code, e.g. "RV".
2629 * When "name" is NULL use "opt_idx".
2630 */
2631 void
2632set_term_option_sctx_idx(char *name, int opt_idx)
2633{
2634 char_u buf[5];
2635 int idx;
2636
2637 if (name == NULL)
2638 idx = opt_idx;
2639 else
2640 {
2641 buf[0] = 't';
2642 buf[1] = '_';
2643 buf[2] = name[0];
2644 buf[3] = name[1];
2645 buf[4] = 0;
2646 idx = findoption(buf);
2647 }
2648 if (idx >= 0)
2649 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2650}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002651#endif
2652
Bram Moolenaarf4140482020-02-15 23:06:45 +01002653#if defined(FEAT_EVAL)
2654/*
2655 * Apply the OptionSet autocommand.
2656 */
2657 static void
2658apply_optionset_autocmd(
2659 int opt_idx,
2660 long opt_flags,
2661 long oldval,
2662 long oldval_g,
2663 long newval,
2664 char *errmsg)
2665{
2666 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2667
2668 // Don't do this while starting up, failure or recursively.
2669 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2670 return;
2671
2672 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2673 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2674 oldval_g);
2675 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2676 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2677 (opt_flags & OPT_LOCAL) ? "local" : "global");
2678 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2679 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2680 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2681 if (opt_flags & OPT_LOCAL)
2682 {
2683 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2684 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2685 }
2686 if (opt_flags & OPT_GLOBAL)
2687 {
2688 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2689 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2690 }
2691 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2692 {
2693 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2694 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2695 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2696 }
2697 if (opt_flags & OPT_MODELINE)
2698 {
2699 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2700 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2701 }
2702 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2703 NULL, FALSE, NULL);
2704 reset_v_option_vars();
2705}
2706#endif
2707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708/*
2709 * Set the value of a boolean option, and take care of side effects.
2710 * Returns NULL for success, or an error message for an error.
2711 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002712 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002713set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002714 int opt_idx, // index in options[] table
2715 char_u *varp, // pointer to the option variable
2716 int value, // new value
2717 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
2719 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002720#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002721 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002722#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002723 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 if ((secure
2727#ifdef HAVE_SANDBOX
2728 || sandbox != 0
2729#endif
2730 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002731 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002733#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002734 // Save the global value before changing anything. This is needed as for
2735 // a global-only option setting the "local value" in fact sets the global
2736 // value (since there is only one value).
2737 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2738 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2739 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002740#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002741
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002742 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002744 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002745 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746#endif
2747
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002748#ifdef FEAT_GUI
2749 need_mouse_correct = TRUE;
2750#endif
2751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002752 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2754 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2755
2756 /*
2757 * Handle side effects of changing a bool option.
2758 */
2759
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002760 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
Bram Moolenaar920694c2016-08-21 17:45:02 +02002764#ifdef FEAT_LANGMAP
2765 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002766 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002767 p_lnr = !p_lrm;
2768 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002770 p_lrm = !p_lnr;
2771#endif
2772
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002773#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002775 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2776 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002777 // Only take action when the option was set. When reset we do not
2778 // delete the undo file, the option may be set again without making
2779 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002780 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002781 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002782 char_u hash[UNDO_HASH_SIZE];
2783 buf_T *save_curbuf = curbuf;
2784
Bram Moolenaar29323592016-07-24 22:04:11 +02002785 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002786 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002787 // When 'undofile' is set globally: for every buffer, otherwise
2788 // only for the current buffer: Try to read in the undofile,
2789 // if one exists, the buffer wasn't changed and the buffer was
2790 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002791 if ((curbuf == save_curbuf
2792 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2793 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2794 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002795#ifdef FEAT_CRYPT
2796 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2797 continue;
2798#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002799 u_compute_hash(hash);
2800 u_read_undo(NULL, hash, curbuf->b_fname);
2801 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002802 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002803 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002804 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002805 }
2806#endif
2807
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 else if ((int *)varp == &curbuf->b_p_ro)
2809 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002810 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2812 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002814 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002815 if (curbuf->b_p_ro)
2816 curbuf->b_did_warn = FALSE;
2817
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002818 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
2820
Bram Moolenaar9c449722010-07-20 18:44:27 +02002821#ifdef FEAT_GUI
2822 else if ((int *)varp == &p_mh)
2823 {
2824 if (!p_mh)
2825 gui_mch_mousehide(FALSE);
2826 }
2827#endif
2828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002829 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002831 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002832# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002833 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002834 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2835 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002836 {
2837 curbuf->b_p_ma = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002838 return e_cannot_make_terminal_with_running_job_modifiable;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002839 }
2840# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002841 redraw_titles();
2842 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002843 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002845 {
2846 redraw_titles();
2847 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002848 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002849 else if ((int *)varp == &curbuf->b_p_fixeol)
2850 {
2851 redraw_titles();
2852 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002854 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002855 {
2856 redraw_titles();
2857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 else if ((int *)varp == &curbuf->b_p_bin)
2861 {
2862 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002863 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
2865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002866 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2868 {
2869 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2870 NULL, NULL, TRUE, curbuf);
2871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002873 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 else if ((int *)varp == &curbuf->b_p_swf)
2875 {
2876 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002879 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2880 // buf->b_p_swf
2881 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
2883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002884 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 else if ((int *)varp == &p_terse)
2886 {
2887 char_u *p;
2888
2889 p = vim_strchr(p_shm, SHM_SEARCH);
2890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002891 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 if (p_terse && p == NULL)
2893 {
2894 STRCPY(IObuff, p_shm);
2895 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002896 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002898 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002900 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
2902
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002903 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 else if ((int *)varp == &p_paste)
2905 {
2906 paste_option_changed();
2907 }
2908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 else if ((int *)varp == &p_im)
2911 {
2912 if (p_im)
2913 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002914 if ((State & MODE_INSERT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 need_start_insertmode = TRUE;
2916 stop_insert_mode = FALSE;
2917 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002918 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002919 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 {
2921 need_start_insertmode = FALSE;
2922 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002923 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002924 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 restart_edit = 0;
2926 }
2927 }
2928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002929 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 else if ((int *)varp == &p_ic && p_hls)
2931 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002932 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
2934
2935#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002936 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 else if ((int *)varp == &p_hls)
2938 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002939 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 }
2941#endif
2942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002943 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2944 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 else if ((int *)varp == &curwin->w_p_scb)
2946 {
2947 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002950 curwin->w_scbind_pos = curwin->w_topline;
2951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
Bram Moolenaar4033c552017-09-16 20:54:51 +02002954#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 else if ((int *)varp == &curwin->w_p_pvw)
2957 {
2958 if (curwin->w_p_pvw)
2959 {
2960 win_T *win;
2961
Bram Moolenaar29323592016-07-24 22:04:11 +02002962 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 if (win->w_p_pvw && win != curwin)
2964 {
2965 curwin->w_p_pvw = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002966 return e_preview_window_already_exists;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 }
2968 }
2969 }
2970#endif
2971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 else if ((int *)varp == &curbuf->b_p_tx)
2974 {
2975 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2976 }
2977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002978 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 set_string_option_direct((char_u *)"ffs", -1,
2982 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002983 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986 /*
2987 * When 'lisp' option changes include/exclude '-' in
2988 * keyword characters.
2989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2991 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002992 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002995 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002996 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002998 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 else if ((int *)varp == &curbuf->b_changed)
3002 {
3003 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003004 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003005 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 }
3008
3009#ifdef BACKSLASH_IN_FILENAME
3010 else if ((int *)varp == &p_ssl)
3011 {
3012 if (p_ssl)
3013 {
3014 psepc = '/';
3015 psepcN = '\\';
3016 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 }
3018 else
3019 {
3020 psepc = '\\';
3021 psepcN = '/';
3022 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 }
3024
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003025 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 buflist_slash_adjust();
3027 alist_slash_adjust();
3028# ifdef FEAT_EVAL
3029 scriptnames_slash_adjust();
3030# endif
3031 }
3032#endif
3033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003034 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 else if ((int *)varp == &curwin->w_p_wrap)
3036 {
3037 if (curwin->w_p_wrap)
3038 curwin->w_leftcol = 0;
3039 }
3040
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 else if ((int *)varp == &p_ea)
3042 {
3043 if (p_ea && !old_value)
3044 win_equal(curwin, FALSE, 0);
3045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046
3047 else if ((int *)varp == &p_wiv)
3048 {
3049 /*
3050 * When 'weirdinvert' changed, set/reset 't_xs'.
3051 * Then set 'weirdinvert' according to value of 't_xs'.
3052 */
3053 if (p_wiv && !old_value)
3054 T_XS = (char_u *)"y";
3055 else if (!p_wiv && old_value)
3056 T_XS = empty_option;
3057 p_wiv = (*T_XS != NUL);
3058 }
3059
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003060#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 else if ((int *)varp == &p_beval)
3062 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003063 if (!balloonEvalForTerm)
3064 {
3065 if (p_beval && !old_value)
3066 gui_mch_enable_beval_area(balloonEval);
3067 else if (!p_beval && old_value)
3068 gui_mch_disable_beval_area(balloonEval);
3069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003071#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003072#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003073 else if ((int *)varp == &p_bevalterm)
3074 {
3075 mch_bevalterm_changed();
3076 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003079#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 else if ((int *)varp == &p_acd)
3081 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003082 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003083 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 }
3085#endif
3086
3087#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003088 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 else if ((int *)varp == &curwin->w_p_diff)
3090 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003091 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003092 diff_buf_adjust(curwin);
3093# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (foldmethodIsDiff(curwin))
3095 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 }
3098#endif
3099
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003100#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003101 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 else if ((int *)varp == &p_imdisable)
3103 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003104 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 if (p_imdisable)
3106 im_set_active(FALSE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003107 else if (State & MODE_INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003108 // When the option is set from an autocommand, it may need to take
3109 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003110 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 }
3112#endif
3113
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003114#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003115 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003116 else if ((int *)varp == &curwin->w_p_spell)
3117 {
3118 if (curwin->w_p_spell)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003119 errmsg = did_set_spelllang(curwin);
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003120 }
3121#endif
3122
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#ifdef FEAT_ARABIC
3124 if ((int *)varp == &curwin->w_p_arab)
3125 {
3126 if (curwin->w_p_arab)
3127 {
3128 /*
3129 * 'arabic' is set, handle various sub-settings.
3130 */
3131 if (!p_tbidi)
3132 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003133 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 if (!curwin->w_p_rl)
3135 {
3136 curwin->w_p_rl = TRUE;
3137 changed_window_setting();
3138 }
3139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003140 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (!p_arshape)
3142 {
3143 p_arshape = TRUE;
3144 redraw_later_clear();
3145 }
3146 }
3147
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003148 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003149 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003151 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003152 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3153
Bram Moolenaar8820b482017-03-16 17:23:31 +01003154 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003155 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003156#ifdef FEAT_EVAL
3157 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3158#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003161 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003165 // Force-set the necessary keymap for arabic
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003166 errmsg = set_option_value((char_u *)"keymap",
3167 0L, (char_u *)"arabic", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
3170 else
3171 {
3172 /*
3173 * 'arabic' is reset, handle various sub-settings.
3174 */
3175 if (!p_tbidi)
3176 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003177 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 if (curwin->w_p_rl)
3179 {
3180 curwin->w_p_rl = FALSE;
3181 changed_window_setting();
3182 }
3183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003184 // 'arabicshape' isn't reset, it is a global option and
3185 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003188 // 'delcombine' isn't reset, it is a global option and another
3189 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
3191# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003192 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 curbuf->b_p_iminsert = B_IMODE_NONE;
3194 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3195# endif
3196 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003197 }
3198
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003199#endif
3200
Bram Moolenaar44826212019-08-22 21:23:20 +02003201#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3202 else if (((int *)varp == &curwin->w_p_nu
3203 || (int *)varp == &curwin->w_p_rnu)
3204 && gui.in_use
3205 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3206 && curbuf->b_signlist != NULL)
3207 {
3208 // If the 'number' or 'relativenumber' options are modified and
3209 // 'signcolumn' is set to 'number', then clear the screen for a full
3210 // refresh. Otherwise the sign icons are not displayed properly in the
3211 // number column. If the 'number' option is set and only the
3212 // 'relativenumber' option is toggled, then don't refresh the screen
3213 // (optimization).
3214 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3215 redraw_all_later(CLEAR);
3216 }
3217#endif
3218
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003219#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003220 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003221 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003222 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003223# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003224 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003225 if (
3226# ifdef VIMDLL
3227 !gui.in_use && !gui.starting &&
3228# endif
3229 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003230 {
3231 p_tgc = 0;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003232 return e_24_bit_colors_are_not_supported_on_this_environment;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003233 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003234 if (is_term_win32())
3235 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003236# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003237# ifdef FEAT_GUI
3238 if (!gui.in_use && !gui.starting)
3239# endif
3240 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003241# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003242 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003243 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003244 {
3245 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003246 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003247 init_highlight(TRUE, FALSE);
3248 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003249# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003250# ifdef FEAT_TERMINAL
3251 term_update_colors_all();
LemonBoyb2b3acb2022-05-20 10:10:34 +01003252 term_update_palette_all();
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003253 term_update_wincolor_all();
3254# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003255 }
3256#endif
3257
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 /*
3259 * End of handling side effects for bool options.
3260 */
3261
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003262 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 options[opt_idx].flags |= P_WAS_SET;
3265
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003266#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003267 apply_optionset_autocmd(opt_idx, opt_flags,
3268 (long)(old_value ? TRUE : FALSE),
3269 (long)(old_global_value ? TRUE : FALSE),
3270 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003271#endif
3272
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003273 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003274 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003275 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003276 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003277
3278 if ((opt_flags & OPT_NO_REDRAW) == 0)
3279 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003281 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282}
3283
3284/*
3285 * Set the value of a number option, and take care of side effects.
3286 * Returns NULL for success, or an error message for an error.
3287 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003288 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003289set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003290 int opt_idx, // index in options[] table
3291 char_u *varp, // pointer to the option variable
3292 long value, // new value
3293 char *errbuf, // buffer for error messages
3294 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003295 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3296 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003298 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003300#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003301 long old_global_value = 0; // only used when setting a local and
3302 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003303#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003304 long old_Rows = Rows; // remember old Rows
3305 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 long *pp = (long *)varp;
3307
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003308 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003309 if ((secure
3310#ifdef HAVE_SANDBOX
3311 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003313 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003314 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003316#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003317 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003318 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003319 // value (since there is only one value).
3320 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003321 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3322 OPT_GLOBAL);
3323#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003324
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 *pp = value;
3326#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003327 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003328 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003330#ifdef FEAT_GUI
3331 need_mouse_correct = TRUE;
3332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaar14f24742012-08-08 18:01:05 +02003334 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003336 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003337#ifdef FEAT_VARTABS
3338 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3339 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003340 ? tabstop_first(curbuf->b_p_vts_array)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003341 : curbuf->b_p_ts;
3342#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
3346
3347 /*
3348 * Number options that need some action when changed
3349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 if (pp == &p_wh || pp == &p_hh)
3351 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003352 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 if (p_wh < 1)
3354 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003355 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 p_wh = 1;
3357 }
3358 if (p_wmh > p_wh)
3359 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003360 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 p_wh = p_wmh;
3362 }
3363 if (p_hh < 0)
3364 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003365 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 p_hh = 0;
3367 }
3368
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003369 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003370 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
3372 if (pp == &p_wh && curwin->w_height < p_wh)
3373 win_setheight((int)p_wh);
3374 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3375 win_setheight((int)p_hh);
3376 }
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 else if (pp == &p_wmh)
3379 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003380 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 if (p_wmh < 0)
3382 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003383 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 p_wmh = 0;
3385 }
3386 if (p_wmh > p_wh)
3387 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003388 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 p_wmh = p_wh;
3390 }
3391 win_setminheight();
3392 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003393 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003395 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 if (p_wiw < 1)
3397 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003398 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 p_wiw = 1;
3400 }
3401 if (p_wmw > p_wiw)
3402 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003403 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 p_wiw = p_wmw;
3405 }
3406
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003407 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003408 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 win_setwidth((int)p_wiw);
3410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 else if (pp == &p_wmw)
3412 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003413 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (p_wmw < 0)
3415 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003416 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 p_wmw = 0;
3418 }
3419 if (p_wmw > p_wiw)
3420 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003421 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 p_wmw = p_wiw;
3423 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003424 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003427 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 else if (pp == &p_ls)
3429 {
3430 last_status(FALSE);
3431 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003432
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003433 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003434 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003435 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003436 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
3439#ifdef FEAT_GUI
3440 else if (pp == &p_linespace)
3441 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003442 // Recompute gui.char_height and resize the Vim window to keep the
3443 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003444 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003445 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447#endif
3448
3449#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003450 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 else if (pp == &curwin->w_p_fdl)
3452 {
3453 if (curwin->w_p_fdl < 0)
3454 curwin->w_p_fdl = 0;
3455 newFoldLevel();
3456 }
3457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003458 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 else if (pp == &curwin->w_p_fml)
3460 {
3461 foldUpdateAll(curwin);
3462 }
3463
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003464 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 else if (pp == &curwin->w_p_fdn)
3466 {
3467 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3468 foldUpdateAll(curwin);
3469 }
3470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003471 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 else if (pp == &curwin->w_p_fdc)
3473 {
3474 if (curwin->w_p_fdc < 0)
3475 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003476 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 curwin->w_p_fdc = 0;
3478 }
3479 else if (curwin->w_p_fdc > 12)
3480 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003481 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 curwin->w_p_fdc = 12;
3483 }
3484 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003485#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003487 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3489 {
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003490#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (foldmethodIsIndent(curwin))
3492 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003493#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003494 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3495 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003496 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3497 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003500 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003501 else if (pp == &p_mco)
3502 {
3503 if (p_mco > MAX_MCO)
3504 p_mco = MAX_MCO;
3505 else if (p_mco < 0)
3506 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003507 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003508 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003509
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 else if (pp == &curbuf->b_p_iminsert)
3511 {
3512 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3513 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003514 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 curbuf->b_p_iminsert = B_IMODE_NONE;
3516 }
3517 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003518 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003520#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003521 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 status_redraw_curbuf();
3523#endif
3524 }
3525
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003526#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003527 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003528 else if (pp == &p_imst)
3529 {
3530 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003531 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003532 }
3533#endif
3534
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003535 else if (pp == &p_window)
3536 {
3537 if (p_window < 1)
3538 p_window = 1;
3539 else if (p_window >= Rows)
3540 p_window = Rows - 1;
3541 }
3542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 else if (pp == &curbuf->b_p_imsearch)
3544 {
3545 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3546 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003547 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 curbuf->b_p_imsearch = B_IMODE_NONE;
3549 }
3550 p_imsearch = curbuf->b_p_imsearch;
3551 }
3552
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003553 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 else if (pp == &p_titlelen)
3555 {
3556 if (p_titlelen < 0)
3557 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003558 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 p_titlelen = 85;
3560 }
3561 if (starting != NO_SCREEN && old_value != p_titlelen)
3562 need_maketitle = TRUE;
3563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003565 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 else if (pp == &p_ch)
3567 {
3568 if (p_ch < 1)
3569 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003570 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 p_ch = 1;
3572 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003573 if (p_ch > Rows - min_rows() + 1)
3574 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003576 // Only compute the new window layout when startup has been
3577 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (p_ch != old_value && full_screen
3579#ifdef FEAT_GUI
3580 && !gui.starting
3581#endif
3582 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003583 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
3585
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003586 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 else if (pp == &p_uc)
3588 {
3589 if (p_uc < 0)
3590 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003591 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 p_uc = 100;
3593 }
3594 if (p_uc && !old_value)
3595 ml_open_files();
3596 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003597#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003598 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003599 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003600 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003601 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003602 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003603 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003604 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003605 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003606 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003607 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003608 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003609 }
3610 }
3611#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003612#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003613 else if (pp == &p_mzq)
3614 mzvim_reset_timer();
3615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003617#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003618 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003619 else if (pp == &p_pyx)
3620 {
3621 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003622 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003623 }
3624#endif
3625
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003626 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 else if (pp == &p_ul)
3628 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003629 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003631 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 p_ul = value;
3633 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003634 else if (pp == &curbuf->b_p_ul)
3635 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003636 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003637 curbuf->b_p_ul = old_value;
3638 u_sync(TRUE);
3639 curbuf->b_p_ul = value;
3640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003642#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003643 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003644 else if (pp == &curwin->w_p_nuw)
3645 {
3646 if (curwin->w_p_nuw < 1)
3647 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003648 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003649 curwin->w_p_nuw = 1;
3650 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003651 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003652 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003653 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003654 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003655 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003656 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003657 }
3658#endif
3659
Bram Moolenaar1a384422010-07-14 19:53:30 +02003660 else if (pp == &curbuf->b_p_tw)
3661 {
3662 if (curbuf->b_p_tw < 0)
3663 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003664 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003665 curbuf->b_p_tw = 0;
3666 }
3667#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003668 {
3669 win_T *wp;
3670 tabpage_T *tp;
3671
3672 FOR_ALL_TAB_WINDOWS(tp, wp)
3673 check_colorcolumn(wp);
3674 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003675#endif
3676 }
3677
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 /*
3679 * Check the bounds for numeric options here
3680 */
3681 if (Rows < min_rows() && full_screen)
3682 {
3683 if (errbuf != NULL)
3684 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003685 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003686 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 errmsg = errbuf;
3688 }
3689 Rows = min_rows();
3690 }
3691 if (Columns < MIN_COLUMNS && full_screen)
3692 {
3693 if (errbuf != NULL)
3694 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003695 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003696 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 errmsg = errbuf;
3698 }
3699 Columns = MIN_COLUMNS;
3700 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003701 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 /*
3704 * If the screen (shell) height has been changed, assume it is the
3705 * physical screenheight.
3706 */
3707 if (old_Rows != Rows || old_Columns != Columns)
3708 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003709 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 if (updating_screen)
3711 *pp = old_value;
3712 else if (full_screen
3713#ifdef FEAT_GUI
3714 && !gui.starting
3715#endif
3716 )
3717 set_shellsize((int)Columns, (int)Rows, TRUE);
3718 else
3719 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003720 // Postpone the resizing; check the size and cmdline position for
3721 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 check_shellsize();
3723 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3724 cmdline_row = Rows - p_ch;
3725 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003726 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003727 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 if (curbuf->b_p_ts <= 0)
3731 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003732 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 curbuf->b_p_ts = 8;
3734 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003735 else if (curbuf->b_p_ts > TABSTOP_MAX)
3736 {
3737 errmsg = e_invalid_argument;
3738 curbuf->b_p_ts = 8;
3739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (p_tm < 0)
3741 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003742 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 p_tm = 0;
3744 }
3745 if ((curwin->w_p_scr <= 0
3746 || (curwin->w_p_scr > curwin->w_height
3747 && curwin->w_height > 0))
3748 && full_screen)
3749 {
3750 if (pp == &(curwin->w_p_scr))
3751 {
3752 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003753 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 win_comp_scroll(curwin);
3755 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003756 // If 'scroll' became invalid because of a side effect silently adjust
3757 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 else if (curwin->w_p_scr <= 0)
3759 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003760 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 curwin->w_p_scr = curwin->w_height;
3762 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003763 if (p_hi < 0)
3764 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003765 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003766 p_hi = 0;
3767 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003768 else if (p_hi > 10000)
3769 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003770 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003771 p_hi = 10000;
3772 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003773 if (p_re < 0 || p_re > 2)
3774 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003775 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003776 p_re = 0;
3777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 if (p_report < 0)
3779 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003780 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 p_report = 1;
3782 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003783 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003785 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 p_sj = Rows / 2;
3787 else
3788 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003789 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 p_sj = 1;
3791 }
3792 }
3793 if (p_so < 0 && full_screen)
3794 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003795 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 p_so = 0;
3797 }
3798 if (p_siso < 0 && full_screen)
3799 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003800 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 p_siso = 0;
3802 }
3803#ifdef FEAT_CMDWIN
3804 if (p_cwh < 1)
3805 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003806 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 p_cwh = 1;
3808 }
3809#endif
3810 if (p_ut < 0)
3811 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003812 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 p_ut = 2000;
3814 }
3815 if (p_ss < 0)
3816 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003817 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 p_ss = 0;
3819 }
3820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003821 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3823 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3824
3825 options[opt_idx].flags |= P_WAS_SET;
3826
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003827#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003828 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3829 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003830#endif
3831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003832 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003833 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003834 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003835 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003836 if ((opt_flags & OPT_NO_REDRAW) == 0)
3837 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
3839 return errmsg;
3840}
3841
3842/*
3843 * Called after an option changed: check if something needs to be redrawn.
3844 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003845 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003846check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003848 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003849 int doclear = (flags & P_RCLR) == P_RCLR;
3850 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003852 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
3855 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3856 changed_window_setting();
3857 if (flags & P_RBUF)
3858 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003859 if (flags & P_RWINONLY)
3860 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003861 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 redraw_all_later(CLEAR);
3863 else if (all)
3864 redraw_all_later(NOT_VALID);
3865}
3866
3867/*
3868 * Find index for option 'arg'.
3869 * Return -1 if not found.
3870 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003871 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003872findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873{
3874 int opt_idx;
3875 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003876 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 int is_term_opt;
3878
3879 /*
3880 * For first call: Initialize the quick-access table.
3881 * It contains the index for the first option that starts with a certain
3882 * letter. There are 26 letters, plus the first "t_" option.
3883 */
3884 if (quick_tab[1] == 0)
3885 {
3886 p = options[0].fullname;
3887 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3888 {
3889 if (s[0] != p[0])
3890 {
3891 if (s[0] == 't' && s[1] == '_')
3892 quick_tab[26] = opt_idx;
3893 else
3894 quick_tab[CharOrdLow(s[0])] = opt_idx;
3895 }
3896 p = s;
3897 }
3898 }
3899
3900 /*
3901 * Check for name starting with an illegal character.
3902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 return -1;
3905
3906 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3907 if (is_term_opt)
3908 opt_idx = quick_tab[26];
3909 else
3910 opt_idx = quick_tab[CharOrdLow(arg[0])];
3911 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3912 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003913 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 break;
3915 }
3916 if (s == NULL && !is_term_opt)
3917 {
3918 opt_idx = quick_tab[CharOrdLow(arg[0])];
3919 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3920 {
3921 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003922 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 break;
3924 s = NULL;
3925 }
3926 }
3927 if (s == NULL)
3928 opt_idx = -1;
3929 return opt_idx;
3930}
3931
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003932#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933/*
3934 * Get the value for an option.
3935 *
3936 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003937 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003938 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003939 * String option: gov_string, *stringval gets allocated string.
3940 * Hidden Number option: gov_hidden_number.
3941 * Hidden Toggle option: gov_hidden_bool.
3942 * Hidden String option: gov_hidden_string.
3943 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003944 *
3945 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003947 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003948get_option_value(
3949 char_u *name,
3950 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003951 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003952 int *flagsp,
3953 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
3955 int opt_idx;
3956 char_u *varp;
3957
3958 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003959 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003960 {
3961 int key;
3962
3963 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003964 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003965 {
3966 char_u key_name[2];
3967 char_u *p;
3968
Bram Moolenaar10c75c42021-12-28 20:53:30 +00003969 if (flagsp != NULL)
3970 *flagsp = 0; // terminal option has no flags
3971
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003972 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003973 if (key < 0)
3974 {
3975 key_name[0] = KEY2TERMCAP0(key);
3976 key_name[1] = KEY2TERMCAP1(key);
3977 }
3978 else
3979 {
3980 key_name[0] = KS_KEY;
3981 key_name[1] = (key & 0xff);
3982 }
3983 p = find_termcode(key_name);
3984 if (p != NULL)
3985 {
3986 if (stringval != NULL)
3987 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003988 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003989 }
3990 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003991 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003994 varp = get_varp_scope(&(options[opt_idx]), scope);
3995
3996 if (flagsp != NULL)
3997 // Return the P_xxxx option flags.
3998 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
4000 if (options[opt_idx].flags & P_STRING)
4001 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004002 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004003 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (stringval != NULL)
4005 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004006 if ((char_u **)varp == &p_pt) // 'pastetoggle'
4007 *stringval = str2special_save(*(char_u **)(varp), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004009 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004010 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004011 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004014 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 *stringval = vim_strsave(*(char_u **)(varp));
4016 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004017 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004020 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004021 return (options[opt_idx].flags & P_NUM)
4022 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 if (options[opt_idx].flags & P_NUM)
4024 *numval = *(long *)varp;
4025 else
4026 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004027 // Special case: 'modified' is b_changed, but we also want to consider
4028 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 if ((int *)varp == &curbuf->b_changed)
4030 *numval = curbufIsChanged();
4031 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004032 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004034 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035}
4036#endif
4037
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004038#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004039/*
4040 * Returns the option attributes and its value. Unlike the above function it
4041 * will return either global value or local value of the option depending on
4042 * what was requested, but it will never return global value if it was
4043 * requested to return local one and vice versa. Neither it will return
4044 * buffer-local value if it was requested to return window-local one.
4045 *
4046 * Pretends that option is absent if it is not present in the requested scope
4047 * (i.e. has no global, window-local or buffer-local value depending on
4048 * opt_type). Uses
4049 *
4050 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004051 * 0 hidden or unknown option, also option that does not have requested
4052 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004053 * see SOPT_* in vim.h for other flags
4054 *
4055 * Possible opt_type values: see SREQ_* in vim.h
4056 */
4057 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004058get_option_value_strict(
4059 char_u *name,
4060 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004061 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004062 int opt_type,
4063 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004064{
4065 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004066 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004067 struct vimoption *p;
4068 int r = 0;
4069
4070 opt_idx = findoption(name);
4071 if (opt_idx < 0)
4072 return 0;
4073
4074 p = &(options[opt_idx]);
4075
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004076 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004077 if (p->var == NULL)
4078 return 0;
4079
4080 if (p->flags & P_BOOL)
4081 r |= SOPT_BOOL;
4082 else if (p->flags & P_NUM)
4083 r |= SOPT_NUM;
4084 else if (p->flags & P_STRING)
4085 r |= SOPT_STRING;
4086
4087 if (p->indir == PV_NONE)
4088 {
4089 if (opt_type == SREQ_GLOBAL)
4090 r |= SOPT_GLOBAL;
4091 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004092 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004093 }
4094 else
4095 {
4096 if (p->indir & PV_BOTH)
4097 r |= SOPT_GLOBAL;
4098 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004099 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004100
4101 if (p->indir & PV_WIN)
4102 {
4103 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004104 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004105 else
4106 r |= SOPT_WIN;
4107 }
4108 else if (p->indir & PV_BUF)
4109 {
4110 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004111 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004112 else
4113 r |= SOPT_BUF;
4114 }
4115 }
4116
4117 if (stringval == NULL)
4118 return r;
4119
4120 if (opt_type == SREQ_GLOBAL)
4121 varp = p->var;
4122 else
4123 {
4124 if (opt_type == SREQ_BUF)
4125 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004126 // Special case: 'modified' is b_changed, but we also want to
4127 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004128 if (p->indir == PV_MOD)
4129 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004130 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004131 varp = NULL;
4132 }
4133#ifdef FEAT_CRYPT
4134 else if (p->indir == PV_KEY)
4135 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004136 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004137 *stringval = NULL;
4138 varp = NULL;
4139 }
4140#endif
4141 else
4142 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004143 buf_T *save_curbuf = curbuf;
4144
4145 // only getting a pointer, no need to use aucmd_prepbuf()
4146 curbuf = (buf_T *)from;
4147 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004148 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004149 curbuf = save_curbuf;
4150 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004151 }
4152 }
4153 else if (opt_type == SREQ_WIN)
4154 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004155 win_T *save_curwin = curwin;
4156
4157 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004158 curbuf = curwin->w_buffer;
4159 varp = get_varp(p);
4160 curwin = save_curwin;
4161 curbuf = curwin->w_buffer;
4162 }
4163 if (varp == p->var)
4164 return (r | SOPT_UNSET);
4165 }
4166
4167 if (varp != NULL)
4168 {
4169 if (p->flags & P_STRING)
4170 *stringval = vim_strsave(*(char_u **)(varp));
4171 else if (p->flags & P_NUM)
4172 *numval = *(long *) varp;
4173 else
4174 *numval = *(int *)varp;
4175 }
4176
4177 return r;
4178}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004179
4180/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004181 * Iterate over options. First argument is a pointer to a pointer to a
4182 * structure inside options[] array, second is option type like in the above
4183 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004184 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004185 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004186 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004187 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004188 * first argument to NULL.
4189 *
4190 * Returns full option name for current option on each call.
4191 */
4192 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004193option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004194{
4195 struct vimoption *ret = NULL;
4196 do
4197 {
4198 if (*option == NULL)
4199 *option = (void *) options;
4200 else if (((struct vimoption *) (*option))->fullname == NULL)
4201 {
4202 *option = NULL;
4203 return NULL;
4204 }
4205 else
4206 *option = (void *) (((struct vimoption *) (*option)) + 1);
4207
4208 ret = ((struct vimoption *) (*option));
4209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004210 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004211 if (ret->var == NULL)
4212 {
4213 ret = NULL;
4214 continue;
4215 }
4216
4217 switch (opt_type)
4218 {
4219 case SREQ_GLOBAL:
4220 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4221 ret = NULL;
4222 break;
4223 case SREQ_BUF:
4224 if (!(ret->indir & PV_BUF))
4225 ret = NULL;
4226 break;
4227 case SREQ_WIN:
4228 if (!(ret->indir & PV_WIN))
4229 ret = NULL;
4230 break;
4231 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004232 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004233 return NULL;
4234 }
4235 }
4236 while (ret == NULL);
4237
4238 return (char_u *)ret->fullname;
4239}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004240#endif
4241
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004243 * Return the flags for the option at 'opt_idx'.
4244 */
4245 long_u
4246get_option_flags(int opt_idx)
4247{
4248 return options[opt_idx].flags;
4249}
4250
4251/*
4252 * Set a flag for the option at 'opt_idx'.
4253 */
4254 void
4255set_option_flag(int opt_idx, long_u flag)
4256{
4257 options[opt_idx].flags |= flag;
4258}
4259
4260/*
4261 * Clear a flag for the option at 'opt_idx'.
4262 */
4263 void
4264clear_option_flag(int opt_idx, long_u flag)
4265{
4266 options[opt_idx].flags &= ~flag;
4267}
4268
4269/*
4270 * Returns TRUE if the option at 'opt_idx' is a global option
4271 */
4272 int
4273is_global_option(int opt_idx)
4274{
4275 return options[opt_idx].indir == PV_NONE;
4276}
4277
4278/*
4279 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4280 * local value.
4281 */
4282 int
4283is_global_local_option(int opt_idx)
4284{
4285 return options[opt_idx].indir & PV_BOTH;
4286}
4287
4288/*
4289 * Returns TRUE if the option at 'opt_idx' is a window-local option
4290 */
4291 int
4292is_window_local_option(int opt_idx)
4293{
4294 return options[opt_idx].var == VAR_WIN;
4295}
4296
4297/*
4298 * Returns TRUE if the option at 'opt_idx' is a hidden option
4299 */
4300 int
4301is_hidden_option(int opt_idx)
4302{
4303 return options[opt_idx].var == NULL;
4304}
4305
4306#if defined(FEAT_CRYPT) || defined(PROTO)
4307/*
4308 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4309 */
4310 int
4311is_crypt_key_option(int opt_idx)
4312{
4313 return options[opt_idx].indir == PV_KEY;
4314}
4315#endif
4316
4317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 * Set the value of option "name".
4319 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004320 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004321 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004323 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004324set_option_value(
4325 char_u *name,
4326 long number,
4327 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004328 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329{
4330 int opt_idx;
4331 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004332 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
4334 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004335 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004336 {
4337 int key;
4338
4339 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004340 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004341 {
4342 char_u key_name[2];
4343
4344 if (key < 0)
4345 {
4346 key_name[0] = KEY2TERMCAP0(key);
4347 key_name[1] = KEY2TERMCAP1(key);
4348 }
4349 else
4350 {
4351 key_name[0] = KS_KEY;
4352 key_name[1] = (key & 0xff);
4353 }
4354 add_termcode(key_name, string, FALSE);
4355 if (full_screen)
4356 ttest(FALSE);
4357 redraw_all_later(CLEAR);
4358 return NULL;
4359 }
4360
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004361 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 else
4364 {
4365 flags = options[opt_idx].flags;
4366#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004367 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004369 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004370 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004371 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004374 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004375 return set_string_option(opt_idx, string, opt_flags);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004376
4377 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
4378 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004380 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004382 int idx;
4383
4384 // Either we are given a string or we are setting option
4385 // to zero.
4386 for (idx = 0; string[idx] == '0'; ++idx)
4387 ;
4388 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004389 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004390 // There's another character after zeros or the string
4391 // is empty. In both cases, we are trying to set a
4392 // num option using a string.
4393 semsg(_(e_number_required_after_str_equal_str),
4394 name, string);
4395 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004396
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004399 if (flags & P_NUM)
4400 return set_num_option(opt_idx, varp, number,
4401 NULL, 0, opt_flags);
4402 else
4403 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004405
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004407 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408}
4409
4410/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004411 * Call set_option_value() and when an error is returned report it.
4412 */
4413 void
4414set_option_value_give_err(
4415 char_u *name,
4416 long number,
4417 char_u *string,
4418 int opt_flags) // OPT_LOCAL or 0 (both)
4419{
4420 char *errmsg = set_option_value(name, number, string, opt_flags);
4421
4422 if (errmsg != NULL)
4423 emsg(_(errmsg));
4424}
4425
4426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 * Get the terminal code for a terminal option.
4428 * Returns NULL when not found.
4429 */
4430 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004431get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432{
4433 int opt_idx;
4434 char_u *varp;
4435
4436 if (tname[0] != 't' || tname[1] != '_' ||
4437 tname[2] == NUL || tname[3] == NUL)
4438 return NULL;
4439 if ((opt_idx = findoption(tname)) >= 0)
4440 {
4441 varp = get_varp(&(options[opt_idx]));
4442 if (varp != NULL)
4443 varp = *(char_u **)(varp);
4444 return varp;
4445 }
4446 return find_termcode(tname + 2);
4447}
4448
4449 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004450get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452 int i;
4453
4454 i = findoption((char_u *)"hl");
4455 if (i >= 0)
4456 return options[i].def_val[VI_DEFAULT];
4457 return (char_u *)NULL;
4458}
4459
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004460 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004461get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004462{
4463 int i;
4464
4465 i = findoption((char_u *)"enc");
4466 if (i >= 0)
4467 return options[i].def_val[VI_DEFAULT];
4468 return (char_u *)NULL;
4469}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004470
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004471#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004472 int
4473is_option_allocated(char *name)
4474{
4475 int idx = findoption((char_u *)name);
4476
4477 return idx >= 0 && (options[idx].flags & P_ALLOCED);
4478}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004479#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004480
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481/*
4482 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004483 * When "has_lt" is true there is a '<' before "*arg_arg".
4484 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 */
4486 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004487find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004489 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004491 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
4493 /*
4494 * Don't use get_special_key_code() for t_xx, we don't want it to call
4495 * add_termcap_entry().
4496 */
4497 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4498 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004499 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004501 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004503 key = find_special_key(&arg, &modifiers,
4504 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004505 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 key = 0;
4507 }
4508 return key;
4509}
4510
4511/*
4512 * if 'all' == 0: show changed options
4513 * if 'all' == 1: show all normal options
4514 * if 'all' == 2: show all terminal options
4515 */
4516 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004517showoptions(
4518 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004519 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520{
4521 struct vimoption *p;
4522 int col;
4523 int isterm;
4524 char_u *varp;
4525 struct vimoption **items;
4526 int item_count;
4527 int run;
4528 int row, rows;
4529 int cols;
4530 int i;
4531 int len;
4532
4533#define INC 20
4534#define GAP 3
4535
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004536 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 if (items == NULL)
4538 return;
4539
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004540 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004542 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004544 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004546 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004548 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
4550 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004551 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 * 1. display the short items
4553 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004554 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 */
4556 for (run = 1; run <= 2 && !got_int; ++run)
4557 {
4558 /*
4559 * collect the items in items[]
4560 */
4561 item_count = 0;
4562 for (p = &options[0]; p->fullname != NULL; p++)
4563 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004564 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004565 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004566 continue;
4567
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 varp = NULL;
4569 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004570 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
4572 if (p->indir != PV_NONE && !isterm)
4573 varp = get_varp_scope(p, opt_flags);
4574 }
4575 else
4576 varp = get_varp(p);
4577 if (varp != NULL
4578 && ((all == 2 && isterm)
4579 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004580 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004582 if (opt_flags & OPT_ONECOLUMN)
4583 len = Columns;
4584 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004585 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 else
4587 {
4588 option_value2string(p, opt_flags);
4589 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4590 }
4591 if ((len <= INC - GAP && run == 1) ||
4592 (len > INC - GAP && run == 2))
4593 items[item_count++] = p;
4594 }
4595 }
4596
4597 /*
4598 * display the items
4599 */
4600 if (run == 1)
4601 {
4602 cols = (Columns + GAP - 3) / INC;
4603 if (cols == 0)
4604 cols = 1;
4605 rows = (item_count + cols - 1) / cols;
4606 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004607 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 rows = item_count;
4609 for (row = 0; row < rows && !got_int; ++row)
4610 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004611 msg_putchar('\n'); // go to next line
4612 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 break;
4614 col = 0;
4615 for (i = row; i < item_count; i += rows)
4616 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004617 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 showoneopt(items[i], opt_flags);
4619 col += INC;
4620 }
4621 out_flush();
4622 ui_breakcheck();
4623 }
4624 }
4625 vim_free(items);
4626}
4627
4628/*
4629 * Return TRUE if option "p" has its default value.
4630 */
4631 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004632optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633{
4634 int dvi;
4635
4636 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004637 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004638 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004640 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004642 // the cast to long is required for Manx C, long_i is
4643 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004644 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004645 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4647}
4648
4649/*
4650 * showoneopt: show the value of one option
4651 * must not be called with a hidden option!
4652 */
4653 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004654showoneopt(
4655 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004656 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004658 char_u *varp;
4659 int save_silent = silent_mode;
4660
4661 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004662 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
4664 varp = get_varp_scope(p, opt_flags);
4665
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004666 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4668 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004669 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004671 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004673 msg_puts(" ");
4674 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (!(p->flags & P_BOOL))
4676 {
4677 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004678 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 option_value2string(p, opt_flags);
4680 msg_outtrans(NameBuff);
4681 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004682
4683 silent_mode = save_silent;
4684 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685}
4686
4687/*
4688 * Write modified options as ":set" commands to a file.
4689 *
4690 * There are three values for "opt_flags":
4691 * OPT_GLOBAL: Write global option values and fresh values of
4692 * buffer-local options (used for start of a session
4693 * file).
4694 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4695 * curwin (used for a vimrc file).
4696 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4697 * and local values for window-local options of
4698 * curwin. Local values are also written when at the
4699 * default value, because a modeline or autocommand
4700 * may have set them when doing ":edit file" and the
4701 * user has set them back at the default or fresh
4702 * value.
4703 * When "local_only" is TRUE, don't write fresh
4704 * values, only local values (for ":mkview").
4705 * (fresh value = value used for a new buffer or window for a local option).
4706 *
4707 * Return FAIL on error, OK otherwise.
4708 */
4709 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004710makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
4712 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004713 char_u *varp; // currently used value
4714 char_u *varp_fresh; // local value
4715 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 char *cmd;
4717 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004718 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719
4720 /*
4721 * The options that don't have a default (terminal name, columns, lines)
4722 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004723 * Do the loop over "options[]" twice: once for options with the
4724 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004726 for (pri = 1; pri >= 0; --pri)
4727 {
4728 for (p = &options[0]; !istermoption(p); p++)
4729 if (!(p->flags & P_NO_MKRC)
4730 && !istermoption(p)
4731 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004733 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4735 continue;
4736
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004737 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4738 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4740 continue;
4741
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004742 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004744 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 continue;
4746
Bram Moolenaard23b7142021-04-17 21:04:34 +02004747 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4748 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004749 continue;
4750
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 round = 2;
4752 if (p->indir != PV_NONE)
4753 {
4754 if (p->var == VAR_WIN)
4755 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004756 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 if (!(opt_flags & OPT_LOCAL))
4758 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004759 // When fresh value of window-local option is not at the
4760 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4762 {
4763 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004764 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
4766 round = 1;
4767 varp_local = varp;
4768 varp = varp_fresh;
4769 }
4770 }
4771 }
4772 }
4773
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004774 // Round 1: fresh value for window-local options.
4775 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 for ( ; round <= 2; varp = varp_local, ++round)
4777 {
4778 if (round == 1 || (opt_flags & OPT_GLOBAL))
4779 cmd = "set";
4780 else
4781 cmd = "setlocal";
4782
4783 if (p->flags & P_BOOL)
4784 {
4785 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4786 return FAIL;
4787 }
4788 else if (p->flags & P_NUM)
4789 {
4790 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4791 return FAIL;
4792 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004793 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004795 int do_endif = FALSE;
4796
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004797 // Don't set 'syntax' and 'filetype' again if the value is
4798 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004799 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004800#if defined(FEAT_SYN_HL)
4801 p->indir == PV_SYN ||
4802#endif
4803 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
4805 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4806 *(char_u **)(varp)) < 0
4807 || put_eol(fd) < 0)
4808 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004809 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 }
4811 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004812 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004814 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
4816 if (put_line(fd, "endif") == FAIL)
4817 return FAIL;
4818 }
4819 }
4820 }
4821 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 return OK;
4824}
4825
4826#if defined(FEAT_FOLDING) || defined(PROTO)
4827/*
4828 * Generate set commands for the local fold options only. Used when
4829 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4830 */
4831 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004832makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004834 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004836 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 == FAIL
4838# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004839 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004841 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 == FAIL
4843 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4844 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4845 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4846 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4847 )
4848 return FAIL;
4849
4850 return OK;
4851}
4852#endif
4853
4854 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004855put_setstring(
4856 FILE *fd,
4857 char *cmd,
4858 char *name,
4859 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004860 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
4862 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004863 char_u *buf = NULL;
4864 char_u *part = NULL;
4865 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
4867 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4868 return FAIL;
4869 if (*valuep != NULL)
4870 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004871 // Output 'pastetoggle' as key names. For other
4872 // options some characters have to be escaped with
4873 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 if (valuep == &p_pt)
4875 {
4876 s = *valuep;
4877 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004878 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 return FAIL;
4880 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004881 // expand the option value, replace $HOME by ~
4882 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004884 int size = (int)STRLEN(*valuep) + 1;
4885
4886 // replace home directory in the whole option value into "buf"
4887 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004888 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004889 goto fail;
4890 home_replace(NULL, *valuep, buf, size, FALSE);
4891
4892 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004893 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004894 // can be expanded when read back.
4895 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4896 && vim_strchr(*valuep, ',') != NULL)
4897 {
4898 part = alloc(size);
4899 if (part == NULL)
4900 goto fail;
4901
4902 // write line break to clear the option, e.g. ':set rtp='
4903 if (put_eol(fd) == FAIL)
4904 goto fail;
4905
4906 p = buf;
4907 while (*p != NUL)
4908 {
4909 // for each comma separated option part, append value to
4910 // the option, :set rtp+=value
4911 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4912 goto fail;
4913 (void)copy_option_part(&p, part, size, ",");
4914 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4915 goto fail;
4916 }
4917 vim_free(buf);
4918 vim_free(part);
4919 return OK;
4920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004922 {
4923 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004925 }
4926 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else if (put_escstr(fd, *valuep, 2) == FAIL)
4929 return FAIL;
4930 }
4931 if (put_eol(fd) < 0)
4932 return FAIL;
4933 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004934fail:
4935 vim_free(buf);
4936 vim_free(part);
4937 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938}
4939
4940 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004941put_setnum(
4942 FILE *fd,
4943 char *cmd,
4944 char *name,
4945 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947 long wc;
4948
4949 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4950 return FAIL;
4951 if (wc_use_keyname((char_u *)valuep, &wc))
4952 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004953 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4955 return FAIL;
4956 }
4957 else if (fprintf(fd, "%ld", *valuep) < 0)
4958 return FAIL;
4959 if (put_eol(fd) < 0)
4960 return FAIL;
4961 return OK;
4962}
4963
4964 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004965put_setbool(
4966 FILE *fd,
4967 char *cmd,
4968 char *name,
4969 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004971 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004972 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4974 || put_eol(fd) < 0)
4975 return FAIL;
4976 return OK;
4977}
4978
4979/*
4980 * Clear all the terminal options.
4981 * If the option has been allocated, free the memory.
4982 * Terminal options are never hidden or indirect.
4983 */
4984 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004985clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 /*
4988 * Reset a few things before clearing the old options. This may cause
4989 * outputting a few things that the terminal doesn't understand, but the
4990 * screen will be cleared later, so this is OK.
4991 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004992 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004993 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004995 // When starting the GUI close the display opened for the clipboard.
4996 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 if (gui.starting)
4998 clear_xterm_clip();
4999#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005000 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005002 free_termoptions();
5003}
5004
5005 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005006free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005007{
5008 struct vimoption *p;
5009
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005010 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 if (istermoption(p))
5012 {
5013 if (p->flags & P_ALLOCED)
5014 free_string_option(*(char_u **)(p->var));
5015 if (p->flags & P_DEF_ALLOCED)
5016 free_string_option(p->def_val[VI_DEFAULT]);
5017 *(char_u **)(p->var) = empty_option;
5018 p->def_val[VI_DEFAULT] = empty_option;
5019 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005020#ifdef FEAT_EVAL
5021 // remember where the option was cleared
5022 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
5025 clear_termcodes();
5026}
5027
5028/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005029 * Free the string for one term option, if it was allocated.
5030 * Set the string to empty_option and clear allocated flag.
5031 * "var" points to the option value.
5032 */
5033 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005034free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005035{
5036 struct vimoption *p;
5037
5038 for (p = &options[0]; p->fullname != NULL; p++)
5039 if (p->var == var)
5040 {
5041 if (p->flags & P_ALLOCED)
5042 free_string_option(*(char_u **)(p->var));
5043 *(char_u **)(p->var) = empty_option;
5044 p->flags &= ~P_ALLOCED;
5045 break;
5046 }
5047}
5048
5049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 * Set the terminal option defaults to the current value.
5051 * Used after setting the terminal name.
5052 */
5053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005054set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055{
5056 struct vimoption *p;
5057
5058 for (p = &options[0]; p->fullname != NULL; p++)
5059 {
5060 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5061 {
5062 if (p->flags & P_DEF_ALLOCED)
5063 {
5064 free_string_option(p->def_val[VI_DEFAULT]);
5065 p->flags &= ~P_DEF_ALLOCED;
5066 }
5067 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5068 if (p->flags & P_ALLOCED)
5069 {
5070 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005071 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073 }
5074 }
5075}
5076
5077/*
5078 * return TRUE if 'p' starts with 't_'
5079 */
5080 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005081istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
5083 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5084}
5085
Bram Moolenaardac13472019-09-16 21:06:21 +02005086/*
5087 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5088 */
5089 int
5090istermoption_idx(int opt_idx)
5091{
5092 return istermoption(&options[opt_idx]);
5093}
5094
Bram Moolenaar113e1072019-01-20 15:30:40 +01005095#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005097 * Unset local option value, similar to ":set opt<".
5098 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005099 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005100unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005101{
5102 struct vimoption *p;
5103 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005104 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005105
5106 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005107 if (opt_idx < 0)
5108 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005109 p = &(options[opt_idx]);
5110
5111 switch ((int)p->indir)
5112 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005113 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005114 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005115 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005116 break;
5117 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005118 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005119 break;
5120 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005121 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005122 break;
5123 case PV_AR:
5124 buf->b_p_ar = -1;
5125 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005126 case PV_BKC:
5127 clear_string_option(&buf->b_p_bkc);
5128 buf->b_bkc_flags = 0;
5129 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005130 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005131 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005132 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005133 case PV_TC:
5134 clear_string_option(&buf->b_p_tc);
5135 buf->b_tc_flags = 0;
5136 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005137 case PV_SISO:
5138 curwin->w_p_siso = -1;
5139 break;
5140 case PV_SO:
5141 curwin->w_p_so = -1;
5142 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005143#ifdef FEAT_FIND_ID
5144 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005145 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005146 break;
5147 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005148 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005149 break;
5150#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005151 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005152 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005153 break;
5154 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005155 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005156 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005157#ifdef FEAT_COMPL_FUNC
5158 case PV_TSRFU:
5159 clear_string_option(&buf->b_p_tsrfu);
5160 break;
5161#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005162 case PV_FP:
5163 clear_string_option(&buf->b_p_fp);
5164 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005165#ifdef FEAT_QUICKFIX
5166 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005167 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005168 break;
5169 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005170 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005171 break;
5172 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005173 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005174 break;
5175#endif
5176#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5177 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005178 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005179 break;
5180#endif
5181#if defined(FEAT_CRYPT)
5182 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005183 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005184 break;
5185#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005186#ifdef FEAT_LINEBREAK
5187 case PV_SBR:
5188 clear_string_option(&((win_T *)from)->w_p_sbr);
5189 break;
5190#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005191#ifdef FEAT_STL_OPT
5192 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005193 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005194 break;
5195#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005196 case PV_UL:
5197 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5198 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005199 case PV_LW:
5200 clear_string_option(&buf->b_p_lw);
5201 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005202 case PV_MENC:
5203 clear_string_option(&buf->b_p_menc);
5204 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005205 case PV_LCS:
5206 clear_string_option(&((win_T *)from)->w_p_lcs);
5207 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5208 redraw_later(NOT_VALID);
5209 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005210 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005211 clear_string_option(&((win_T *)from)->w_p_ve);
5212 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005213 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005214 }
5215}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005216#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005217
5218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005220 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 */
5222 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005223get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005225 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
5227 if (p->var == VAR_WIN)
5228 return (char_u *)GLOBAL_WO(get_varp(p));
5229 return p->var;
5230 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005231 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
5233 switch ((int)p->indir)
5234 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005235 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005237 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5238 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5239 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005241 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5242 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5243 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005244 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005245 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005246 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005247 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5248 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005250 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5251 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005253 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5254 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005255#ifdef FEAT_COMPL_FUNC
5256 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5257#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005258#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5259 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5260#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005261#if defined(FEAT_CRYPT)
5262 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5263#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005264#ifdef FEAT_LINEBREAK
5265 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5266#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005267#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005268 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005269#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005270 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005271 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005272 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005273 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005274 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005275 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005276
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005278 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280 return get_varp(p);
5281}
5282
5283/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005284 * Get pointer to option variable at 'opt_idx', depending on local or global
5285 * scope.
5286 */
5287 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005288get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005289{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005290 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005291}
5292
5293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 * Get pointer to option variable.
5295 */
5296 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005297get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005299 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (p->var == NULL)
5301 return NULL;
5302
5303 switch ((int)p->indir)
5304 {
5305 case PV_NONE: return p->var;
5306
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005307 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005308 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005310 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005312 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005314 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005316 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005318 case PV_TC: return *curbuf->b_p_tc != NUL
5319 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005320 case PV_BKC: return *curbuf->b_p_bkc != NUL
5321 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005322 case PV_SISO: return curwin->w_p_siso >= 0
5323 ? (char_u *)&(curwin->w_p_siso) : p->var;
5324 case PV_SO: return curwin->w_p_so >= 0
5325 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005327 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005329 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5331#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005332 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005334 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005336#ifdef FEAT_COMPL_FUNC
5337 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5338 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5339#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005340 case PV_FP: return *curbuf->b_p_fp != NUL
5341 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005343 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005345 case PV_GP: return *curbuf->b_p_gp != NUL
5346 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5347 case PV_MP: return *curbuf->b_p_mp != NUL
5348 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005350#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5351 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5352 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5353#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005354#if defined(FEAT_CRYPT)
5355 case PV_CM: return *curbuf->b_p_cm != NUL
5356 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5357#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005358#ifdef FEAT_LINEBREAK
5359 case PV_SBR: return *curwin->w_p_sbr != NUL
5360 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5361#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005362#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005363 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005364 ? (char_u *)&(curwin->w_p_stl) : p->var;
5365#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005366 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5367 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005368 case PV_LW: return *curbuf->b_p_lw != NUL
5369 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005370 case PV_MENC: return *curbuf->b_p_menc != NUL
5371 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372#ifdef FEAT_ARABIC
5373 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5374#endif
5375 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005376 case PV_LCS: return *curwin->w_p_lcs != NUL
5377 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005378 case PV_VE: return *curwin->w_p_ve != NUL
5379 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005380#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005381 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5382#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005383#ifdef FEAT_SYN_HL
5384 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5385 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005386 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005387 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389#ifdef FEAT_DIFF
5390 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5391#endif
5392#ifdef FEAT_FOLDING
5393 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5394 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5395 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5396 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5397 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5398 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5399 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5400# ifdef FEAT_EVAL
5401 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5402 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5403# endif
5404 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5405#endif
5406 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005407 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005408#ifdef FEAT_LINEBREAK
5409 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005412 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005413#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5415#endif
5416#ifdef FEAT_RIGHTLEFT
5417 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5418 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5419#endif
5420 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5421 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5422#ifdef FEAT_LINEBREAK
5423 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005424 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5425 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005427 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005429 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005430#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005431 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5432 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5433#endif
5434#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005435 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5436 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5437 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
5440 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5441 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5444 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5446 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5448 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5449 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01005450 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453#ifdef FEAT_FOLDING
5454 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005457#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005458 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005460#ifdef FEAT_COMPL_FUNC
5461 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005462 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005463#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005464#ifdef FEAT_EVAL
5465 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005468 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005474 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5476 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5477 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5478 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5479#ifdef FEAT_FIND_ID
5480# ifdef FEAT_EVAL
5481 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5482# endif
5483#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01005484#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5486 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5487#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005488#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005489 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491#ifdef FEAT_CRYPT
5492 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5496 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5497 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5498 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5499 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005501#ifdef FEAT_TEXTOBJ
5502 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5508#ifdef FEAT_SEARCHPATH
5509 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5510#endif
5511 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5512#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005513 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005514 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5515#endif
5516#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005517 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5518 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5519 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005520 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521#endif
5522 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5523 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5524 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5525 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005526#ifdef FEAT_PERSISTENT_UNDO
5527 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5530#ifdef FEAT_KEYMAP
5531 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5532#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005533#ifdef FEAT_SIGNS
5534 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5535#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005536#ifdef FEAT_VARTABS
5537 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5538 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5539#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005540 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005542 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 return (char_u *)&(curbuf->b_p_wm);
5544}
5545
5546/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005547 * Return a pointer to the variable for option at 'opt_idx'
5548 */
5549 char_u *
5550get_option_var(int opt_idx)
5551{
5552 return options[opt_idx].var;
5553}
5554
Dominique Pelle748b3082022-01-08 12:41:16 +00005555#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005556/*
5557 * Return the full name of the option at 'opt_idx'
5558 */
5559 char_u *
5560get_option_fullname(int opt_idx)
5561{
5562 return (char_u *)options[opt_idx].fullname;
5563}
Dominique Pelle748b3082022-01-08 12:41:16 +00005564#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005565
5566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 * Get the value of 'equalprg', either the buffer-local one or the global one.
5568 */
5569 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005570get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571{
5572 if (*curbuf->b_p_ep == NUL)
5573 return p_ep;
5574 return curbuf->b_p_ep;
5575}
5576
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577/*
5578 * Copy options from one window to another.
5579 * Used when splitting a window.
5580 */
5581 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005582win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5585 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005586 after_copy_winopt(wp_to);
5587}
5588
5589/*
5590 * After copying window options: update variables depending on options.
5591 */
5592 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00005593after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005594{
5595#ifdef FEAT_LINEBREAK
5596 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005597#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005598#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005599 fill_culopt_flags(NULL, wp);
5600 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005601#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005602 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
5605/*
5606 * Copy the options from one winopt_T to another.
5607 * Doesn't free the old option values in "to", use clear_winopt() for that.
5608 * The 'scroll' option is not copied, because it depends on the window height.
5609 * The 'previewwindow' option is reset, there can be only one preview window.
5610 */
5611 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005612copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
5614#ifdef FEAT_ARABIC
5615 to->wo_arab = from->wo_arab;
5616#endif
5617 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005618 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005620 to->wo_rnu = from->wo_rnu;
Gary Johnson51ad8502021-08-03 18:33:08 +02005621 to->wo_ve = vim_strsave(from->wo_ve);
5622 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005623#ifdef FEAT_LINEBREAK
5624 to->wo_nuw = from->wo_nuw;
5625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626#ifdef FEAT_RIGHTLEFT
5627 to->wo_rl = from->wo_rl;
5628 to->wo_rlc = vim_strsave(from->wo_rlc);
5629#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005630#ifdef FEAT_LINEBREAK
5631 to->wo_sbr = vim_strsave(from->wo_sbr);
5632#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005633#ifdef FEAT_STL_OPT
5634 to->wo_stl = vim_strsave(from->wo_stl);
5635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005637#ifdef FEAT_DIFF
5638 to->wo_wrap_save = from->wo_wrap_save;
5639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640#ifdef FEAT_LINEBREAK
5641 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005642 to->wo_bri = from->wo_bri;
5643 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005645 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005647 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005648 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005649 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005650#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005651 to->wo_spell = from->wo_spell;
5652#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005653#ifdef FEAT_SYN_HL
5654 to->wo_cuc = from->wo_cuc;
5655 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005656 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005657 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659#ifdef FEAT_DIFF
5660 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005661 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005663#ifdef FEAT_CONCEAL
5664 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005665 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005666#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005667#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005668 to->wo_twk = vim_strsave(from->wo_twk);
5669 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671#ifdef FEAT_FOLDING
5672 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005673 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005675 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 to->wo_fdi = vim_strsave(from->wo_fdi);
5677 to->wo_fml = from->wo_fml;
5678 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005679 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005681 to->wo_fdm_save = from->wo_diff_saved
5682 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 to->wo_fdn = from->wo_fdn;
5684# ifdef FEAT_EVAL
5685 to->wo_fde = vim_strsave(from->wo_fde);
5686 to->wo_fdt = vim_strsave(from->wo_fdt);
5687# endif
5688 to->wo_fmr = vim_strsave(from->wo_fmr);
5689#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005690#ifdef FEAT_SIGNS
5691 to->wo_scl = vim_strsave(from->wo_scl);
5692#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005693
5694#ifdef FEAT_EVAL
5695 // Copy the script context so that we know where the value was last set.
5696 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5697 sizeof(to->wo_script_ctx));
5698#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005699 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700}
5701
5702/*
5703 * Check string options in a window for a NULL value.
5704 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005705 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005706check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708 check_winopt(&win->w_onebuf_opt);
5709 check_winopt(&win->w_allbuf_opt);
5710}
5711
5712/*
5713 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5714 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005715 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005716check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718#ifdef FEAT_FOLDING
5719 check_string_option(&wop->wo_fdi);
5720 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005721 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722# ifdef FEAT_EVAL
5723 check_string_option(&wop->wo_fde);
5724 check_string_option(&wop->wo_fdt);
5725# endif
5726 check_string_option(&wop->wo_fmr);
5727#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005728#ifdef FEAT_SIGNS
5729 check_string_option(&wop->wo_scl);
5730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731#ifdef FEAT_RIGHTLEFT
5732 check_string_option(&wop->wo_rlc);
5733#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005734#ifdef FEAT_LINEBREAK
5735 check_string_option(&wop->wo_sbr);
5736#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005737#ifdef FEAT_STL_OPT
5738 check_string_option(&wop->wo_stl);
5739#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005740#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005741 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005742 check_string_option(&wop->wo_cc);
5743#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005744#ifdef FEAT_CONCEAL
5745 check_string_option(&wop->wo_cocu);
5746#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005747#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005748 check_string_option(&wop->wo_twk);
5749 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005750#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005751#ifdef FEAT_LINEBREAK
5752 check_string_option(&wop->wo_briopt);
5753#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005754 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005755 check_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005756 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757}
5758
5759/*
5760 * Free the allocated memory inside a winopt_T.
5761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005763clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
5765#ifdef FEAT_FOLDING
5766 clear_string_option(&wop->wo_fdi);
5767 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005768 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769# ifdef FEAT_EVAL
5770 clear_string_option(&wop->wo_fde);
5771 clear_string_option(&wop->wo_fdt);
5772# endif
5773 clear_string_option(&wop->wo_fmr);
5774#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005775#ifdef FEAT_SIGNS
5776 clear_string_option(&wop->wo_scl);
5777#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005778#ifdef FEAT_LINEBREAK
5779 clear_string_option(&wop->wo_briopt);
5780#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005781 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782#ifdef FEAT_RIGHTLEFT
5783 clear_string_option(&wop->wo_rlc);
5784#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005785#ifdef FEAT_LINEBREAK
5786 clear_string_option(&wop->wo_sbr);
5787#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005788#ifdef FEAT_STL_OPT
5789 clear_string_option(&wop->wo_stl);
5790#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005791#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005792 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005793 clear_string_option(&wop->wo_cc);
5794#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005795#ifdef FEAT_CONCEAL
5796 clear_string_option(&wop->wo_cocu);
5797#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005798#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005799 clear_string_option(&wop->wo_twk);
5800 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005801#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005802 clear_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005803 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804}
5805
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005806#ifdef FEAT_EVAL
5807// Index into the options table for a buffer-local option enum.
5808static int buf_opt_idx[BV_COUNT];
5809# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5810
5811/*
5812 * Initialize buf_opt_idx[] if not done already.
5813 */
5814 static void
5815init_buf_opt_idx(void)
5816{
5817 static int did_init_buf_opt_idx = FALSE;
5818 int i;
5819
5820 if (did_init_buf_opt_idx)
5821 return;
5822 did_init_buf_opt_idx = TRUE;
5823 for (i = 0; !istermoption_idx(i); i++)
5824 if (options[i].indir & PV_BUF)
5825 buf_opt_idx[options[i].indir & PV_MASK] = i;
5826}
5827#else
5828# define COPY_OPT_SCTX(buf, bv)
5829#endif
5830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831/*
5832 * Copy global option values to local options for one buffer.
5833 * Used when creating a new buffer and sometimes when entering a buffer.
5834 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005835 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5837 * appropriate.
5838 * BCO_NOHELP Don't copy the values to a help buffer.
5839 */
5840 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005841buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842{
5843 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005844 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 int dont_do_help;
5846 int did_isk = FALSE;
5847
5848 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 * Skip this when the option defaults have not been set yet. Happens when
5850 * main() allocates the first buffer.
5851 */
5852 if (p_cpo != NULL)
5853 {
5854 /*
5855 * Always copy when entering and 'cpo' contains 'S'.
5856 * Don't copy when already initialized.
5857 * Don't copy when 'cpo' contains 's' and not entering.
5858 * 'S' BCO_ENTER initialized 's' should_copy
5859 * yes yes X X TRUE
5860 * yes no yes X FALSE
5861 * no X yes X FALSE
5862 * X no no yes FALSE
5863 * X no no no TRUE
5864 * no yes no X TRUE
5865 */
5866 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5867 && (buf->b_p_initialized
5868 || (!(flags & BCO_ENTER)
5869 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5870 should_copy = FALSE;
5871
5872 if (should_copy || (flags & BCO_ALWAYS))
5873 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005874#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005875 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005876 init_buf_opt_idx();
5877#endif
5878 // Don't copy the options specific to a help buffer when
5879 // BCO_NOHELP is given or the options were initialized already
5880 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5882 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005883 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 {
5885 save_p_isk = buf->b_p_isk;
5886 buf->b_p_isk = NULL;
5887 }
5888 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005889 * Always free the allocated strings. If not already initialized,
5890 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 */
5892 if (!buf->b_p_initialized)
5893 {
5894 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005895 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005898 switch (*p_ffs)
5899 {
5900 case 'm':
5901 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5902 case 'd':
5903 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5904 case 'u':
5905 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5906 default:
5907 buf->b_p_ff = vim_strsave(p_ff);
5908 }
5909 if (buf->b_p_ff != NULL)
5910 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 buf->b_p_bh = empty_option;
5912 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 }
5914 else
5915 free_buf_options(buf, FALSE);
5916
5917 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005918 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 buf->b_p_ai_nopaste = p_ai_nopaste;
5920 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005921 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005923 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 buf->b_p_tw_nopaste = p_tw_nopaste;
5925 buf->b_p_tw_nobin = p_tw_nobin;
5926 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005927 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 buf->b_p_wm_nopaste = p_wm_nopaste;
5929 buf->b_p_wm_nobin = p_wm_nobin;
5930 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005931 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005932 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005933 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005934 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005935 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005937 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005939 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005941 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 buf->b_p_ml_nobin = p_ml_nobin;
5943 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005944 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005945 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005946 buf->b_p_swf = FALSE;
5947 else
5948 {
5949 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00005950 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005953 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005954#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005955 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005956 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005958#ifdef FEAT_COMPL_FUNC
5959 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005960 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005961 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005962 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005963 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005964 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005965#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005966#ifdef FEAT_EVAL
5967 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005968 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005969 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005972 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005974#ifdef FEAT_VARTABS
5975 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005976 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005977 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02005978 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005979 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00005980 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005981 buf->b_p_vsts_nopaste = p_vsts_nopaste
5982 ? vim_strsave(p_vsts_nopaste) : NULL;
5983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005985 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005987 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988#ifdef FEAT_FOLDING
5989 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005990 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991#endif
5992 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005993 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005994 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005995 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005996 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5997 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005999 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006001 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006003 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006005 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006006
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006008 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006010 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006012 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006013 buf->b_p_cinsd = vim_strsave(p_cinsd);
6014 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006015
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006016 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006019 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006021 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006023 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006025 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006027 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006028 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006029 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006030#endif
6031#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006032 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006033 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006034 (void)compile_cap_prog(&buf->b_s);
6035 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006036 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006037 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006038 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006039 buf->b_s.b_p_spo = vim_strsave(p_spo);
6040 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006042#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006044 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006046 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006048 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006049#if defined(FEAT_EVAL)
6050 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006051 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053#ifdef FEAT_CRYPT
6054 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006055 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056#endif
6057#ifdef FEAT_SEARCHPATH
6058 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006059 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060#endif
6061#ifdef FEAT_KEYMAP
6062 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006063 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 buf->b_kmap_state |= KEYMAP_INIT;
6065#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006066#ifdef FEAT_TERMINAL
6067 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006068 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006069#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006070 // This isn't really an option, but copying the langmap and IME
6071 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006073 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006075 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006077 // options that are normally global but also have a local value
6078 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006080 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006081 buf->b_p_bkc = empty_option;
6082 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083#ifdef FEAT_QUICKFIX
6084 buf->b_p_gp = empty_option;
6085 buf->b_p_mp = empty_option;
6086 buf->b_p_efm = empty_option;
6087#endif
6088 buf->b_p_ep = empty_option;
6089 buf->b_p_kp = empty_option;
6090 buf->b_p_path = empty_option;
6091 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006092 buf->b_p_tc = empty_option;
6093 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094#ifdef FEAT_FIND_ID
6095 buf->b_p_def = empty_option;
6096 buf->b_p_inc = empty_option;
6097# ifdef FEAT_EVAL
6098 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006099 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100# endif
6101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 buf->b_p_dict = empty_option;
6103 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006104#ifdef FEAT_COMPL_FUNC
6105 buf->b_p_tsrfu = empty_option;
6106#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006107#ifdef FEAT_TEXTOBJ
6108 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006109 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006110#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006111#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6112 buf->b_p_bexpr = empty_option;
6113#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006114#if defined(FEAT_CRYPT)
6115 buf->b_p_cm = empty_option;
6116#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006117#ifdef FEAT_PERSISTENT_UNDO
6118 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006119 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006120#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006121 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006122 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123
6124 /*
6125 * Don't copy the options set by ex_help(), use the saved values,
6126 * when going from a help buffer to a non-help buffer.
6127 * Don't touch these at all when BCO_NOHELP is used and going from
6128 * or to a help buffer.
6129 */
6130 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006133#ifdef FEAT_VARTABS
6134 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006135 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006136 else
6137 buf->b_p_vts_array = NULL;
6138#endif
6139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 else
6141 {
6142 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006143 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 did_isk = TRUE;
6145 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006146 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006147#ifdef FEAT_VARTABS
6148 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006149 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006150 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006151 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006152 else
6153 buf->b_p_vts_array = NULL;
6154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 if (buf->b_p_bt[0] == 'h')
6157 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006159 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 }
6161 }
6162
6163 /*
6164 * When the options should be copied (ignoring BCO_ALWAYS), set the
6165 * flag that indicates that the options have been initialized.
6166 */
6167 if (should_copy)
6168 buf->b_p_initialized = TRUE;
6169 }
6170
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006171 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (did_isk)
6173 (void)buf_init_chartab(buf, FALSE);
6174}
6175
6176/*
6177 * Reset the 'modifiable' option and its default value.
6178 */
6179 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006180reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
6182 int opt_idx;
6183
6184 curbuf->b_p_ma = FALSE;
6185 p_ma = FALSE;
6186 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006187 if (opt_idx >= 0)
6188 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189}
6190
6191/*
6192 * Set the global value for 'iminsert' to the local value.
6193 */
6194 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006195set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196{
6197 p_iminsert = curbuf->b_p_iminsert;
6198}
6199
6200/*
6201 * Set the global value for 'imsearch' to the local value.
6202 */
6203 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006204set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205{
6206 p_imsearch = curbuf->b_p_imsearch;
6207}
6208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209static int expand_option_idx = -1;
6210static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6211static int expand_option_flags = 0;
6212
6213 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006214set_context_in_set_cmd(
6215 expand_T *xp,
6216 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006217 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218{
6219 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006220 long_u flags = 0; // init for GCC
6221 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 char_u *p;
6223 char_u *s;
6224 int is_term_option = FALSE;
6225 int key;
6226
6227 expand_option_flags = opt_flags;
6228
6229 xp->xp_context = EXPAND_SETTINGS;
6230 if (*arg == NUL)
6231 {
6232 xp->xp_pattern = arg;
6233 return;
6234 }
6235 p = arg + STRLEN(arg) - 1;
6236 if (*p == ' ' && *(p - 1) != '\\')
6237 {
6238 xp->xp_pattern = p + 1;
6239 return;
6240 }
6241 while (p > arg)
6242 {
6243 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006244 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 if (*p == ' ' || *p == ',')
6246 {
6247 while (s > arg && *(s - 1) == '\\')
6248 --s;
6249 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006250 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 if (*p == ' ' && ((p - s) & 1) == 0)
6252 {
6253 ++p;
6254 break;
6255 }
6256 --p;
6257 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006258 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {
6260 xp->xp_context = EXPAND_BOOL_SETTINGS;
6261 p += 2;
6262 }
6263 if (STRNCMP(p, "inv", 3) == 0)
6264 {
6265 xp->xp_context = EXPAND_BOOL_SETTINGS;
6266 p += 3;
6267 }
6268 xp->xp_pattern = arg = p;
6269 if (*arg == '<')
6270 {
6271 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006272 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 return;
6274 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006275 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 {
6277 xp->xp_context = EXPAND_NOTHING;
6278 return;
6279 }
6280 nextchar = *++p;
6281 is_term_option = TRUE;
6282 expand_option_name[2] = KEY2TERMCAP0(key);
6283 expand_option_name[3] = KEY2TERMCAP1(key);
6284 }
6285 else
6286 {
6287 if (p[0] == 't' && p[1] == '_')
6288 {
6289 p += 2;
6290 if (*p != NUL)
6291 ++p;
6292 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006293 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 nextchar = *++p;
6295 is_term_option = TRUE;
6296 expand_option_name[2] = p[-2];
6297 expand_option_name[3] = p[-1];
6298 }
6299 else
6300 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006301 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6303 p++;
6304 if (*p == NUL)
6305 return;
6306 nextchar = *p;
6307 *p = NUL;
6308 opt_idx = findoption(arg);
6309 *p = nextchar;
6310 if (opt_idx == -1 || options[opt_idx].var == NULL)
6311 {
6312 xp->xp_context = EXPAND_NOTHING;
6313 return;
6314 }
6315 flags = options[opt_idx].flags;
6316 if (flags & P_BOOL)
6317 {
6318 xp->xp_context = EXPAND_NOTHING;
6319 return;
6320 }
6321 }
6322 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006323 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6325 {
6326 ++p;
6327 nextchar = '=';
6328 }
6329 if ((nextchar != '=' && nextchar != ':')
6330 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6331 {
6332 xp->xp_context = EXPAND_UNSUCCESSFUL;
6333 return;
6334 }
6335 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6336 {
6337 xp->xp_context = EXPAND_OLD_SETTING;
6338 if (is_term_option)
6339 expand_option_idx = -1;
6340 else
6341 expand_option_idx = opt_idx;
6342 xp->xp_pattern = p + 1;
6343 return;
6344 }
6345 xp->xp_context = EXPAND_NOTHING;
6346 if (is_term_option || (flags & P_NUM))
6347 return;
6348
6349 xp->xp_pattern = p + 1;
6350
6351 if (flags & P_EXPAND)
6352 {
6353 p = options[opt_idx].var;
6354 if (p == (char_u *)&p_bdir
6355 || p == (char_u *)&p_dir
6356 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006357 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 || p == (char_u *)&p_rtp
6359#ifdef FEAT_SEARCHPATH
6360 || p == (char_u *)&p_cdpath
6361#endif
6362#ifdef FEAT_SESSION
6363 || p == (char_u *)&p_vdir
6364#endif
6365 )
6366 {
6367 xp->xp_context = EXPAND_DIRECTORIES;
6368 if (p == (char_u *)&p_path
6369#ifdef FEAT_SEARCHPATH
6370 || p == (char_u *)&p_cdpath
6371#endif
6372 )
6373 xp->xp_backslash = XP_BS_THREE;
6374 else
6375 xp->xp_backslash = XP_BS_ONE;
6376 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006377 else if (p == (char_u *)&p_ft)
6378 {
6379 xp->xp_context = EXPAND_FILETYPE;
6380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 else
6382 {
6383 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006384 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 if (p == (char_u *)&p_tags)
6386 xp->xp_backslash = XP_BS_THREE;
6387 else
6388 xp->xp_backslash = XP_BS_ONE;
6389 }
6390 }
6391
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006392 // For an option that is a list of file names, find the start of the
6393 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6395 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006396 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 if (*p == ' ' || *p == ',')
6398 {
6399 s = p;
6400 while (s > xp->xp_pattern && *(s - 1) == '\\')
6401 --s;
6402 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6403 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6404 {
6405 xp->xp_pattern = p + 1;
6406 break;
6407 }
6408 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006409
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006410#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006411 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006412 if (options[opt_idx].var == (char_u *)&p_sps
6413 && STRNCMP(p, "file:", 5) == 0)
6414 {
6415 xp->xp_pattern = p + 5;
6416 break;
6417 }
6418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420}
6421
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006422/*
6423 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
6424 *
6425 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
6426 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
6427 *
6428 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
6429 * regular expression 'regmatch', then stores the match in matches[idx] and
6430 * returns TRUE.
6431 *
6432 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6433 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
6434 *
6435 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6436 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
6437 */
6438 static int
6439match_str(
6440 char_u *str,
6441 regmatch_T *regmatch,
6442 char_u **matches,
6443 int idx,
6444 int test_only,
6445 int fuzzy,
6446 char_u *fuzzystr,
6447 fuzmatch_str_T *fuzmatch)
6448{
6449 if (!fuzzy)
6450 {
6451 if (vim_regexec(regmatch, str, (colnr_T)0))
6452 {
6453 if (!test_only)
6454 matches[idx] = vim_strsave(str);
6455 return TRUE;
6456 }
6457 }
6458 else
6459 {
6460 int score;
6461
6462 score = fuzzy_match_str(str, fuzzystr);
6463 if (score != 0)
6464 {
6465 if (!test_only)
6466 {
6467 fuzmatch[idx].idx = idx;
6468 fuzmatch[idx].str = vim_strsave(str);
6469 fuzmatch[idx].score = score;
6470 }
6471
6472 return TRUE;
6473 }
6474 }
6475
6476 return FALSE;
6477}
6478
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006480ExpandSettings(
6481 expand_T *xp,
6482 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006483 char_u *fuzzystr,
6484 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01006485 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006486 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006488 int num_normal = 0; // Nr of matching non-term-code settings
6489 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 int opt_idx;
6491 int match;
6492 int count = 0;
6493 char_u *str;
6494 int loop;
6495 int is_term_opt;
6496 char_u name_buf[MAX_KEY_NAME_LEN];
6497 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006498 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006499 int fuzzy;
6500 fuzmatch_str_T *fuzmatch = NULL;
6501
Christian Brabandtcb747892022-05-08 21:10:56 +01006502 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006504 // do this loop twice:
6505 // loop == 0: count the number of matching options
6506 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 for (loop = 0; loop <= 1; ++loop)
6508 {
6509 regmatch->rm_ic = ic;
6510 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6511 {
K.Takataeeec2542021-06-02 13:28:16 +02006512 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006513 {
6514 if (match_str((char_u *)names[match], regmatch, *matches,
6515 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 {
6517 if (loop == 0)
6518 num_normal++;
6519 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006520 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 }
6524 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6525 opt_idx++)
6526 {
6527 if (options[opt_idx].var == NULL)
6528 continue;
6529 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6530 && !(options[opt_idx].flags & P_BOOL))
6531 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006532 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (is_term_opt && num_normal > 0)
6534 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006535
6536 if (match_str(str, regmatch, *matches, count, (loop == 0),
6537 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
6539 if (loop == 0)
6540 {
6541 if (is_term_opt)
6542 num_term++;
6543 else
6544 num_normal++;
6545 }
6546 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006547 count++;
6548 }
6549 else if (!fuzzy && options[opt_idx].shortname != NULL
6550 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006551 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006552 {
6553 // Compare against the abbreviated option name (for regular
6554 // expression match). Fuzzy matching (previous if) already
6555 // matches against both the expanded and abbreviated names.
6556 if (loop == 0)
6557 {
6558 if (is_term_opt)
6559 num_term++;
6560 else
6561 num_normal++;
6562 }
6563 else
6564 (*matches)[count++] = vim_strsave(str);
6565 }
6566 else if (is_term_opt)
6567 {
6568 name_buf[0] = '<';
6569 name_buf[1] = 't';
6570 name_buf[2] = '_';
6571 name_buf[3] = str[2];
6572 name_buf[4] = str[3];
6573 name_buf[5] = '>';
6574 name_buf[6] = NUL;
6575
6576 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6577 fuzzy, fuzzystr, fuzmatch))
6578 {
6579 if (loop == 0)
6580 num_term++;
6581 else
6582 count++;
6583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 }
6585 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006586
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 /*
6588 * Check terminal key codes, these are not in the option table
6589 */
6590 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6591 {
6592 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6593 {
6594 if (!isprint(str[0]) || !isprint(str[1]))
6595 continue;
6596
6597 name_buf[0] = 't';
6598 name_buf[1] = '_';
6599 name_buf[2] = str[0];
6600 name_buf[3] = str[1];
6601 name_buf[4] = NUL;
6602
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006603 if (match_str(name_buf, regmatch, *matches, count,
6604 (loop == 0), fuzzy, fuzzystr, fuzmatch))
6605 {
6606 if (loop == 0)
6607 num_term++;
6608 else
6609 count++;
6610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 else
6612 {
6613 name_buf[0] = '<';
6614 name_buf[1] = 't';
6615 name_buf[2] = '_';
6616 name_buf[3] = str[0];
6617 name_buf[4] = str[1];
6618 name_buf[5] = '>';
6619 name_buf[6] = NUL;
6620
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006621 if (match_str(name_buf, regmatch, *matches, count,
6622 (loop == 0), fuzzy, fuzzystr,
6623 fuzmatch))
6624 {
6625 if (loop == 0)
6626 num_term++;
6627 else
6628 count++;
6629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 }
6631 }
6632
6633 /*
6634 * Check special key names.
6635 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006636 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6638 {
6639 name_buf[0] = '<';
6640 STRCPY(name_buf + 1, str);
6641 STRCAT(name_buf, ">");
6642
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006643 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6644 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 {
6646 if (loop == 0)
6647 num_term++;
6648 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006649 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 }
6651 }
6652 }
6653 if (loop == 0)
6654 {
6655 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006656 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006658 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 else
6660 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006661 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006663 *matches = ALLOC_MULT(char_u *, *numMatches);
6664 if (*matches == NULL)
6665 {
6666 *matches = (char_u **)"";
6667 return FAIL;
6668 }
6669 }
6670 else
6671 {
6672 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
6673 if (fuzmatch == NULL)
6674 {
6675 *matches = (char_u **)"";
6676 return FAIL;
6677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 }
6679 }
6680 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006681
6682 if (fuzzy &&
6683 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
6684 return FAIL;
6685
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 return OK;
6687}
6688
6689 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006690ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006692 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 char_u *buf;
6694
6695 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006696 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 if (*file == NULL)
6698 return FAIL;
6699
6700 /*
6701 * For a terminal key code expand_option_idx is < 0.
6702 */
6703 if (expand_option_idx < 0)
6704 {
6705 var = find_termcode(expand_option_name + 2);
6706 if (var == NULL)
6707 expand_option_idx = findoption(expand_option_name);
6708 }
6709
6710 if (expand_option_idx >= 0)
6711 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006712 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 option_value2string(&options[expand_option_idx], expand_option_flags);
6714 var = NameBuff;
6715 }
6716 else if (var == NULL)
6717 var = (char_u *)"";
6718
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006719 // A backslash is required before some characters. This is the reverse of
6720 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 buf = vim_strsave_escaped(var, escape_chars);
6722
6723 if (buf == NULL)
6724 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006725 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 return FAIL;
6727 }
6728
6729#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006730 // For MS-Windows et al. we don't double backslashes at the start and
6731 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006732 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 if (var[0] == '\\' && var[1] == '\\'
6734 && expand_option_idx >= 0
6735 && (options[expand_option_idx].flags & P_EXPAND)
6736 && vim_isfilec(var[2])
6737 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006738 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739#endif
6740
6741 *file[0] = buf;
6742 *num_file = 1;
6743 return OK;
6744}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745
6746/*
6747 * Get the value for the numeric or string option *opp in a nice format into
6748 * NameBuff[]. Must not be called with a hidden option!
6749 */
6750 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006751option_value2string(
6752 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006753 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 char_u *varp;
6756
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006757 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
6759 if (opp->flags & P_NUM)
6760 {
6761 long wc = 0;
6762
6763 if (wc_use_keyname(varp, &wc))
6764 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6765 else if (wc != 0)
6766 STRCPY(NameBuff, transchar((int)wc));
6767 else
6768 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6769 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006770 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {
6772 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006773 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 NameBuff[0] = NUL;
6775#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006776 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006777 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 STRCPY(NameBuff, "*****");
6779#endif
6780 else if (opp->flags & P_EXPAND)
6781 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006782 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 else if ((char_u **)opp->var == &p_pt)
6784 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6785 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006786 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 }
6788}
6789
6790/*
6791 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6792 * printed as a keyname.
6793 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6794 */
6795 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006796wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797{
6798 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6799 {
6800 *wcp = *(long *)varp;
6801 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6802 return TRUE;
6803 }
6804 return FALSE;
6805}
6806
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 * Return TRUE if "x" is present in 'shortmess' option, or
6809 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6810 */
6811 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006812shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006814 return p_shm != NULL &&
6815 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 || (vim_strchr(p_shm, 'a') != NULL
6817 && vim_strchr((char_u *)SHM_A, x) != NULL));
6818}
6819
6820/*
6821 * paste_option_changed() - Called after p_paste was set or reset.
6822 */
6823 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006824paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825{
6826 static int old_p_paste = FALSE;
6827 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006828 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829#ifdef FEAT_CMDL_INFO
6830 static int save_ru = 0;
6831#endif
6832#ifdef FEAT_RIGHTLEFT
6833 static int save_ri = 0;
6834 static int save_hkmap = 0;
6835#endif
6836 buf_T *buf;
6837
6838 if (p_paste)
6839 {
6840 /*
6841 * Paste switched from off to on.
6842 * Save the current values, so they can be restored later.
6843 */
6844 if (!old_p_paste)
6845 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006846 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006847 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {
6849 buf->b_p_tw_nopaste = buf->b_p_tw;
6850 buf->b_p_wm_nopaste = buf->b_p_wm;
6851 buf->b_p_sts_nopaste = buf->b_p_sts;
6852 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006853 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006854#ifdef FEAT_VARTABS
6855 if (buf->b_p_vsts_nopaste)
6856 vim_free(buf->b_p_vsts_nopaste);
6857 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6858 ? vim_strsave(buf->b_p_vsts) : NULL;
6859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 }
6861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006862 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006864 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865#ifdef FEAT_CMDL_INFO
6866 save_ru = p_ru;
6867#endif
6868#ifdef FEAT_RIGHTLEFT
6869 save_ri = p_ri;
6870 save_hkmap = p_hkmap;
6871#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006872 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006873 p_ai_nopaste = p_ai;
6874 p_et_nopaste = p_et;
6875 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 p_tw_nopaste = p_tw;
6877 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006878#ifdef FEAT_VARTABS
6879 if (p_vsts_nopaste)
6880 vim_free(p_vsts_nopaste);
6881 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 }
6884
6885 /*
6886 * Always set the option values, also when 'paste' is set when it is
6887 * already on.
6888 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006889 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006890 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006892 buf->b_p_tw = 0; // textwidth is 0
6893 buf->b_p_wm = 0; // wrapmargin is 0
6894 buf->b_p_sts = 0; // softtabstop is 0
6895 buf->b_p_ai = 0; // no auto-indent
6896 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006897#ifdef FEAT_VARTABS
6898 if (buf->b_p_vsts)
6899 free_string_option(buf->b_p_vsts);
6900 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006901 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 }
6904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006905 // set global options
6906 p_sm = 0; // no showmatch
6907 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006910 status_redraw_all(); // redraw to remove the ruler
6911 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912#endif
6913#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006914 p_ri = 0; // no reverse insert
6915 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006917 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 p_tw = 0;
6919 p_wm = 0;
6920 p_sts = 0;
6921 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006922#ifdef FEAT_VARTABS
6923 if (p_vsts)
6924 free_string_option(p_vsts);
6925 p_vsts = empty_option;
6926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 }
6928
6929 /*
6930 * Paste switched from on to off: Restore saved values.
6931 */
6932 else if (old_p_paste)
6933 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006934 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006935 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {
6937 buf->b_p_tw = buf->b_p_tw_nopaste;
6938 buf->b_p_wm = buf->b_p_wm_nopaste;
6939 buf->b_p_sts = buf->b_p_sts_nopaste;
6940 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006941 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006942#ifdef FEAT_VARTABS
6943 if (buf->b_p_vsts)
6944 free_string_option(buf->b_p_vsts);
6945 buf->b_p_vsts = buf->b_p_vsts_nopaste
6946 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006947 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006948 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006949 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006950 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006951 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 }
6954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006955 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006957 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006960 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 p_ru = save_ru;
6962#endif
6963#ifdef FEAT_RIGHTLEFT
6964 p_ri = save_ri;
6965 p_hkmap = save_hkmap;
6966#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006967 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006968 p_ai = p_ai_nopaste;
6969 p_et = p_et_nopaste;
6970 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 p_tw = p_tw_nopaste;
6972 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006973#ifdef FEAT_VARTABS
6974 if (p_vsts)
6975 free_string_option(p_vsts);
6976 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 }
6979
6980 old_p_paste = p_paste;
6981}
6982
6983/*
6984 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6985 *
6986 * Reset 'compatible' and set the values for options that didn't get set yet
6987 * to the Vim defaults.
6988 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006989 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 */
6991 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006992vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006994 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006995 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006996 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
6998 if (!option_was_set((char_u *)"cp"))
6999 {
7000 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007001 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7003 set_option_default(opt_idx, OPT_FREE, FALSE);
7004 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007005 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007007
7008 if (fname != NULL)
7009 {
7010 p = vim_getenv(envname, &dofree);
7011 if (p == NULL)
7012 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007013 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007014 p = FullName_save(fname, FALSE);
7015 if (p != NULL)
7016 {
7017 vim_setenv(envname, p);
7018 vim_free(p);
7019 }
7020 }
7021 else if (dofree)
7022 vim_free(p);
7023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024}
7025
7026/*
7027 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7028 */
7029 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007030change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007032 int opt_idx;
7033
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 if (p_cp != on)
7035 {
7036 p_cp = on;
7037 compatible_set();
7038 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007039 opt_idx = findoption((char_u *)"cp");
7040 if (opt_idx >= 0)
7041 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042}
7043
7044/*
7045 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007046 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 */
7048 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007049option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050{
7051 int idx;
7052
7053 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007054 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 return FALSE;
7056 if (options[idx].flags & P_WAS_SET)
7057 return TRUE;
7058 return FALSE;
7059}
7060
7061/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007062 * Reset the flag indicating option "name" was set.
7063 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007064 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007065reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007066{
7067 int idx = findoption(name);
7068
7069 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007070 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007071 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007072 return OK;
7073 }
7074 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007075}
7076
7077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 * compatible_set() - Called when 'compatible' has been set or unset.
7079 *
7080 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7081 * flag) to a Vi compatible value.
7082 * When 'compatible' is unset: Set all options that have a different default
7083 * for Vim (without the P_VI_DEF flag) to that default.
7084 */
7085 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007086compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087{
7088 int opt_idx;
7089
Bram Moolenaardac13472019-09-16 21:06:21 +02007090 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7092 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7093 set_option_default(opt_idx, OPT_FREE, p_cp);
7094 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007095 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096}
7097
Bram Moolenaardac13472019-09-16 21:06:21 +02007098#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100/*
7101 * fill_breakat_flags() -- called when 'breakat' changes value.
7102 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007103 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007104fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007106 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 int i;
7108
7109 for (i = 0; i < 256; i++)
7110 breakat_flags[i] = FALSE;
7111
7112 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007113 for (p = p_breakat; *p; p++)
7114 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116#endif
7117
7118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 * Check if backspacing over something is allowed.
7120 */
7121 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007122can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007123 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007125#ifdef FEAT_JOB_CHANNEL
7126 if (what == BS_START && bt_prompt(curbuf))
7127 return FALSE;
7128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 switch (*p_bs)
7130 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007131 case '3': return TRUE;
7132 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 case '1': return (what != BS_START);
7134 case '0': return FALSE;
7135 }
7136 return vim_strchr(p_bs, what) != NULL;
7137}
7138
7139/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007140 * Return the effective 'scrolloff' value for the current window, using the
7141 * global value when appropriate.
7142 */
7143 long
7144get_scrolloff_value(void)
7145{
7146 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7147}
7148
7149/*
7150 * Return the effective 'sidescrolloff' value for the current window, using the
7151 * global value when appropriate.
7152 */
7153 long
7154get_sidescrolloff_value(void)
7155{
7156 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7157}
7158
7159/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007160 * Get the local or global value of 'backupcopy'.
7161 */
7162 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007163get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007164{
7165 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7166}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007167
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007168#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007169/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007170 * Get the local or global value of 'formatlistpat'.
7171 */
7172 char_u *
7173get_flp_value(buf_T *buf)
7174{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007175 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7176 return p_flp;
7177 return buf->b_p_flp;
7178}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007179#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007180
7181/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007182 * Get the local or global value of the 'virtualedit' flags.
7183 */
7184 unsigned int
7185get_ve_flags(void)
7186{
Gary Johnson51ad8502021-08-03 18:33:08 +02007187 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7188 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007189}
7190
Bram Moolenaaree857022019-11-09 23:26:40 +01007191#if defined(FEAT_LINEBREAK) || defined(PROTO)
7192/*
7193 * Get the local or global value of 'showbreak'.
7194 */
7195 char_u *
7196get_showbreak_value(win_T *win)
7197{
7198 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7199 return p_sbr;
7200 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7201 return empty_option;
7202 return win->w_p_sbr;
7203}
7204#endif
7205
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007206#if defined(FEAT_EVAL) || defined(PROTO)
7207/*
7208 * Get window or buffer local options.
7209 */
7210 dict_T *
7211get_winbuf_options(int bufopt)
7212{
7213 dict_T *d;
7214 int opt_idx;
7215
7216 d = dict_alloc();
7217 if (d == NULL)
7218 return NULL;
7219
Bram Moolenaardac13472019-09-16 21:06:21 +02007220 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007221 {
7222 struct vimoption *opt = &options[opt_idx];
7223
7224 if ((bufopt && (opt->indir & PV_BUF))
7225 || (!bufopt && (opt->indir & PV_WIN)))
7226 {
7227 char_u *varp = get_varp(opt);
7228
7229 if (varp != NULL)
7230 {
7231 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007232 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007233 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007234 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007235 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007236 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007237 }
7238 }
7239 }
7240
7241 return d;
7242}
7243#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007244
Bram Moolenaardac13472019-09-16 21:06:21 +02007245#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007246/*
7247 * This is called when 'culopt' is changed
7248 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007249 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007250fill_culopt_flags(char_u *val, win_T *wp)
7251{
7252 char_u *p;
7253 char_u culopt_flags_new = 0;
7254
7255 if (val == NULL)
7256 p = wp->w_p_culopt;
7257 else
7258 p = val;
7259 while (*p != NUL)
7260 {
7261 if (STRNCMP(p, "line", 4) == 0)
7262 {
7263 p += 4;
7264 culopt_flags_new |= CULOPT_LINE;
7265 }
7266 else if (STRNCMP(p, "both", 4) == 0)
7267 {
7268 p += 4;
7269 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7270 }
7271 else if (STRNCMP(p, "number", 6) == 0)
7272 {
7273 p += 6;
7274 culopt_flags_new |= CULOPT_NBR;
7275 }
7276 else if (STRNCMP(p, "screenline", 10) == 0)
7277 {
7278 p += 10;
7279 culopt_flags_new |= CULOPT_SCRLINE;
7280 }
7281
7282 if (*p != ',' && *p != NUL)
7283 return FAIL;
7284 if (*p == ',')
7285 ++p;
7286 }
7287
7288 // Can't have both "line" and "screenline".
7289 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7290 return FAIL;
7291 wp->w_p_culopt_flags = culopt_flags_new;
7292
7293 return OK;
7294}
7295#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007296
7297/*
7298 * Get the value of 'magic' adjusted for Vim9 script.
7299 */
7300 int
7301magic_isset(void)
7302{
7303 switch (magic_overruled)
7304 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007305 case OPTION_MAGIC_ON: return TRUE;
7306 case OPTION_MAGIC_OFF: return FALSE;
7307 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007308 }
7309#ifdef FEAT_EVAL
7310 if (in_vim9script())
7311 return TRUE;
7312#endif
7313 return p_magic;
7314}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007315
7316/*
7317 * Set the callback function value for an option that accepts a function name,
7318 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7319 * Returns OK if the option is successfully set to a function, otherwise
7320 * returns FAIL.
7321 */
7322 int
7323option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7324{
7325#ifdef FEAT_EVAL
7326 typval_T *tv;
7327 callback_T cb;
7328
7329 if (optval == NULL || *optval == NUL)
7330 {
7331 free_callback(optcb);
7332 return OK;
7333 }
7334
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007335 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007336 || (STRNCMP(optval, "function(", 9) == 0)
7337 || (STRNCMP(optval, "funcref(", 8) == 0))
7338 // Lambda expression or a funcref
7339 tv = eval_expr(optval, NULL);
7340 else
7341 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007342 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007343 if (tv == NULL)
7344 return FAIL;
7345
7346 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007347 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007348 {
7349 free_tv(tv);
7350 return FAIL;
7351 }
7352
7353 free_callback(optcb);
7354 set_callback(optcb, &cb);
7355 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007356
7357 // when using Vim9 style "import.funcname" it needs to be expanded to
7358 // "import#funcname".
7359 expand_autload_callback(optcb);
7360
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007361 return OK;
7362#else
7363 return FAIL;
7364#endif
7365}