blob: f3c53b2282c4c2f7165b2058c4f2327d30bde582 [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 Moolenaara4d158b2022-08-14 14:17:45 +01001281 redraw_all_later(UPD_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 Moolenaar071d4272004-06-13 20:20:40 +00001719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001720 // When using ":set opt=val" for a global option
1721 // with a local value the local value will be
1722 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001724 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 varp = options[opt_idx].var;
1726
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001727 // The old value is kept until we are sure that the
1728 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001730
Bram Moolenaard7c96872019-06-15 17:12:48 +02001731 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1732 {
1733 origval_l = *(char_u **)get_varp_scope(
1734 &(options[opt_idx]), OPT_LOCAL);
1735 origval_g = *(char_u **)get_varp_scope(
1736 &(options[opt_idx]), OPT_GLOBAL);
1737
1738 // A global-local string option might have an empty
1739 // option as value to indicate that the global
1740 // value should be used.
1741 if (((int)options[opt_idx].indir & PV_BOTH)
1742 && origval_l == empty_option)
1743 origval_l = origval_g;
1744 }
1745
1746 // When setting the local value of a global
1747 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001748 if (((int)options[opt_idx].indir & PV_BOTH)
1749 && (opt_flags & OPT_LOCAL))
1750 origval = *(char_u **)get_varp(
1751 &options[opt_idx]);
1752 else
1753 origval = oldval;
1754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001755 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 {
1757 newval = options[opt_idx].def_val[
1758 ((flags & P_VI_DEF) || cp_val)
1759 ? VI_DEFAULT : VIM_DEFAULT];
1760 if ((char_u **)varp == &p_bg)
1761 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001762 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763#ifdef FEAT_GUI
1764 if (gui.in_use)
1765 newval = gui_bg_default();
1766 else
1767#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001768 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001770 else if ((char_u **)varp == &p_fencs && enc_utf8)
1771 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001773 // expand environment variables and ~ since the
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001774 // default value was already expanded, only
1775 // required when an environment variable was set
1776 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 if (newval == NULL)
1778 newval = empty_option;
1779 else
1780 {
1781 s = option_expand(opt_idx, newval);
1782 if (s == NULL)
1783 s = newval;
1784 newval = vim_strsave(s);
1785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001787 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 {
1789 newval = vim_strsave(*(char_u **)get_varp_scope(
1790 &(options[opt_idx]), OPT_GLOBAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 }
1792 else
1793 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001794 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796 /*
1797 * Set 'keywordprg' to ":help" if an empty
1798 * value was passed to :set by the user.
1799 * Misuse errbuf[] for the resulting string.
1800 */
1801 if (varp == (char_u *)&p_kp
1802 && (*arg == NUL || *arg == ' '))
1803 {
1804 STRCPY(errbuf, ":help");
1805 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001806 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
1808 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001809 * Convert 'backspace' number to string, for
1810 * adding, prepending and removing string.
1811 */
1812 else if (varp == (char_u *)&p_bs
1813 && VIM_ISDIGIT(**(char_u **)varp))
1814 {
1815 i = getdigits((char_u **)varp);
1816 switch (i)
1817 {
1818 case 0:
1819 *(char_u **)varp = empty_option;
1820 break;
1821 case 1:
1822 *(char_u **)varp = vim_strsave(
1823 (char_u *)"indent,eol");
1824 break;
1825 case 2:
1826 *(char_u **)varp = vim_strsave(
1827 (char_u *)"indent,eol,start");
1828 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001829 case 3:
1830 *(char_u **)varp = vim_strsave(
1831 (char_u *)"indent,eol,nostop");
1832 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001833 }
1834 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001835 if (origval == oldval)
1836 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001837 if (origval_l == oldval)
1838 origval_l = *(char_u **)varp;
1839 if (origval_g == oldval)
1840 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001841 oldval = *(char_u **)varp;
1842 }
1843 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 * Convert 'whichwrap' number to string, for
1845 * backwards compatibility with Vim 3.0.
1846 * Misuse errbuf[] for the resulting string.
1847 */
1848 else if (varp == (char_u *)&p_ww
1849 && VIM_ISDIGIT(*arg))
1850 {
1851 *errbuf = NUL;
1852 i = getdigits(&arg);
1853 if (i & 1)
1854 STRCAT(errbuf, "b,");
1855 if (i & 2)
1856 STRCAT(errbuf, "s,");
1857 if (i & 4)
1858 STRCAT(errbuf, "h,l,");
1859 if (i & 8)
1860 STRCAT(errbuf, "<,>,");
1861 if (i & 16)
1862 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001863 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 errbuf[STRLEN(errbuf) - 1] = NUL;
1865 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001866 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 /*
1869 * Remove '>' before 'dir' and 'bdir', for
1870 * backwards compatibility with version 3.0
1871 */
1872 else if ( *arg == '>'
1873 && (varp == (char_u *)&p_dir
1874 || varp == (char_u *)&p_bdir))
1875 {
1876 ++arg;
1877 }
1878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 /*
1880 * Copy the new string into allocated memory.
1881 * Can't use set_string_option_direct(), because
1882 * we need to remove the backslashes.
1883 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001884 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 newlen = (unsigned)STRLEN(arg) + 1;
1886 if (adding || prepending || removing)
1887 newlen += (unsigned)STRLEN(origval) + 1;
1888 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001889 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 break;
1891 s = newval;
1892
1893 /*
1894 * Copy the string, skip over escaped chars.
1895 * For MS-DOS and WIN32 backslashes before normal
1896 * file name characters are not removed, and keep
1897 * backslash at start, for "\\machine\path", but
1898 * do remove it for "\\\\machine\\path".
1899 * The reverse is found in ExpandOldSetting().
1900 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001901 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 {
1903 if (*arg == '\\' && arg[1] != NUL
1904#ifdef BACKSLASH_IN_FILENAME
1905 && !((flags & P_EXPAND)
1906 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001907 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 && (arg[1] != '\\'
1909 || (s == newval
1910 && arg[2] != '\\')))
1911#endif
1912 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001913 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001915 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001917 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 mch_memmove(s, arg, (size_t)i);
1919 arg += i;
1920 s += i;
1921 }
1922 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 *s++ = *arg++;
1924 }
1925 *s = NUL;
1926
1927 /*
1928 * Expand environment variables and ~.
1929 * Don't do it when adding without inserting a
1930 * comma.
1931 */
1932 if (!(adding || prepending || removing)
1933 || (flags & P_COMMA))
1934 {
1935 s = option_expand(opt_idx, newval);
1936 if (s != NULL)
1937 {
1938 vim_free(newval);
1939 newlen = (unsigned)STRLEN(s) + 1;
1940 if (adding || prepending || removing)
1941 newlen += (unsigned)STRLEN(origval) + 1;
1942 newval = alloc(newlen);
1943 if (newval == NULL)
1944 break;
1945 STRCPY(newval, s);
1946 }
1947 }
1948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001949 // locate newval[] in origval[] when removing it
1950 // and when adding to avoid duplicates
1951 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (removing || (flags & P_NODUP))
1953 {
1954 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001955 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001957 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001958 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
1960 prepending = FALSE;
1961 adding = FALSE;
1962 STRCPY(newval, origval);
1963 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001964
1965 // if no duplicate, move pointer to end of
1966 // original value
1967 if (s == NULL)
1968 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
1970
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001971 // concatenate the two strings; add a ',' if
1972 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 if (adding || prepending)
1974 {
1975 comma = ((flags & P_COMMA) && *origval != NUL
1976 && *newval != NUL);
1977 if (adding)
1978 {
1979 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001980 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001981 if (comma && i > 1
1982 && (flags & P_ONECOMMA) == P_ONECOMMA
1983 && origval[i - 1] == ','
1984 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001985 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 mch_memmove(newval + i + comma, newval,
1987 STRLEN(newval) + 1);
1988 mch_memmove(newval, origval, (size_t)i);
1989 }
1990 else
1991 {
1992 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001993 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 }
1995 if (comma)
1996 newval[i] = ',';
1997 }
1998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001999 // Remove newval[] from origval[]. (Note: "i" has
2000 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 if (removing)
2002 {
2003 STRCPY(newval, origval);
2004 if (*s)
2005 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002006 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 if (flags & P_COMMA)
2008 {
2009 if (s == origval)
2010 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002011 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (s[i] == ',')
2013 ++i;
2014 }
2015 else
2016 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002017 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 --s;
2019 ++i;
2020 }
2021 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002022 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 }
2024 }
2025
2026 if (flags & P_FLAGLIST)
2027 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002028 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002029 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002030 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002031 // if options have P_FLAGLIST and
2032 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002033 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002035 if (*s != ',' && *(s + 1) == ','
2036 && vim_strchr(s + 2, *s) != NULL)
2037 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002038 // Remove the duplicated value and
2039 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002040 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002041 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002044 else
2045 {
2046 if ((!(flags & P_COMMA) || *s != ',')
2047 && vim_strchr(s + 1, *s) != NULL)
2048 {
2049 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002050 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002051 }
2052 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002053 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002057 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 arg = save_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002061 /*
2062 * Set the new value.
2063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 *(char_u **)(varp) = newval;
2065
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002066#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002067 if (!starting
2068# ifdef FEAT_CRYPT
2069 && options[opt_idx].indir != PV_KEY
2070# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002071 && origval != NULL && newval != NULL)
2072 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002073 // origval may be freed by
2074 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002075 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002076 // newval (and varp) may become invalid if the
2077 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002078 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002079 if (origval_l != NULL)
2080 saved_origval_l = vim_strsave(origval_l);
2081 if (origval_g != NULL)
2082 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002083 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002084#endif
2085
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002086 {
2087 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002088 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002089
2090 // When an option is set in the sandbox, from a
2091 // modeline or in secure mode, then deal with side
2092 // effects in secure mode. Also when the value was
2093 // set with the P_INSECURE flag and is not
2094 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002095 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002096#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002097 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002098#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002099 || (!value_is_replaced && (*p & P_INSECURE)))
2100 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002101
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002102 // Handle side effects, and set the global value
2103 // for ":set" on local options. Note: when setting
2104 // 'syntax' or 'filetype' autocommands may be
2105 // triggered that can cause havoc.
2106 errmsg = did_set_string_option(
zeertzjqf6782732022-07-27 18:26:03 +01002107 opt_idx, (char_u **)varp, oldval, errbuf,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002108 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002109
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002110 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002113#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002114 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002115 trigger_optionsset_string(
2116 opt_idx, opt_flags, saved_origval,
2117 saved_origval_l, saved_origval_g,
2118 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002119 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002120 vim_free(saved_origval_l);
2121 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002122 vim_free(saved_newval);
2123#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002124 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 if (errmsg != NULL)
2126 goto skip;
2127 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002128 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
2130 char_u *p;
2131
2132 if (nextchar == '&')
2133 {
2134 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002135 errmsg = e_not_found_in_termcap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 }
2137 else
2138 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002139 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002140 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 if (*p == '\\' && p[1] != NUL)
2142 ++p;
2143 nextchar = *p;
2144 *p = NUL;
2145 add_termcode(key_name, arg, FALSE);
2146 *p = nextchar;
2147 }
2148 if (full_screen)
2149 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002150 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 }
2152 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002153
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002155 did_set_option(
2156 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158
2159skip:
2160 /*
2161 * Advance to next argument.
2162 * - skip until a blank found, taking care of backslashes
2163 * - skip blanks
2164 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2165 */
2166 for (i = 0; i < 2 ; ++i)
2167 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002168 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 if (*arg++ == '\\' && *arg != NUL)
2170 ++arg;
2171 arg = skipwhite(arg);
2172 if (*arg != '=')
2173 break;
2174 }
2175 }
2176
2177 if (errmsg != NULL)
2178 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002179 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002180 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (i + (arg - startarg) < IOSIZE)
2182 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002183 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 STRCAT(IObuff, ": ");
2185 mch_memmove(IObuff + i, startarg, (arg - startarg));
2186 IObuff[i + (arg - startarg)] = NUL;
2187 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002188 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 trans_characters(IObuff, IOSIZE);
2190
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002191 ++no_wait_return; // wait_return done later
2192 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 --no_wait_return;
2194
2195 return FAIL;
2196 }
2197
2198 arg = skipwhite(arg);
2199 }
2200
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002201theend:
2202 if (silent_mode && did_show)
2203 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002204 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002205 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002206 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002207 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002208 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002209 out_flush();
2210 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002211 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002212 }
2213
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 return OK;
2215}
2216
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002217/*
2218 * Call this when an option has been given a new value through a user command.
2219 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2220 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002221 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002222did_set_option(
2223 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002224 int opt_flags, // possibly with OPT_MODELINE
2225 int new_value, // value was replaced completely
2226 int value_checked) // value was checked to be safe, no need to set the
2227 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002228{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002229 long_u *p;
2230
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002231 options[opt_idx].flags |= P_WAS_SET;
2232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002233 // When an option is set in the sandbox, from a modeline or in secure mode
2234 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2235 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002236 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002237 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002238#ifdef HAVE_SANDBOX
2239 || sandbox != 0
2240#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002241 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002242 *p = *p | P_INSECURE;
2243 else if (new_value)
2244 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002245}
2246
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247/*
2248 * Convert a key name or string into a key value.
2249 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002250 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002252 int
2253string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254{
2255 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002256 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 if (*arg == '^')
2258 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002259 if (multi_byte)
2260 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 return *arg;
2262}
2263
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264/*
2265 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2266 * maketitle() to create and display it.
2267 * When switching the title or icon off, call mch_restore_title() to get
2268 * the old value back.
2269 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002270 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002271did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
2273 if (starting != NO_SCREEN
2274#ifdef FEAT_GUI
2275 && !gui.starting
2276#endif
2277 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280
2281/*
2282 * set_options_bin - called when 'bin' changes value.
2283 */
2284 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002285set_options_bin(
2286 int oldval,
2287 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002288 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 /*
2291 * The option values that are changed when 'bin' changes are
2292 * copied when 'bin is set and restored when 'bin' is reset.
2293 */
2294 if (newval)
2295 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002296 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 if (!(opt_flags & OPT_GLOBAL))
2299 {
2300 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2301 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2302 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2303 curbuf->b_p_et_nobin = curbuf->b_p_et;
2304 }
2305 if (!(opt_flags & OPT_LOCAL))
2306 {
2307 p_tw_nobin = p_tw;
2308 p_wm_nobin = p_wm;
2309 p_ml_nobin = p_ml;
2310 p_et_nobin = p_et;
2311 }
2312 }
2313
2314 if (!(opt_flags & OPT_GLOBAL))
2315 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002316 curbuf->b_p_tw = 0; // no automatic line wrap
2317 curbuf->b_p_wm = 0; // no automatic line wrap
2318 curbuf->b_p_ml = 0; // no modelines
2319 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321 if (!(opt_flags & OPT_LOCAL))
2322 {
2323 p_tw = 0;
2324 p_wm = 0;
2325 p_ml = FALSE;
2326 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002327 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002330 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
2332 if (!(opt_flags & OPT_GLOBAL))
2333 {
2334 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2335 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2336 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2337 curbuf->b_p_et = curbuf->b_p_et_nobin;
2338 }
2339 if (!(opt_flags & OPT_LOCAL))
2340 {
2341 p_tw = p_tw_nobin;
2342 p_wm = p_wm_nobin;
2343 p_ml = p_ml_nobin;
2344 p_et = p_et_nobin;
2345 }
2346 }
2347}
2348
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349/*
2350 * Expand environment variables for some string options.
2351 * These string options cannot be indirect!
2352 * If "val" is NULL expand the current value of the option.
2353 * Return pointer to NameBuff, or NULL when not expanded.
2354 */
2355 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002356option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002358 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2360 return NULL;
2361
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002362 // If val is longer than MAXPATHL no meaningful expansion can be done,
2363 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (val != NULL && STRLEN(val) > MAXPATHL)
2365 return NULL;
2366
2367 if (val == NULL)
2368 val = *(char_u **)options[opt_idx].var;
2369
2370 /*
2371 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2372 * Escape spaces when expanding 'tags', they are used to separate file
2373 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002374 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 */
2376 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002377 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002378#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002379 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2380#endif
2381 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002382 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 return NULL;
2384
2385 return NameBuff;
2386}
2387
2388/*
2389 * After setting various option values: recompute variables that depend on
2390 * option values.
2391 */
2392 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002393didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002395 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 (void)init_chartab();
2397
Bram Moolenaardac13472019-09-16 21:06:21 +02002398 didset_string_options();
2399
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002400#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002401 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002402 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002403 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002404 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002407 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 (void)check_cedit();
2409#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002410#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002411 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002412 fill_breakat_flags();
2413#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002414 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002415}
2416
2417/*
2418 * More side effects of setting options.
2419 */
2420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002421didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002422{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002423 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002424 (void)highlight_changed();
2425
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002426 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002427 check_opt_wim();
2428
Bram Moolenaareed9d462021-02-15 20:38:25 +01002429 // Parse default for 'listchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002430 (void)set_chars_option(curwin, &curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002431
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002432 // Parse default for 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002433 (void)set_chars_option(curwin, &curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002434
2435#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002436 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002437 (void)check_clipboard_option();
2438#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002439#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002440 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002441 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002442 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002443 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445}
2446
2447/*
2448 * Check for string options that are NULL (normally only termcap options).
2449 */
2450 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002451check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452{
2453 int opt_idx;
2454
2455 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2456 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2457 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2458}
2459
2460/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002461 * Return the option index found by a pointer into term_strings[].
2462 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002464 int
2465get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002467 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
2469 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2470 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002471 return opt_idx;
2472 return -1; // cannot happen: didn't find it!
2473}
2474
2475/*
2476 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2477 * Return the option index or -1 if not found.
2478 */
2479 int
2480set_term_option_alloced(char_u **p)
2481{
2482 int opt_idx = get_term_opt_idx(p);
2483
2484 if (opt_idx >= 0)
2485 options[opt_idx].flags |= P_ALLOCED;
2486 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487}
2488
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002489#if defined(FEAT_EVAL) || defined(PROTO)
2490/*
2491 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2492 * Return FALSE when it wasn't.
2493 * Return -1 for an unknown option.
2494 */
2495 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002496was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002497{
2498 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002499 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002500
2501 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002502 {
2503 flagp = insecure_flag(idx, opt_flags);
2504 return (*flagp & P_INSECURE) != 0;
2505 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002506 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002507 return -1;
2508}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002509
2510/*
2511 * Get a pointer to the flags used for the P_INSECURE flag of option
2512 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002513 * NOTE: Caller must make sure that "curwin" is set to the window from which
2514 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002515 */
2516 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002517insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002518{
2519 if (opt_flags & OPT_LOCAL)
2520 switch ((int)options[opt_idx].indir)
2521 {
2522#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002523 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002524#endif
2525#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002526# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002527 case PV_FDE: return &curwin->w_p_fde_flags;
2528 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002529# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002530# ifdef FEAT_BEVAL
2531 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2532# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002533 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002534 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002535# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002536 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002537# endif
2538#endif
2539 }
2540
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002541 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002542 return &options[opt_idx].flags;
2543}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002544#endif
2545
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002546/*
2547 * Redraw the window title and/or tab page text later.
2548 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002549void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002550{
2551 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002552 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002553}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002554
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002556 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2557 * characters or characters in "allowed".
2558 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002559 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002560valid_name(char_u *val, char *allowed)
2561{
2562 char_u *s;
2563
2564 for (s = val; *s != NUL; ++s)
2565 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2566 return FALSE;
2567 return TRUE;
2568}
2569
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002570#if defined(FEAT_EVAL) || defined(PROTO)
2571/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002572 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002573 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002574 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002575 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002576set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002577{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002578 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2579 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002580 sctx_T new_script_ctx = script_ctx;
2581
Bram Moolenaar51258742020-05-03 17:19:33 +02002582 // Modeline already has the line number set.
2583 if (!(opt_flags & OPT_MODELINE))
2584 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002585
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002586 // Remember where the option was set. For local options need to do that
2587 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002588 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002589 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002590 if (both || (opt_flags & OPT_LOCAL))
2591 {
2592 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002593 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002594 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002595 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002596 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002597 if (both)
2598 // also setting the "all buffers" value
2599 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2600 new_script_ctx;
2601 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002602 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002603}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002604
2605/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002606 * Get the script context of global option "name".
2607 *
2608 */
2609 sctx_T *
2610get_option_sctx(char *name)
2611{
2612 int idx = findoption((char_u *)name);
2613
2614 if (idx >= 0)
2615 return &options[idx].script_ctx;
2616 siemsg("no such option: %s", name);
2617 return NULL;
2618}
2619
2620/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002621 * Set the script_ctx for a termcap option.
2622 * "name" must be the two character code, e.g. "RV".
2623 * When "name" is NULL use "opt_idx".
2624 */
2625 void
2626set_term_option_sctx_idx(char *name, int opt_idx)
2627{
2628 char_u buf[5];
2629 int idx;
2630
2631 if (name == NULL)
2632 idx = opt_idx;
2633 else
2634 {
2635 buf[0] = 't';
2636 buf[1] = '_';
2637 buf[2] = name[0];
2638 buf[3] = name[1];
2639 buf[4] = 0;
2640 idx = findoption(buf);
2641 }
2642 if (idx >= 0)
2643 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2644}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002645#endif
2646
Bram Moolenaarf4140482020-02-15 23:06:45 +01002647#if defined(FEAT_EVAL)
2648/*
2649 * Apply the OptionSet autocommand.
2650 */
2651 static void
2652apply_optionset_autocmd(
2653 int opt_idx,
2654 long opt_flags,
2655 long oldval,
2656 long oldval_g,
2657 long newval,
2658 char *errmsg)
2659{
2660 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2661
2662 // Don't do this while starting up, failure or recursively.
2663 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2664 return;
2665
2666 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2667 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2668 oldval_g);
2669 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2670 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2671 (opt_flags & OPT_LOCAL) ? "local" : "global");
2672 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2673 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2674 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2675 if (opt_flags & OPT_LOCAL)
2676 {
2677 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2678 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2679 }
2680 if (opt_flags & OPT_GLOBAL)
2681 {
2682 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2683 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2684 }
2685 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2686 {
2687 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2688 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2689 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2690 }
2691 if (opt_flags & OPT_MODELINE)
2692 {
2693 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2694 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2695 }
2696 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2697 NULL, FALSE, NULL);
2698 reset_v_option_vars();
2699}
2700#endif
2701
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702/*
2703 * Set the value of a boolean option, and take care of side effects.
2704 * Returns NULL for success, or an error message for an error.
2705 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002706 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002707set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002708 int opt_idx, // index in options[] table
2709 char_u *varp, // pointer to the option variable
2710 int value, // new value
2711 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712{
2713 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002714#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002715 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002716#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002717 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002719 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 if ((secure
2721#ifdef HAVE_SANDBOX
2722 || sandbox != 0
2723#endif
2724 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002725 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002727#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002728 // Save the global value before changing anything. This is needed as for
2729 // a global-only option setting the "local value" in fact sets the global
2730 // value (since there is only one value).
2731 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2732 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2733 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002734#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002735
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002738 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002739 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740#endif
2741
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002742#ifdef FEAT_GUI
2743 need_mouse_correct = TRUE;
2744#endif
2745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2748 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2749
2750 /*
2751 * Handle side effects of changing a bool option.
2752 */
2753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
Bram Moolenaar920694c2016-08-21 17:45:02 +02002758#ifdef FEAT_LANGMAP
2759 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002760 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002761 p_lnr = !p_lrm;
2762 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002763 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002764 p_lrm = !p_lnr;
2765#endif
2766
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002767#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002768 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002769 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2770 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 // Only take action when the option was set. When reset we do not
2772 // delete the undo file, the option may be set again without making
2773 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002774 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002775 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002776 char_u hash[UNDO_HASH_SIZE];
2777 buf_T *save_curbuf = curbuf;
2778
Bram Moolenaar29323592016-07-24 22:04:11 +02002779 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002780 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002781 // When 'undofile' is set globally: for every buffer, otherwise
2782 // only for the current buffer: Try to read in the undofile,
2783 // if one exists, the buffer wasn't changed and the buffer was
2784 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002785 if ((curbuf == save_curbuf
2786 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2787 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2788 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002789#ifdef FEAT_CRYPT
2790 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2791 continue;
2792#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002793 u_compute_hash(hash);
2794 u_read_undo(NULL, hash, curbuf->b_fname);
2795 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002796 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002797 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002798 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002799 }
2800#endif
2801
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 else if ((int *)varp == &curbuf->b_p_ro)
2803 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002804 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2806 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002807
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002808 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002809 if (curbuf->b_p_ro)
2810 curbuf->b_did_warn = FALSE;
2811
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002812 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
2814
Bram Moolenaar9c449722010-07-20 18:44:27 +02002815#ifdef FEAT_GUI
2816 else if ((int *)varp == &p_mh)
2817 {
2818 if (!p_mh)
2819 gui_mch_mousehide(FALSE);
2820 }
2821#endif
2822
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002823 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002825 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002826# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002827 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002828 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2829 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002830 {
2831 curbuf->b_p_ma = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002832 return e_cannot_make_terminal_with_running_job_modifiable;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002833 }
2834# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002835 redraw_titles();
2836 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002837 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002839 {
2840 redraw_titles();
2841 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002842 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002843 else if ((int *)varp == &curbuf->b_p_fixeol)
2844 {
2845 redraw_titles();
2846 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002847 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002848 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002849 {
2850 redraw_titles();
2851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 else if ((int *)varp == &curbuf->b_p_bin)
2855 {
2856 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002857 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
2859
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002860 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2862 {
2863 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2864 NULL, NULL, TRUE, curbuf);
2865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002867 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 else if ((int *)varp == &curbuf->b_p_swf)
2869 {
2870 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002871 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002873 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2874 // buf->b_p_swf
2875 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
2877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 else if ((int *)varp == &p_terse)
2880 {
2881 char_u *p;
2882
2883 p = vim_strchr(p_shm, SHM_SEARCH);
2884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002885 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 if (p_terse && p == NULL)
2887 {
2888 STRCPY(IObuff, p_shm);
2889 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002890 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002892 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002894 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
2896
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002897 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 else if ((int *)varp == &p_paste)
2899 {
2900 paste_option_changed();
2901 }
2902
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002903 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 else if ((int *)varp == &p_im)
2905 {
2906 if (p_im)
2907 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002908 if ((State & MODE_INSERT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 need_start_insertmode = TRUE;
2910 stop_insert_mode = FALSE;
2911 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002913 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 {
2915 need_start_insertmode = FALSE;
2916 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002917 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002918 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 restart_edit = 0;
2920 }
2921 }
2922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 else if ((int *)varp == &p_ic && p_hls)
2925 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002926 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 }
2928
2929#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002930 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 else if ((int *)varp == &p_hls)
2932 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002933 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 }
2935#endif
2936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2938 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 else if ((int *)varp == &curwin->w_p_scb)
2940 {
2941 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002944 curwin->w_scbind_pos = curwin->w_topline;
2945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947
Bram Moolenaar4033c552017-09-16 20:54:51 +02002948#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002949 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 else if ((int *)varp == &curwin->w_p_pvw)
2951 {
2952 if (curwin->w_p_pvw)
2953 {
2954 win_T *win;
2955
Bram Moolenaar29323592016-07-24 22:04:11 +02002956 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 if (win->w_p_pvw && win != curwin)
2958 {
2959 curwin->w_p_pvw = FALSE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002960 return e_preview_window_already_exists;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 }
2962 }
2963 }
2964#endif
2965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002966 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 else if ((int *)varp == &curbuf->b_p_tx)
2968 {
2969 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2970 }
2971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002974 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 set_string_option_direct((char_u *)"ffs", -1,
2976 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002977 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
2980 /*
2981 * When 'lisp' option changes include/exclude '-' in
2982 * keyword characters.
2983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2985 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002989 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002990 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002992 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
2995 else if ((int *)varp == &curbuf->b_changed)
2996 {
2997 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002998 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002999 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
3002
3003#ifdef BACKSLASH_IN_FILENAME
3004 else if ((int *)varp == &p_ssl)
3005 {
3006 if (p_ssl)
3007 {
3008 psepc = '/';
3009 psepcN = '\\';
3010 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 }
3012 else
3013 {
3014 psepc = '\\';
3015 psepcN = '/';
3016 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 }
3018
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003019 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 buflist_slash_adjust();
3021 alist_slash_adjust();
3022# ifdef FEAT_EVAL
3023 scriptnames_slash_adjust();
3024# endif
3025 }
3026#endif
3027
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003028 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 else if ((int *)varp == &curwin->w_p_wrap)
3030 {
3031 if (curwin->w_p_wrap)
3032 curwin->w_leftcol = 0;
3033 }
3034
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 else if ((int *)varp == &p_ea)
3036 {
3037 if (p_ea && !old_value)
3038 win_equal(curwin, FALSE, 0);
3039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
3041 else if ((int *)varp == &p_wiv)
3042 {
3043 /*
3044 * When 'weirdinvert' changed, set/reset 't_xs'.
3045 * Then set 'weirdinvert' according to value of 't_xs'.
3046 */
3047 if (p_wiv && !old_value)
3048 T_XS = (char_u *)"y";
3049 else if (!p_wiv && old_value)
3050 T_XS = empty_option;
3051 p_wiv = (*T_XS != NUL);
3052 }
3053
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003054#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 else if ((int *)varp == &p_beval)
3056 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003057 if (!balloonEvalForTerm)
3058 {
3059 if (p_beval && !old_value)
3060 gui_mch_enable_beval_area(balloonEval);
3061 else if (!p_beval && old_value)
3062 gui_mch_disable_beval_area(balloonEval);
3063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003065#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003066#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003067 else if ((int *)varp == &p_bevalterm)
3068 {
3069 mch_bevalterm_changed();
3070 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003073#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 else if ((int *)varp == &p_acd)
3075 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003076 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003077 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 }
3079#endif
3080
3081#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003082 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 else if ((int *)varp == &curwin->w_p_diff)
3084 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003085 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003086 diff_buf_adjust(curwin);
3087# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 if (foldmethodIsDiff(curwin))
3089 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
3092#endif
3093
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003094#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003095 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 else if ((int *)varp == &p_imdisable)
3097 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003098 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 if (p_imdisable)
3100 im_set_active(FALSE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003101 else if (State & MODE_INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003102 // When the option is set from an autocommand, it may need to take
3103 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003104 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
3106#endif
3107
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003108#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003109 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003110 else if ((int *)varp == &curwin->w_p_spell)
3111 {
3112 if (curwin->w_p_spell)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003113 errmsg = did_set_spelllang(curwin);
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003114 }
3115#endif
3116
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117#ifdef FEAT_ARABIC
3118 if ((int *)varp == &curwin->w_p_arab)
3119 {
3120 if (curwin->w_p_arab)
3121 {
3122 /*
3123 * 'arabic' is set, handle various sub-settings.
3124 */
3125 if (!p_tbidi)
3126 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003127 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 if (!curwin->w_p_rl)
3129 {
3130 curwin->w_p_rl = TRUE;
3131 changed_window_setting();
3132 }
3133
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003134 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 if (!p_arshape)
3136 {
3137 p_arshape = TRUE;
3138 redraw_later_clear();
3139 }
3140 }
3141
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003142 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003143 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003145 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003146 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3147
Bram Moolenaar8820b482017-03-16 17:23:31 +01003148 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003149 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003150#ifdef FEAT_EVAL
3151 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3152#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003155 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003159 // Force-set the necessary keymap for arabic
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003160 errmsg = set_option_value((char_u *)"keymap",
3161 0L, (char_u *)"arabic", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
3164 else
3165 {
3166 /*
3167 * 'arabic' is reset, handle various sub-settings.
3168 */
3169 if (!p_tbidi)
3170 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003171 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (curwin->w_p_rl)
3173 {
3174 curwin->w_p_rl = FALSE;
3175 changed_window_setting();
3176 }
3177
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003178 // 'arabicshape' isn't reset, it is a global option and
3179 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 }
3181
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003182 // 'delcombine' isn't reset, it is a global option and another
3183 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
3185# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003186 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 curbuf->b_p_iminsert = B_IMODE_NONE;
3188 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3189# endif
3190 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003191 }
3192
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003193#endif
3194
Bram Moolenaar44826212019-08-22 21:23:20 +02003195#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3196 else if (((int *)varp == &curwin->w_p_nu
3197 || (int *)varp == &curwin->w_p_rnu)
3198 && gui.in_use
3199 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3200 && curbuf->b_signlist != NULL)
3201 {
3202 // If the 'number' or 'relativenumber' options are modified and
3203 // 'signcolumn' is set to 'number', then clear the screen for a full
3204 // refresh. Otherwise the sign icons are not displayed properly in the
3205 // number column. If the 'number' option is set and only the
3206 // 'relativenumber' option is toggled, then don't refresh the screen
3207 // (optimization).
3208 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003209 redraw_all_later(UPD_CLEAR);
Bram Moolenaar44826212019-08-22 21:23:20 +02003210 }
3211#endif
3212
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003213#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003214 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003215 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003216 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003217# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003218 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003219 if (
3220# ifdef VIMDLL
3221 !gui.in_use && !gui.starting &&
3222# endif
3223 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003224 {
3225 p_tgc = 0;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003226 return e_24_bit_colors_are_not_supported_on_this_environment;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003227 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003228 if (is_term_win32())
3229 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003230# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003231# ifdef FEAT_GUI
3232 if (!gui.in_use && !gui.starting)
3233# endif
3234 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003235# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003236 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003237 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003238 {
3239 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003240 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003241 init_highlight(TRUE, FALSE);
3242 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003243# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003244# ifdef FEAT_TERMINAL
3245 term_update_colors_all();
LemonBoyb2b3acb2022-05-20 10:10:34 +01003246 term_update_palette_all();
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003247 term_update_wincolor_all();
3248# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003249 }
3250#endif
3251
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 /*
3253 * End of handling side effects for bool options.
3254 */
3255
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003256 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003257
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 options[opt_idx].flags |= P_WAS_SET;
3259
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003260#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003261 apply_optionset_autocmd(opt_idx, opt_flags,
3262 (long)(old_value ? TRUE : FALSE),
3263 (long)(old_global_value ? TRUE : FALSE),
3264 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003265#endif
3266
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003267 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003268 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003269 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003270 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003271
3272 if ((opt_flags & OPT_NO_REDRAW) == 0)
3273 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003275 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276}
3277
3278/*
3279 * Set the value of a number option, and take care of side effects.
3280 * Returns NULL for success, or an error message for an error.
3281 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003282 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003283set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003284 int opt_idx, // index in options[] table
3285 char_u *varp, // pointer to the option variable
3286 long value, // new value
3287 char *errbuf, // buffer for error messages
3288 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003289 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3290 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003292 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003294#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003295 long old_global_value = 0; // only used when setting a local and
3296 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003297#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003298 long old_Rows = Rows; // remember old Rows
3299 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 long *pp = (long *)varp;
3301
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003302 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003303 if ((secure
3304#ifdef HAVE_SANDBOX
3305 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003307 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003308 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003310#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003311 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003312 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003313 // value (since there is only one value).
3314 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003315 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3316 OPT_GLOBAL);
3317#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003318
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 *pp = value;
3320#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003321 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003322 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003324#ifdef FEAT_GUI
3325 need_mouse_correct = TRUE;
3326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
Bram Moolenaar14f24742012-08-08 18:01:05 +02003328 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003330 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003331#ifdef FEAT_VARTABS
3332 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3333 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003334 ? tabstop_first(curbuf->b_p_vts_array)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003335 : curbuf->b_p_ts;
3336#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 }
3340
3341 /*
3342 * Number options that need some action when changed
3343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 if (pp == &p_wh || pp == &p_hh)
3345 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003346 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (p_wh < 1)
3348 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003349 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 p_wh = 1;
3351 }
3352 if (p_wmh > p_wh)
3353 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003354 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 p_wh = p_wmh;
3356 }
3357 if (p_hh < 0)
3358 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003359 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 p_hh = 0;
3361 }
3362
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003363 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003364 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 {
3366 if (pp == &p_wh && curwin->w_height < p_wh)
3367 win_setheight((int)p_wh);
3368 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3369 win_setheight((int)p_hh);
3370 }
3371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 else if (pp == &p_wmh)
3373 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003374 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (p_wmh < 0)
3376 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003377 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 p_wmh = 0;
3379 }
3380 if (p_wmh > p_wh)
3381 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003382 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 p_wmh = p_wh;
3384 }
3385 win_setminheight();
3386 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003387 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003389 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 if (p_wiw < 1)
3391 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003392 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 p_wiw = 1;
3394 }
3395 if (p_wmw > p_wiw)
3396 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003397 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 p_wiw = p_wmw;
3399 }
3400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003401 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003402 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 win_setwidth((int)p_wiw);
3404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 else if (pp == &p_wmw)
3406 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003407 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (p_wmw < 0)
3409 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003410 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 p_wmw = 0;
3412 }
3413 if (p_wmw > p_wiw)
3414 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003415 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 p_wmw = p_wiw;
3417 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003418 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003421 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 else if (pp == &p_ls)
3423 {
3424 last_status(FALSE);
3425 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003426
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003427 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003428 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003429 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003430 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
3433#ifdef FEAT_GUI
3434 else if (pp == &p_linespace)
3435 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003436 // Recompute gui.char_height and resize the Vim window to keep the
3437 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003438 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003439 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
3441#endif
3442
3443#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003444 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 else if (pp == &curwin->w_p_fdl)
3446 {
3447 if (curwin->w_p_fdl < 0)
3448 curwin->w_p_fdl = 0;
3449 newFoldLevel();
3450 }
3451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003452 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 else if (pp == &curwin->w_p_fml)
3454 {
3455 foldUpdateAll(curwin);
3456 }
3457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003458 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 else if (pp == &curwin->w_p_fdn)
3460 {
3461 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3462 foldUpdateAll(curwin);
3463 }
3464
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003465 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 else if (pp == &curwin->w_p_fdc)
3467 {
3468 if (curwin->w_p_fdc < 0)
3469 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003470 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 curwin->w_p_fdc = 0;
3472 }
3473 else if (curwin->w_p_fdc > 12)
3474 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003475 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 curwin->w_p_fdc = 12;
3477 }
3478 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003479#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003481 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3483 {
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003484#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (foldmethodIsIndent(curwin))
3486 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01003487#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003488 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3489 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003490 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3491 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003494 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003495 else if (pp == &p_mco)
3496 {
3497 if (p_mco > MAX_MCO)
3498 p_mco = MAX_MCO;
3499 else if (p_mco < 0)
3500 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003501 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003502 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003503
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 else if (pp == &curbuf->b_p_iminsert)
3505 {
3506 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3507 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003508 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 curbuf->b_p_iminsert = B_IMODE_NONE;
3510 }
3511 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003512 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003514#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003515 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 status_redraw_curbuf();
3517#endif
3518 }
3519
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003520#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003521 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003522 else if (pp == &p_imst)
3523 {
3524 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003525 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003526 }
3527#endif
3528
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003529 else if (pp == &p_window)
3530 {
3531 if (p_window < 1)
3532 p_window = 1;
3533 else if (p_window >= Rows)
3534 p_window = Rows - 1;
3535 }
3536
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 else if (pp == &curbuf->b_p_imsearch)
3538 {
3539 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3540 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003541 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 curbuf->b_p_imsearch = B_IMODE_NONE;
3543 }
3544 p_imsearch = curbuf->b_p_imsearch;
3545 }
3546
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003547 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 else if (pp == &p_titlelen)
3549 {
3550 if (p_titlelen < 0)
3551 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003552 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 p_titlelen = 85;
3554 }
3555 if (starting != NO_SCREEN && old_value != p_titlelen)
3556 need_maketitle = TRUE;
3557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003559 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 else if (pp == &p_ch)
3561 {
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01003562 if (p_ch < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003564 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 p_ch = 1;
3566 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003567 if (p_ch > Rows - min_rows() + 1)
3568 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003570 // Only compute the new window layout when startup has been
3571 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 if (p_ch != old_value && full_screen
3573#ifdef FEAT_GUI
3574 && !gui.starting
3575#endif
3576 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003577 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003580 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 else if (pp == &p_uc)
3582 {
3583 if (p_uc < 0)
3584 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003585 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 p_uc = 100;
3587 }
3588 if (p_uc && !old_value)
3589 ml_open_files();
3590 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003591#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003592 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003593 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003594 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003595 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003596 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003597 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003598 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003599 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003600 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003601 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003602 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003603 }
3604 }
3605#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003606#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003607 else if (pp == &p_mzq)
3608 mzvim_reset_timer();
3609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003611#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003612 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003613 else if (pp == &p_pyx)
3614 {
3615 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003616 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003617 }
3618#endif
3619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003620 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 else if (pp == &p_ul)
3622 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003623 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003625 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 p_ul = value;
3627 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003628 else if (pp == &curbuf->b_p_ul)
3629 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003630 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003631 curbuf->b_p_ul = old_value;
3632 u_sync(TRUE);
3633 curbuf->b_p_ul = value;
3634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003636#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003637 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003638 else if (pp == &curwin->w_p_nuw)
3639 {
3640 if (curwin->w_p_nuw < 1)
3641 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003642 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003643 curwin->w_p_nuw = 1;
3644 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003645 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003646 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003647 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003648 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003649 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003650 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003651 }
3652#endif
3653
Bram Moolenaar1a384422010-07-14 19:53:30 +02003654 else if (pp == &curbuf->b_p_tw)
3655 {
3656 if (curbuf->b_p_tw < 0)
3657 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003658 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003659 curbuf->b_p_tw = 0;
3660 }
3661#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003662 {
3663 win_T *wp;
3664 tabpage_T *tp;
3665
3666 FOR_ALL_TAB_WINDOWS(tp, wp)
3667 check_colorcolumn(wp);
3668 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003669#endif
3670 }
3671
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 /*
3673 * Check the bounds for numeric options here
3674 */
3675 if (Rows < min_rows() && full_screen)
3676 {
3677 if (errbuf != NULL)
3678 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003679 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003680 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 errmsg = errbuf;
3682 }
3683 Rows = min_rows();
3684 }
3685 if (Columns < MIN_COLUMNS && full_screen)
3686 {
3687 if (errbuf != NULL)
3688 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003689 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003690 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 errmsg = errbuf;
3692 }
3693 Columns = MIN_COLUMNS;
3694 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003695 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 /*
3698 * If the screen (shell) height has been changed, assume it is the
3699 * physical screenheight.
3700 */
3701 if (old_Rows != Rows || old_Columns != Columns)
3702 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003703 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 if (updating_screen)
3705 *pp = old_value;
3706 else if (full_screen
3707#ifdef FEAT_GUI
3708 && !gui.starting
3709#endif
3710 )
3711 set_shellsize((int)Columns, (int)Rows, TRUE);
3712 else
3713 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003714 // Postpone the resizing; check the size and cmdline position for
3715 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 check_shellsize();
3717 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3718 cmdline_row = Rows - p_ch;
3719 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003720 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003721 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 }
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 if (curbuf->b_p_ts <= 0)
3725 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003726 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 curbuf->b_p_ts = 8;
3728 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003729 else if (curbuf->b_p_ts > TABSTOP_MAX)
3730 {
3731 errmsg = e_invalid_argument;
3732 curbuf->b_p_ts = 8;
3733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (p_tm < 0)
3735 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003736 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 p_tm = 0;
3738 }
3739 if ((curwin->w_p_scr <= 0
3740 || (curwin->w_p_scr > curwin->w_height
3741 && curwin->w_height > 0))
3742 && full_screen)
3743 {
3744 if (pp == &(curwin->w_p_scr))
3745 {
3746 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003747 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 win_comp_scroll(curwin);
3749 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003750 // If 'scroll' became invalid because of a side effect silently adjust
3751 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 else if (curwin->w_p_scr <= 0)
3753 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003754 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 curwin->w_p_scr = curwin->w_height;
3756 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003757 if (p_hi < 0)
3758 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003759 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003760 p_hi = 0;
3761 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003762 else if (p_hi > 10000)
3763 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003764 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003765 p_hi = 10000;
3766 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003767 if (p_re < 0 || p_re > 2)
3768 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003769 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003770 p_re = 0;
3771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 if (p_report < 0)
3773 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003774 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 p_report = 1;
3776 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003777 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003779 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 p_sj = Rows / 2;
3781 else
3782 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003783 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 p_sj = 1;
3785 }
3786 }
3787 if (p_so < 0 && full_screen)
3788 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003789 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 p_so = 0;
3791 }
3792 if (p_siso < 0 && full_screen)
3793 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003794 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 p_siso = 0;
3796 }
3797#ifdef FEAT_CMDWIN
3798 if (p_cwh < 1)
3799 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003800 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 p_cwh = 1;
3802 }
3803#endif
3804 if (p_ut < 0)
3805 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003806 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 p_ut = 2000;
3808 }
3809 if (p_ss < 0)
3810 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003811 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 p_ss = 0;
3813 }
3814
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003815 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3817 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3818
3819 options[opt_idx].flags |= P_WAS_SET;
3820
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003821#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003822 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3823 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003824#endif
3825
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003826 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003827 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003828 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003829 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003830 if ((opt_flags & OPT_NO_REDRAW) == 0)
3831 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
3833 return errmsg;
3834}
3835
3836/*
3837 * Called after an option changed: check if something needs to be redrawn.
3838 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003839 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003840check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003842 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003843 int doclear = (flags & P_RCLR) == P_RCLR;
3844 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003846 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848
3849 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3850 changed_window_setting();
3851 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003852 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003853 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003854 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003855 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003856 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003858 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859}
3860
3861/*
3862 * Find index for option 'arg'.
3863 * Return -1 if not found.
3864 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003865 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003866findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867{
3868 int opt_idx;
3869 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003870 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 int is_term_opt;
3872
3873 /*
3874 * For first call: Initialize the quick-access table.
3875 * It contains the index for the first option that starts with a certain
3876 * letter. There are 26 letters, plus the first "t_" option.
3877 */
3878 if (quick_tab[1] == 0)
3879 {
3880 p = options[0].fullname;
3881 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3882 {
3883 if (s[0] != p[0])
3884 {
3885 if (s[0] == 't' && s[1] == '_')
3886 quick_tab[26] = opt_idx;
3887 else
3888 quick_tab[CharOrdLow(s[0])] = opt_idx;
3889 }
3890 p = s;
3891 }
3892 }
3893
3894 /*
3895 * Check for name starting with an illegal character.
3896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 return -1;
3899
3900 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3901 if (is_term_opt)
3902 opt_idx = quick_tab[26];
3903 else
3904 opt_idx = quick_tab[CharOrdLow(arg[0])];
3905 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3906 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003907 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 break;
3909 }
3910 if (s == NULL && !is_term_opt)
3911 {
3912 opt_idx = quick_tab[CharOrdLow(arg[0])];
3913 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3914 {
3915 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003916 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 break;
3918 s = NULL;
3919 }
3920 }
3921 if (s == NULL)
3922 opt_idx = -1;
3923 return opt_idx;
3924}
3925
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003926#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927/*
3928 * Get the value for an option.
3929 *
3930 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003931 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003932 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003933 * String option: gov_string, *stringval gets allocated string.
3934 * Hidden Number option: gov_hidden_number.
3935 * Hidden Toggle option: gov_hidden_bool.
3936 * Hidden String option: gov_hidden_string.
3937 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003938 *
3939 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003941 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003942get_option_value(
3943 char_u *name,
3944 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003945 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003946 int *flagsp,
3947 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
3949 int opt_idx;
3950 char_u *varp;
3951
3952 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003953 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003954 {
3955 int key;
3956
3957 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003958 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003959 {
3960 char_u key_name[2];
3961 char_u *p;
3962
Bram Moolenaar10c75c42021-12-28 20:53:30 +00003963 if (flagsp != NULL)
3964 *flagsp = 0; // terminal option has no flags
3965
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003966 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003967 if (key < 0)
3968 {
3969 key_name[0] = KEY2TERMCAP0(key);
3970 key_name[1] = KEY2TERMCAP1(key);
3971 }
3972 else
3973 {
3974 key_name[0] = KS_KEY;
3975 key_name[1] = (key & 0xff);
3976 }
3977 p = find_termcode(key_name);
3978 if (p != NULL)
3979 {
3980 if (stringval != NULL)
3981 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003982 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003983 }
3984 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003985 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003988 varp = get_varp_scope(&(options[opt_idx]), scope);
3989
3990 if (flagsp != NULL)
3991 // Return the P_xxxx option flags.
3992 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994 if (options[opt_idx].flags & P_STRING)
3995 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003996 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003997 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (stringval != NULL)
3999 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004000 if ((char_u **)varp == &p_pt) // 'pastetoggle'
4001 *stringval = str2special_save(*(char_u **)(varp), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004003 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004004 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004005 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004008 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 *stringval = vim_strsave(*(char_u **)(varp));
4010 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004011 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004014 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004015 return (options[opt_idx].flags & P_NUM)
4016 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 if (options[opt_idx].flags & P_NUM)
4018 *numval = *(long *)varp;
4019 else
4020 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004021 // Special case: 'modified' is b_changed, but we also want to consider
4022 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 if ((int *)varp == &curbuf->b_changed)
4024 *numval = curbufIsChanged();
4025 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004026 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004028 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029}
4030#endif
4031
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004032#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004033/*
4034 * Returns the option attributes and its value. Unlike the above function it
4035 * will return either global value or local value of the option depending on
4036 * what was requested, but it will never return global value if it was
4037 * requested to return local one and vice versa. Neither it will return
4038 * buffer-local value if it was requested to return window-local one.
4039 *
4040 * Pretends that option is absent if it is not present in the requested scope
4041 * (i.e. has no global, window-local or buffer-local value depending on
4042 * opt_type). Uses
4043 *
4044 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004045 * 0 hidden or unknown option, also option that does not have requested
4046 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004047 * see SOPT_* in vim.h for other flags
4048 *
4049 * Possible opt_type values: see SREQ_* in vim.h
4050 */
4051 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004052get_option_value_strict(
4053 char_u *name,
4054 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004055 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004056 int opt_type,
4057 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004058{
4059 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004060 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004061 struct vimoption *p;
4062 int r = 0;
4063
4064 opt_idx = findoption(name);
4065 if (opt_idx < 0)
4066 return 0;
4067
4068 p = &(options[opt_idx]);
4069
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004070 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004071 if (p->var == NULL)
4072 return 0;
4073
4074 if (p->flags & P_BOOL)
4075 r |= SOPT_BOOL;
4076 else if (p->flags & P_NUM)
4077 r |= SOPT_NUM;
4078 else if (p->flags & P_STRING)
4079 r |= SOPT_STRING;
4080
4081 if (p->indir == PV_NONE)
4082 {
4083 if (opt_type == SREQ_GLOBAL)
4084 r |= SOPT_GLOBAL;
4085 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004086 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004087 }
4088 else
4089 {
4090 if (p->indir & PV_BOTH)
4091 r |= SOPT_GLOBAL;
4092 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004093 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004094
4095 if (p->indir & PV_WIN)
4096 {
4097 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004098 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004099 else
4100 r |= SOPT_WIN;
4101 }
4102 else if (p->indir & PV_BUF)
4103 {
4104 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004105 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004106 else
4107 r |= SOPT_BUF;
4108 }
4109 }
4110
4111 if (stringval == NULL)
4112 return r;
4113
4114 if (opt_type == SREQ_GLOBAL)
4115 varp = p->var;
4116 else
4117 {
4118 if (opt_type == SREQ_BUF)
4119 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004120 // Special case: 'modified' is b_changed, but we also want to
4121 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004122 if (p->indir == PV_MOD)
4123 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004124 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004125 varp = NULL;
4126 }
4127#ifdef FEAT_CRYPT
4128 else if (p->indir == PV_KEY)
4129 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004130 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004131 *stringval = NULL;
4132 varp = NULL;
4133 }
4134#endif
4135 else
4136 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004137 buf_T *save_curbuf = curbuf;
4138
4139 // only getting a pointer, no need to use aucmd_prepbuf()
4140 curbuf = (buf_T *)from;
4141 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004142 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004143 curbuf = save_curbuf;
4144 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004145 }
4146 }
4147 else if (opt_type == SREQ_WIN)
4148 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004149 win_T *save_curwin = curwin;
4150
4151 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004152 curbuf = curwin->w_buffer;
4153 varp = get_varp(p);
4154 curwin = save_curwin;
4155 curbuf = curwin->w_buffer;
4156 }
4157 if (varp == p->var)
4158 return (r | SOPT_UNSET);
4159 }
4160
4161 if (varp != NULL)
4162 {
4163 if (p->flags & P_STRING)
4164 *stringval = vim_strsave(*(char_u **)(varp));
4165 else if (p->flags & P_NUM)
4166 *numval = *(long *) varp;
4167 else
4168 *numval = *(int *)varp;
4169 }
4170
4171 return r;
4172}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004173
4174/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004175 * Iterate over options. First argument is a pointer to a pointer to a
4176 * structure inside options[] array, second is option type like in the above
4177 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004178 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004179 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004180 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004181 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004182 * first argument to NULL.
4183 *
4184 * Returns full option name for current option on each call.
4185 */
4186 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004187option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004188{
4189 struct vimoption *ret = NULL;
4190 do
4191 {
4192 if (*option == NULL)
4193 *option = (void *) options;
4194 else if (((struct vimoption *) (*option))->fullname == NULL)
4195 {
4196 *option = NULL;
4197 return NULL;
4198 }
4199 else
4200 *option = (void *) (((struct vimoption *) (*option)) + 1);
4201
4202 ret = ((struct vimoption *) (*option));
4203
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004204 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004205 if (ret->var == NULL)
4206 {
4207 ret = NULL;
4208 continue;
4209 }
4210
4211 switch (opt_type)
4212 {
4213 case SREQ_GLOBAL:
4214 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4215 ret = NULL;
4216 break;
4217 case SREQ_BUF:
4218 if (!(ret->indir & PV_BUF))
4219 ret = NULL;
4220 break;
4221 case SREQ_WIN:
4222 if (!(ret->indir & PV_WIN))
4223 ret = NULL;
4224 break;
4225 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004226 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004227 return NULL;
4228 }
4229 }
4230 while (ret == NULL);
4231
4232 return (char_u *)ret->fullname;
4233}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004234#endif
4235
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004237 * Return the flags for the option at 'opt_idx'.
4238 */
4239 long_u
4240get_option_flags(int opt_idx)
4241{
4242 return options[opt_idx].flags;
4243}
4244
4245/*
4246 * Set a flag for the option at 'opt_idx'.
4247 */
4248 void
4249set_option_flag(int opt_idx, long_u flag)
4250{
4251 options[opt_idx].flags |= flag;
4252}
4253
4254/*
4255 * Clear a flag for the option at 'opt_idx'.
4256 */
4257 void
4258clear_option_flag(int opt_idx, long_u flag)
4259{
4260 options[opt_idx].flags &= ~flag;
4261}
4262
4263/*
4264 * Returns TRUE if the option at 'opt_idx' is a global option
4265 */
4266 int
4267is_global_option(int opt_idx)
4268{
4269 return options[opt_idx].indir == PV_NONE;
4270}
4271
4272/*
4273 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4274 * local value.
4275 */
4276 int
4277is_global_local_option(int opt_idx)
4278{
4279 return options[opt_idx].indir & PV_BOTH;
4280}
4281
4282/*
4283 * Returns TRUE if the option at 'opt_idx' is a window-local option
4284 */
4285 int
4286is_window_local_option(int opt_idx)
4287{
4288 return options[opt_idx].var == VAR_WIN;
4289}
4290
4291/*
4292 * Returns TRUE if the option at 'opt_idx' is a hidden option
4293 */
4294 int
4295is_hidden_option(int opt_idx)
4296{
4297 return options[opt_idx].var == NULL;
4298}
4299
4300#if defined(FEAT_CRYPT) || defined(PROTO)
4301/*
4302 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4303 */
4304 int
4305is_crypt_key_option(int opt_idx)
4306{
4307 return options[opt_idx].indir == PV_KEY;
4308}
4309#endif
4310
4311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 * Set the value of option "name".
4313 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004314 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004315 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004317 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004318set_option_value(
4319 char_u *name,
4320 long number,
4321 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004322 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323{
4324 int opt_idx;
4325 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004326 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
4328 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004329 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004330 {
4331 int key;
4332
4333 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004334 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004335 {
4336 char_u key_name[2];
4337
4338 if (key < 0)
4339 {
4340 key_name[0] = KEY2TERMCAP0(key);
4341 key_name[1] = KEY2TERMCAP1(key);
4342 }
4343 else
4344 {
4345 key_name[0] = KS_KEY;
4346 key_name[1] = (key & 0xff);
4347 }
4348 add_termcode(key_name, string, FALSE);
4349 if (full_screen)
4350 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004351 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01004352 return NULL;
4353 }
4354
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004355 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 else
4358 {
4359 flags = options[opt_idx].flags;
4360#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004361 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004363 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004364 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004365 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004368 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004369 return set_string_option(opt_idx, string, opt_flags);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004370
4371 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
4372 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004374 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004376 int idx;
4377
4378 // Either we are given a string or we are setting option
4379 // to zero.
4380 for (idx = 0; string[idx] == '0'; ++idx)
4381 ;
4382 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004383 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004384 // There's another character after zeros or the string
4385 // is empty. In both cases, we are trying to set a
4386 // num option using a string.
4387 semsg(_(e_number_required_after_str_equal_str),
4388 name, string);
4389 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004390
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004393 if (flags & P_NUM)
4394 return set_num_option(opt_idx, varp, number,
4395 NULL, 0, opt_flags);
4396 else
4397 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01004399
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004401 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402}
4403
4404/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004405 * Call set_option_value() and when an error is returned report it.
4406 */
4407 void
4408set_option_value_give_err(
4409 char_u *name,
4410 long number,
4411 char_u *string,
4412 int opt_flags) // OPT_LOCAL or 0 (both)
4413{
4414 char *errmsg = set_option_value(name, number, string, opt_flags);
4415
4416 if (errmsg != NULL)
4417 emsg(_(errmsg));
4418}
4419
4420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 * Get the terminal code for a terminal option.
4422 * Returns NULL when not found.
4423 */
4424 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004425get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426{
4427 int opt_idx;
4428 char_u *varp;
4429
4430 if (tname[0] != 't' || tname[1] != '_' ||
4431 tname[2] == NUL || tname[3] == NUL)
4432 return NULL;
4433 if ((opt_idx = findoption(tname)) >= 0)
4434 {
4435 varp = get_varp(&(options[opt_idx]));
4436 if (varp != NULL)
4437 varp = *(char_u **)(varp);
4438 return varp;
4439 }
4440 return find_termcode(tname + 2);
4441}
4442
4443 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004444get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445{
4446 int i;
4447
4448 i = findoption((char_u *)"hl");
4449 if (i >= 0)
4450 return options[i].def_val[VI_DEFAULT];
4451 return (char_u *)NULL;
4452}
4453
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004454 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004455get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004456{
4457 int i;
4458
4459 i = findoption((char_u *)"enc");
4460 if (i >= 0)
4461 return options[i].def_val[VI_DEFAULT];
4462 return (char_u *)NULL;
4463}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004464
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004465#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004466 int
4467is_option_allocated(char *name)
4468{
4469 int idx = findoption((char_u *)name);
4470
4471 return idx >= 0 && (options[idx].flags & P_ALLOCED);
4472}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004473#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004474
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004475#if defined(FEAT_EVAL) || defined(PROTO)
4476/*
4477 * Return TRUE if "name" is a string option.
4478 * Returns FALSE if option "name" does not exist.
4479 */
4480 int
4481is_string_option(char_u *name)
4482{
4483 int idx = findoption(name);
4484
4485 return idx >= 0 && (options[idx].flags & P_STRING);
4486}
4487#endif
4488
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489/*
4490 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004491 * When "has_lt" is true there is a '<' before "*arg_arg".
4492 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 */
4494 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004495find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004497 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004499 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
4501 /*
4502 * Don't use get_special_key_code() for t_xx, we don't want it to call
4503 * add_termcap_entry().
4504 */
4505 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4506 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004507 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004509 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004511 key = find_special_key(&arg, &modifiers,
4512 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004513 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 key = 0;
4515 }
4516 return key;
4517}
4518
4519/*
4520 * if 'all' == 0: show changed options
4521 * if 'all' == 1: show all normal options
4522 * if 'all' == 2: show all terminal options
4523 */
4524 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004525showoptions(
4526 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004527 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528{
4529 struct vimoption *p;
4530 int col;
4531 int isterm;
4532 char_u *varp;
4533 struct vimoption **items;
4534 int item_count;
4535 int run;
4536 int row, rows;
4537 int cols;
4538 int i;
4539 int len;
4540
4541#define INC 20
4542#define GAP 3
4543
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004544 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (items == NULL)
4546 return;
4547
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004548 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004550 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004552 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004554 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004556 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
4558 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004559 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 * 1. display the short items
4561 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004562 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 */
4564 for (run = 1; run <= 2 && !got_int; ++run)
4565 {
4566 /*
4567 * collect the items in items[]
4568 */
4569 item_count = 0;
4570 for (p = &options[0]; p->fullname != NULL; p++)
4571 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004572 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004573 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004574 continue;
4575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 varp = NULL;
4577 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004578 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 {
4580 if (p->indir != PV_NONE && !isterm)
4581 varp = get_varp_scope(p, opt_flags);
4582 }
4583 else
4584 varp = get_varp(p);
4585 if (varp != NULL
4586 && ((all == 2 && isterm)
4587 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004588 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004590 if (opt_flags & OPT_ONECOLUMN)
4591 len = Columns;
4592 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004593 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 else
4595 {
4596 option_value2string(p, opt_flags);
4597 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4598 }
4599 if ((len <= INC - GAP && run == 1) ||
4600 (len > INC - GAP && run == 2))
4601 items[item_count++] = p;
4602 }
4603 }
4604
4605 /*
4606 * display the items
4607 */
4608 if (run == 1)
4609 {
4610 cols = (Columns + GAP - 3) / INC;
4611 if (cols == 0)
4612 cols = 1;
4613 rows = (item_count + cols - 1) / cols;
4614 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004615 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 rows = item_count;
4617 for (row = 0; row < rows && !got_int; ++row)
4618 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004619 msg_putchar('\n'); // go to next line
4620 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 break;
4622 col = 0;
4623 for (i = row; i < item_count; i += rows)
4624 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004625 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 showoneopt(items[i], opt_flags);
4627 col += INC;
4628 }
4629 out_flush();
4630 ui_breakcheck();
4631 }
4632 }
4633 vim_free(items);
4634}
4635
4636/*
4637 * Return TRUE if option "p" has its default value.
4638 */
4639 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004640optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641{
4642 int dvi;
4643
4644 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004645 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004646 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004648 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004650 // the cast to long is required for Manx C, long_i is
4651 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004652 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004653 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4655}
4656
4657/*
4658 * showoneopt: show the value of one option
4659 * must not be called with a hidden option!
4660 */
4661 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004662showoneopt(
4663 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004664 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004666 char_u *varp;
4667 int save_silent = silent_mode;
4668
4669 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004670 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
4672 varp = get_varp_scope(p, opt_flags);
4673
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004674 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4676 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004677 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004679 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004681 msg_puts(" ");
4682 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 if (!(p->flags & P_BOOL))
4684 {
4685 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004686 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 option_value2string(p, opt_flags);
4688 msg_outtrans(NameBuff);
4689 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004690
4691 silent_mode = save_silent;
4692 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693}
4694
4695/*
4696 * Write modified options as ":set" commands to a file.
4697 *
4698 * There are three values for "opt_flags":
4699 * OPT_GLOBAL: Write global option values and fresh values of
4700 * buffer-local options (used for start of a session
4701 * file).
4702 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4703 * curwin (used for a vimrc file).
4704 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4705 * and local values for window-local options of
4706 * curwin. Local values are also written when at the
4707 * default value, because a modeline or autocommand
4708 * may have set them when doing ":edit file" and the
4709 * user has set them back at the default or fresh
4710 * value.
4711 * When "local_only" is TRUE, don't write fresh
4712 * values, only local values (for ":mkview").
4713 * (fresh value = value used for a new buffer or window for a local option).
4714 *
4715 * Return FAIL on error, OK otherwise.
4716 */
4717 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004718makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719{
4720 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004721 char_u *varp; // currently used value
4722 char_u *varp_fresh; // local value
4723 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 char *cmd;
4725 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004726 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
4728 /*
4729 * The options that don't have a default (terminal name, columns, lines)
4730 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004731 * Do the loop over "options[]" twice: once for options with the
4732 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004734 for (pri = 1; pri >= 0; --pri)
4735 {
4736 for (p = &options[0]; !istermoption(p); p++)
4737 if (!(p->flags & P_NO_MKRC)
4738 && !istermoption(p)
4739 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004741 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4743 continue;
4744
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004745 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4746 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4748 continue;
4749
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004750 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004752 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 continue;
4754
Bram Moolenaard23b7142021-04-17 21:04:34 +02004755 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4756 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004757 continue;
4758
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 round = 2;
4760 if (p->indir != PV_NONE)
4761 {
4762 if (p->var == VAR_WIN)
4763 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004764 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 if (!(opt_flags & OPT_LOCAL))
4766 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004767 // When fresh value of window-local option is not at the
4768 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4770 {
4771 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004772 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
4774 round = 1;
4775 varp_local = varp;
4776 varp = varp_fresh;
4777 }
4778 }
4779 }
4780 }
4781
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004782 // Round 1: fresh value for window-local options.
4783 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 for ( ; round <= 2; varp = varp_local, ++round)
4785 {
4786 if (round == 1 || (opt_flags & OPT_GLOBAL))
4787 cmd = "set";
4788 else
4789 cmd = "setlocal";
4790
4791 if (p->flags & P_BOOL)
4792 {
4793 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4794 return FAIL;
4795 }
4796 else if (p->flags & P_NUM)
4797 {
4798 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4799 return FAIL;
4800 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004801 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004803 int do_endif = FALSE;
4804
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004805 // Don't set 'syntax' and 'filetype' again if the value is
4806 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004807 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004808#if defined(FEAT_SYN_HL)
4809 p->indir == PV_SYN ||
4810#endif
4811 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 {
4813 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4814 *(char_u **)(varp)) < 0
4815 || put_eol(fd) < 0)
4816 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004817 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
4819 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004820 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004822 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 {
4824 if (put_line(fd, "endif") == FAIL)
4825 return FAIL;
4826 }
4827 }
4828 }
4829 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 return OK;
4832}
4833
4834#if defined(FEAT_FOLDING) || defined(PROTO)
4835/*
4836 * Generate set commands for the local fold options only. Used when
4837 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4838 */
4839 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004840makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004842 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004844 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 == FAIL
4846# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004847 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004849 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 == FAIL
4851 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4852 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4853 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4854 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4855 )
4856 return FAIL;
4857
4858 return OK;
4859}
4860#endif
4861
4862 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004863put_setstring(
4864 FILE *fd,
4865 char *cmd,
4866 char *name,
4867 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004868 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869{
4870 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004871 char_u *buf = NULL;
4872 char_u *part = NULL;
4873 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
4875 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4876 return FAIL;
4877 if (*valuep != NULL)
4878 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004879 // Output 'pastetoggle' as key names. For other
4880 // options some characters have to be escaped with
4881 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 if (valuep == &p_pt)
4883 {
4884 s = *valuep;
4885 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004886 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 return FAIL;
4888 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004889 // expand the option value, replace $HOME by ~
4890 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004892 int size = (int)STRLEN(*valuep) + 1;
4893
4894 // replace home directory in the whole option value into "buf"
4895 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004896 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004897 goto fail;
4898 home_replace(NULL, *valuep, buf, size, FALSE);
4899
4900 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004901 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004902 // can be expanded when read back.
4903 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4904 && vim_strchr(*valuep, ',') != NULL)
4905 {
4906 part = alloc(size);
4907 if (part == NULL)
4908 goto fail;
4909
4910 // write line break to clear the option, e.g. ':set rtp='
4911 if (put_eol(fd) == FAIL)
4912 goto fail;
4913
4914 p = buf;
4915 while (*p != NUL)
4916 {
4917 // for each comma separated option part, append value to
4918 // the option, :set rtp+=value
4919 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4920 goto fail;
4921 (void)copy_option_part(&p, part, size, ",");
4922 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4923 goto fail;
4924 }
4925 vim_free(buf);
4926 vim_free(part);
4927 return OK;
4928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004930 {
4931 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004933 }
4934 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
4936 else if (put_escstr(fd, *valuep, 2) == FAIL)
4937 return FAIL;
4938 }
4939 if (put_eol(fd) < 0)
4940 return FAIL;
4941 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004942fail:
4943 vim_free(buf);
4944 vim_free(part);
4945 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946}
4947
4948 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004949put_setnum(
4950 FILE *fd,
4951 char *cmd,
4952 char *name,
4953 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
4955 long wc;
4956
4957 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4958 return FAIL;
4959 if (wc_use_keyname((char_u *)valuep, &wc))
4960 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004961 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4963 return FAIL;
4964 }
4965 else if (fprintf(fd, "%ld", *valuep) < 0)
4966 return FAIL;
4967 if (put_eol(fd) < 0)
4968 return FAIL;
4969 return OK;
4970}
4971
4972 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004973put_setbool(
4974 FILE *fd,
4975 char *cmd,
4976 char *name,
4977 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004979 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004980 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4982 || put_eol(fd) < 0)
4983 return FAIL;
4984 return OK;
4985}
4986
4987/*
4988 * Clear all the terminal options.
4989 * If the option has been allocated, free the memory.
4990 * Terminal options are never hidden or indirect.
4991 */
4992 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004993clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 /*
4996 * Reset a few things before clearing the old options. This may cause
4997 * outputting a few things that the terminal doesn't understand, but the
4998 * screen will be cleared later, so this is OK.
4999 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005000 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005001 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005003 // When starting the GUI close the display opened for the clipboard.
5004 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 if (gui.starting)
5006 clear_xterm_clip();
5007#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005008 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005010 free_termoptions();
5011}
5012
5013 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005014free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005015{
5016 struct vimoption *p;
5017
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005018 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (istermoption(p))
5020 {
5021 if (p->flags & P_ALLOCED)
5022 free_string_option(*(char_u **)(p->var));
5023 if (p->flags & P_DEF_ALLOCED)
5024 free_string_option(p->def_val[VI_DEFAULT]);
5025 *(char_u **)(p->var) = empty_option;
5026 p->def_val[VI_DEFAULT] = empty_option;
5027 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005028#ifdef FEAT_EVAL
5029 // remember where the option was cleared
5030 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 clear_termcodes();
5034}
5035
5036/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005037 * Free the string for one term option, if it was allocated.
5038 * Set the string to empty_option and clear allocated flag.
5039 * "var" points to the option value.
5040 */
5041 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005042free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005043{
5044 struct vimoption *p;
5045
5046 for (p = &options[0]; p->fullname != NULL; p++)
5047 if (p->var == var)
5048 {
5049 if (p->flags & P_ALLOCED)
5050 free_string_option(*(char_u **)(p->var));
5051 *(char_u **)(p->var) = empty_option;
5052 p->flags &= ~P_ALLOCED;
5053 break;
5054 }
5055}
5056
5057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 * Set the terminal option defaults to the current value.
5059 * Used after setting the terminal name.
5060 */
5061 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005062set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063{
5064 struct vimoption *p;
5065
5066 for (p = &options[0]; p->fullname != NULL; p++)
5067 {
5068 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5069 {
5070 if (p->flags & P_DEF_ALLOCED)
5071 {
5072 free_string_option(p->def_val[VI_DEFAULT]);
5073 p->flags &= ~P_DEF_ALLOCED;
5074 }
5075 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5076 if (p->flags & P_ALLOCED)
5077 {
5078 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005079 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081 }
5082 }
5083}
5084
5085/*
5086 * return TRUE if 'p' starts with 't_'
5087 */
5088 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005089istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090{
5091 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5092}
5093
Bram Moolenaardac13472019-09-16 21:06:21 +02005094/*
5095 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5096 */
5097 int
5098istermoption_idx(int opt_idx)
5099{
5100 return istermoption(&options[opt_idx]);
5101}
5102
Bram Moolenaar113e1072019-01-20 15:30:40 +01005103#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005105 * Unset local option value, similar to ":set opt<".
5106 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005107 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005108unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005109{
5110 struct vimoption *p;
5111 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005112 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005113
5114 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005115 if (opt_idx < 0)
5116 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005117 p = &(options[opt_idx]);
5118
5119 switch ((int)p->indir)
5120 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005121 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005122 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005123 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005124 break;
5125 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005126 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005127 break;
5128 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005129 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005130 break;
5131 case PV_AR:
5132 buf->b_p_ar = -1;
5133 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005134 case PV_BKC:
5135 clear_string_option(&buf->b_p_bkc);
5136 buf->b_bkc_flags = 0;
5137 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005138 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005139 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005140 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005141 case PV_TC:
5142 clear_string_option(&buf->b_p_tc);
5143 buf->b_tc_flags = 0;
5144 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005145 case PV_SISO:
5146 curwin->w_p_siso = -1;
5147 break;
5148 case PV_SO:
5149 curwin->w_p_so = -1;
5150 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005151#ifdef FEAT_FIND_ID
5152 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005153 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005154 break;
5155 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005156 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005157 break;
5158#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005159 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005160 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005161 break;
5162 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005163 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005164 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005165#ifdef FEAT_COMPL_FUNC
5166 case PV_TSRFU:
5167 clear_string_option(&buf->b_p_tsrfu);
5168 break;
5169#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005170 case PV_FP:
5171 clear_string_option(&buf->b_p_fp);
5172 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005173#ifdef FEAT_QUICKFIX
5174 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005175 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005176 break;
5177 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005178 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005179 break;
5180 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005181 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005182 break;
5183#endif
5184#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5185 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005186 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005187 break;
5188#endif
5189#if defined(FEAT_CRYPT)
5190 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005191 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005192 break;
5193#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005194#ifdef FEAT_LINEBREAK
5195 case PV_SBR:
5196 clear_string_option(&((win_T *)from)->w_p_sbr);
5197 break;
5198#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005199#ifdef FEAT_STL_OPT
5200 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005201 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005202 break;
5203#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005204 case PV_UL:
5205 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5206 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005207 case PV_LW:
5208 clear_string_option(&buf->b_p_lw);
5209 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005210 case PV_MENC:
5211 clear_string_option(&buf->b_p_menc);
5212 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005213 case PV_LCS:
5214 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005215 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005216 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005217 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005218 case PV_FCS:
5219 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005220 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005221 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005222 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005223 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005224 clear_string_option(&((win_T *)from)->w_p_ve);
5225 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005226 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005227 }
5228}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005229#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005230
5231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005233 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 */
5235 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005236get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005238 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 {
5240 if (p->var == VAR_WIN)
5241 return (char_u *)GLOBAL_WO(get_varp(p));
5242 return p->var;
5243 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005244 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 {
5246 switch ((int)p->indir)
5247 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005248 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005250 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5251 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5252 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005254 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5255 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5256 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005257 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005258 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005259 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005260 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5261 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005263 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5264 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005266 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5267 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005268#ifdef FEAT_COMPL_FUNC
5269 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5270#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005271#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5272 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5273#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005274#if defined(FEAT_CRYPT)
5275 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5276#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005277#ifdef FEAT_LINEBREAK
5278 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5279#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005280#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005281 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005282#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005283 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005284 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005285 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005286 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005287 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005288 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005289 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005290
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005292 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
5294 return get_varp(p);
5295}
5296
5297/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005298 * Get pointer to option variable at 'opt_idx', depending on local or global
5299 * scope.
5300 */
5301 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005302get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005303{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005304 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005305}
5306
5307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 * Get pointer to option variable.
5309 */
5310 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005311get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005313 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 if (p->var == NULL)
5315 return NULL;
5316
5317 switch ((int)p->indir)
5318 {
5319 case PV_NONE: return p->var;
5320
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005321 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005322 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005324 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005326 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005328 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005330 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005332 case PV_TC: return *curbuf->b_p_tc != NUL
5333 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005334 case PV_BKC: return *curbuf->b_p_bkc != NUL
5335 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005336 case PV_SISO: return curwin->w_p_siso >= 0
5337 ? (char_u *)&(curwin->w_p_siso) : p->var;
5338 case PV_SO: return curwin->w_p_so >= 0
5339 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005341 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005343 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5345#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005346 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005348 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005350#ifdef FEAT_COMPL_FUNC
5351 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5352 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5353#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005354 case PV_FP: return *curbuf->b_p_fp != NUL
5355 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005357 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005359 case PV_GP: return *curbuf->b_p_gp != NUL
5360 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5361 case PV_MP: return *curbuf->b_p_mp != NUL
5362 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005364#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5365 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5366 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5367#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005368#if defined(FEAT_CRYPT)
5369 case PV_CM: return *curbuf->b_p_cm != NUL
5370 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5371#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005372#ifdef FEAT_LINEBREAK
5373 case PV_SBR: return *curwin->w_p_sbr != NUL
5374 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5375#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005376#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005377 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005378 ? (char_u *)&(curwin->w_p_stl) : p->var;
5379#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005380 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5381 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005382 case PV_LW: return *curbuf->b_p_lw != NUL
5383 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005384 case PV_MENC: return *curbuf->b_p_menc != NUL
5385 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386#ifdef FEAT_ARABIC
5387 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5388#endif
5389 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005390 case PV_LCS: return *curwin->w_p_lcs != NUL
5391 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005392 case PV_FCS: return *curwin->w_p_fcs != NUL
5393 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005394 case PV_VE: return *curwin->w_p_ve != NUL
5395 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005396#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005397 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5398#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005399#ifdef FEAT_SYN_HL
5400 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5401 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005402 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005403 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405#ifdef FEAT_DIFF
5406 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5407#endif
5408#ifdef FEAT_FOLDING
5409 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5410 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5411 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5412 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5413 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5414 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5415 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5416# ifdef FEAT_EVAL
5417 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5418 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5419# endif
5420 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5421#endif
5422 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005423 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005424#ifdef FEAT_LINEBREAK
5425 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005428 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005429#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5431#endif
5432#ifdef FEAT_RIGHTLEFT
5433 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5434 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5435#endif
5436 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5437 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5438#ifdef FEAT_LINEBREAK
5439 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005440 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5441 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005443 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005445 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005446#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005447 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5448 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5449#endif
5450#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005451 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5452 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5453 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455
5456 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5457 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5460 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5462 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5464 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5465 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01005466 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469#ifdef FEAT_FOLDING
5470 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005473#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005474 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005476#ifdef FEAT_COMPL_FUNC
5477 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005478 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005479#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005480#ifdef FEAT_EVAL
5481 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005484 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005490 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5492 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5493 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5494 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5495#ifdef FEAT_FIND_ID
5496# ifdef FEAT_EVAL
5497 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5498# endif
5499#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01005500#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5502 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5503#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005504#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005505 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507#ifdef FEAT_CRYPT
5508 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5512 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5513 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5514 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5515 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005517 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5522#ifdef FEAT_SEARCHPATH
5523 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5524#endif
5525 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5526#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005527 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005528 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5529#endif
5530#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005531 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5532 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5533 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005534 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535#endif
5536 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5537 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5538 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5539 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005540#ifdef FEAT_PERSISTENT_UNDO
5541 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5544#ifdef FEAT_KEYMAP
5545 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5546#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005547#ifdef FEAT_SIGNS
5548 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5549#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005550#ifdef FEAT_VARTABS
5551 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5552 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5553#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005554 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005556 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 return (char_u *)&(curbuf->b_p_wm);
5558}
5559
5560/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005561 * Return a pointer to the variable for option at 'opt_idx'
5562 */
5563 char_u *
5564get_option_var(int opt_idx)
5565{
5566 return options[opt_idx].var;
5567}
5568
Dominique Pelle748b3082022-01-08 12:41:16 +00005569#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005570/*
5571 * Return the full name of the option at 'opt_idx'
5572 */
5573 char_u *
5574get_option_fullname(int opt_idx)
5575{
5576 return (char_u *)options[opt_idx].fullname;
5577}
Dominique Pelle748b3082022-01-08 12:41:16 +00005578#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005579
5580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 * Get the value of 'equalprg', either the buffer-local one or the global one.
5582 */
5583 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005584get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585{
5586 if (*curbuf->b_p_ep == NUL)
5587 return p_ep;
5588 return curbuf->b_p_ep;
5589}
5590
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591/*
5592 * Copy options from one window to another.
5593 * Used when splitting a window.
5594 */
5595 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005596win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597{
5598 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5599 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005600 after_copy_winopt(wp_to);
5601}
5602
5603/*
5604 * After copying window options: update variables depending on options.
5605 */
5606 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00005607after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005608{
5609#ifdef FEAT_LINEBREAK
5610 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005611#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005612#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005613 fill_culopt_flags(NULL, wp);
5614 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005615#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005616 set_chars_option(wp, &wp->w_p_lcs, TRUE);
5617 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005618}
5619
5620 static char_u *
5621copy_option_val(char_u *val)
5622{
5623 if (val == empty_option)
5624 return empty_option; // no need to allocate memory
5625 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
5628/*
5629 * Copy the options from one winopt_T to another.
5630 * Doesn't free the old option values in "to", use clear_winopt() for that.
5631 * The 'scroll' option is not copied, because it depends on the window height.
5632 * The 'previewwindow' option is reset, there can be only one preview window.
5633 */
5634 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005635copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637#ifdef FEAT_ARABIC
5638 to->wo_arab = from->wo_arab;
5639#endif
5640 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005641 to->wo_lcs = copy_option_val(from->wo_lcs);
5642 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005644 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005645 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02005646 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005647#ifdef FEAT_LINEBREAK
5648 to->wo_nuw = from->wo_nuw;
5649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650#ifdef FEAT_RIGHTLEFT
5651 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005652 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005654#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005655 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01005656#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005657#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005658 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005661#ifdef FEAT_DIFF
5662 to->wo_wrap_save = from->wo_wrap_save;
5663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664#ifdef FEAT_LINEBREAK
5665 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005666 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005667 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005669 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005671 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005672 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005673 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005674#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005675 to->wo_spell = from->wo_spell;
5676#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005677#ifdef FEAT_SYN_HL
5678 to->wo_cuc = from->wo_cuc;
5679 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005680 to->wo_culopt = copy_option_val(from->wo_culopt);
5681 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683#ifdef FEAT_DIFF
5684 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005685 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005687#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005688 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005689 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005690#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005691#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005692 to->wo_twk = copy_option_val(from->wo_twk);
5693 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695#ifdef FEAT_FOLDING
5696 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005697 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005699 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005700 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 to->wo_fml = from->wo_fml;
5702 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005703 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005704 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005705 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005706 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 to->wo_fdn = from->wo_fdn;
5708# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005709 to->wo_fde = copy_option_val(from->wo_fde);
5710 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005712 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005714#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005715 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005716#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005717
5718#ifdef FEAT_EVAL
5719 // Copy the script context so that we know where the value was last set.
5720 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5721 sizeof(to->wo_script_ctx));
5722#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005723 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724}
5725
5726/*
5727 * Check string options in a window for a NULL value.
5728 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005729 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005730check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 check_winopt(&win->w_onebuf_opt);
5733 check_winopt(&win->w_allbuf_opt);
5734}
5735
5736/*
5737 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5738 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005739 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005740check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742#ifdef FEAT_FOLDING
5743 check_string_option(&wop->wo_fdi);
5744 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005745 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746# ifdef FEAT_EVAL
5747 check_string_option(&wop->wo_fde);
5748 check_string_option(&wop->wo_fdt);
5749# endif
5750 check_string_option(&wop->wo_fmr);
5751#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005752#ifdef FEAT_SIGNS
5753 check_string_option(&wop->wo_scl);
5754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755#ifdef FEAT_RIGHTLEFT
5756 check_string_option(&wop->wo_rlc);
5757#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005758#ifdef FEAT_LINEBREAK
5759 check_string_option(&wop->wo_sbr);
5760#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005761#ifdef FEAT_STL_OPT
5762 check_string_option(&wop->wo_stl);
5763#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005764#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005765 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005766 check_string_option(&wop->wo_cc);
5767#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005768#ifdef FEAT_CONCEAL
5769 check_string_option(&wop->wo_cocu);
5770#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005771#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005772 check_string_option(&wop->wo_twk);
5773 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005774#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005775#ifdef FEAT_LINEBREAK
5776 check_string_option(&wop->wo_briopt);
5777#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005778 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005779 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005780 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005781 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782}
5783
5784/*
5785 * Free the allocated memory inside a winopt_T.
5786 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005788clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
5790#ifdef FEAT_FOLDING
5791 clear_string_option(&wop->wo_fdi);
5792 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005793 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794# ifdef FEAT_EVAL
5795 clear_string_option(&wop->wo_fde);
5796 clear_string_option(&wop->wo_fdt);
5797# endif
5798 clear_string_option(&wop->wo_fmr);
5799#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005800#ifdef FEAT_SIGNS
5801 clear_string_option(&wop->wo_scl);
5802#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005803#ifdef FEAT_LINEBREAK
5804 clear_string_option(&wop->wo_briopt);
5805#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005806 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807#ifdef FEAT_RIGHTLEFT
5808 clear_string_option(&wop->wo_rlc);
5809#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005810#ifdef FEAT_LINEBREAK
5811 clear_string_option(&wop->wo_sbr);
5812#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005813#ifdef FEAT_STL_OPT
5814 clear_string_option(&wop->wo_stl);
5815#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005816#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005817 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005818 clear_string_option(&wop->wo_cc);
5819#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005820#ifdef FEAT_CONCEAL
5821 clear_string_option(&wop->wo_cocu);
5822#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005823#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005824 clear_string_option(&wop->wo_twk);
5825 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005826#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005827 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005828 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005829 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830}
5831
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005832#ifdef FEAT_EVAL
5833// Index into the options table for a buffer-local option enum.
5834static int buf_opt_idx[BV_COUNT];
5835# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5836
5837/*
5838 * Initialize buf_opt_idx[] if not done already.
5839 */
5840 static void
5841init_buf_opt_idx(void)
5842{
5843 static int did_init_buf_opt_idx = FALSE;
5844 int i;
5845
5846 if (did_init_buf_opt_idx)
5847 return;
5848 did_init_buf_opt_idx = TRUE;
5849 for (i = 0; !istermoption_idx(i); i++)
5850 if (options[i].indir & PV_BUF)
5851 buf_opt_idx[options[i].indir & PV_MASK] = i;
5852}
5853#else
5854# define COPY_OPT_SCTX(buf, bv)
5855#endif
5856
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857/*
5858 * Copy global option values to local options for one buffer.
5859 * Used when creating a new buffer and sometimes when entering a buffer.
5860 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005861 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5863 * appropriate.
5864 * BCO_NOHELP Don't copy the values to a help buffer.
5865 */
5866 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005867buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
5869 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005870 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 int dont_do_help;
5872 int did_isk = FALSE;
5873
5874 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 * Skip this when the option defaults have not been set yet. Happens when
5876 * main() allocates the first buffer.
5877 */
5878 if (p_cpo != NULL)
5879 {
5880 /*
5881 * Always copy when entering and 'cpo' contains 'S'.
5882 * Don't copy when already initialized.
5883 * Don't copy when 'cpo' contains 's' and not entering.
5884 * 'S' BCO_ENTER initialized 's' should_copy
5885 * yes yes X X TRUE
5886 * yes no yes X FALSE
5887 * no X yes X FALSE
5888 * X no no yes FALSE
5889 * X no no no TRUE
5890 * no yes no X TRUE
5891 */
5892 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5893 && (buf->b_p_initialized
5894 || (!(flags & BCO_ENTER)
5895 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5896 should_copy = FALSE;
5897
5898 if (should_copy || (flags & BCO_ALWAYS))
5899 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005900#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005901 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005902 init_buf_opt_idx();
5903#endif
5904 // Don't copy the options specific to a help buffer when
5905 // BCO_NOHELP is given or the options were initialized already
5906 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5908 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005909 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {
5911 save_p_isk = buf->b_p_isk;
5912 buf->b_p_isk = NULL;
5913 }
5914 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005915 * Always free the allocated strings. If not already initialized,
5916 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 */
5918 if (!buf->b_p_initialized)
5919 {
5920 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005921 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005924 switch (*p_ffs)
5925 {
5926 case 'm':
5927 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5928 case 'd':
5929 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5930 case 'u':
5931 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5932 default:
5933 buf->b_p_ff = vim_strsave(p_ff);
5934 }
5935 if (buf->b_p_ff != NULL)
5936 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 buf->b_p_bh = empty_option;
5938 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 }
5940 else
5941 free_buf_options(buf, FALSE);
5942
5943 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005944 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 buf->b_p_ai_nopaste = p_ai_nopaste;
5946 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005947 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005949 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 buf->b_p_tw_nopaste = p_tw_nopaste;
5951 buf->b_p_tw_nobin = p_tw_nobin;
5952 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005953 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 buf->b_p_wm_nopaste = p_wm_nopaste;
5955 buf->b_p_wm_nobin = p_wm_nobin;
5956 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005957 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005958 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005959 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005960 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005961 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005963 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005965 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005967 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 buf->b_p_ml_nobin = p_ml_nobin;
5969 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005970 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005971 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005972 buf->b_p_swf = FALSE;
5973 else
5974 {
5975 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00005976 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005979 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005980#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005981 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005982 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005984#ifdef FEAT_COMPL_FUNC
5985 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005986 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005987 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005988 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005989 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005990 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005991#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005992#ifdef FEAT_EVAL
5993 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005994 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005995 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005998 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006000#ifdef FEAT_VARTABS
6001 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006002 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006003 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006004 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006005 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006006 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006007 buf->b_p_vsts_nopaste = p_vsts_nopaste
6008 ? vim_strsave(p_vsts_nopaste) : NULL;
6009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006011 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006013 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014#ifdef FEAT_FOLDING
6015 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006016 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017#endif
6018 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006019 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006020 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006021 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006022 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6023 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006025 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006027 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006029 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006031 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006032
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006034 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006036 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006038 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006039 buf->b_p_cinsd = vim_strsave(p_cinsd);
6040 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006041
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006042 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006045 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006047 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006049 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006051 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006053 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006054 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006055 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006056#endif
6057#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006058 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006059 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006060 (void)compile_cap_prog(&buf->b_s);
6061 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006062 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006063 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006064 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006065 buf->b_s.b_p_spo = vim_strsave(p_spo);
6066 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006068#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006070 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006072 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006074 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006075#if defined(FEAT_EVAL)
6076 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006077 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079#ifdef FEAT_CRYPT
6080 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006081 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082#endif
6083#ifdef FEAT_SEARCHPATH
6084 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006085 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086#endif
6087#ifdef FEAT_KEYMAP
6088 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006089 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 buf->b_kmap_state |= KEYMAP_INIT;
6091#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006092#ifdef FEAT_TERMINAL
6093 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006094 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006095#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006096 // This isn't really an option, but copying the langmap and IME
6097 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006099 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006101 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006103 // options that are normally global but also have a local value
6104 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006106 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006107 buf->b_p_bkc = empty_option;
6108 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109#ifdef FEAT_QUICKFIX
6110 buf->b_p_gp = empty_option;
6111 buf->b_p_mp = empty_option;
6112 buf->b_p_efm = empty_option;
6113#endif
6114 buf->b_p_ep = empty_option;
6115 buf->b_p_kp = empty_option;
6116 buf->b_p_path = empty_option;
6117 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006118 buf->b_p_tc = empty_option;
6119 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120#ifdef FEAT_FIND_ID
6121 buf->b_p_def = empty_option;
6122 buf->b_p_inc = empty_option;
6123# ifdef FEAT_EVAL
6124 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006125 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126# endif
6127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 buf->b_p_dict = empty_option;
6129 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006130#ifdef FEAT_COMPL_FUNC
6131 buf->b_p_tsrfu = empty_option;
6132#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006133 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006134 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006135#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6136 buf->b_p_bexpr = empty_option;
6137#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006138#if defined(FEAT_CRYPT)
6139 buf->b_p_cm = empty_option;
6140#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006141#ifdef FEAT_PERSISTENT_UNDO
6142 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006143 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006144#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006145 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006146 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147
6148 /*
6149 * Don't copy the options set by ex_help(), use the saved values,
6150 * when going from a help buffer to a non-help buffer.
6151 * Don't touch these at all when BCO_NOHELP is used and going from
6152 * or to a help buffer.
6153 */
6154 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006155 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006157#ifdef FEAT_VARTABS
6158 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006159 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006160 else
6161 buf->b_p_vts_array = NULL;
6162#endif
6163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 else
6165 {
6166 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006167 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 did_isk = TRUE;
6169 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006170 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006171#ifdef FEAT_VARTABS
6172 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006173 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006174 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006175 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006176 else
6177 buf->b_p_vts_array = NULL;
6178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 if (buf->b_p_bt[0] == 'h')
6181 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006183 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 }
6185 }
6186
6187 /*
6188 * When the options should be copied (ignoring BCO_ALWAYS), set the
6189 * flag that indicates that the options have been initialized.
6190 */
6191 if (should_copy)
6192 buf->b_p_initialized = TRUE;
6193 }
6194
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006195 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 if (did_isk)
6197 (void)buf_init_chartab(buf, FALSE);
6198}
6199
6200/*
6201 * Reset the 'modifiable' option and its default value.
6202 */
6203 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006204reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205{
6206 int opt_idx;
6207
6208 curbuf->b_p_ma = FALSE;
6209 p_ma = FALSE;
6210 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006211 if (opt_idx >= 0)
6212 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213}
6214
6215/*
6216 * Set the global value for 'iminsert' to the local value.
6217 */
6218 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006219set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
6221 p_iminsert = curbuf->b_p_iminsert;
6222}
6223
6224/*
6225 * Set the global value for 'imsearch' to the local value.
6226 */
6227 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006228set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
6230 p_imsearch = curbuf->b_p_imsearch;
6231}
6232
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233static int expand_option_idx = -1;
6234static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6235static int expand_option_flags = 0;
6236
6237 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006238set_context_in_set_cmd(
6239 expand_T *xp,
6240 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006241 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
6243 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006244 long_u flags = 0; // init for GCC
6245 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 char_u *p;
6247 char_u *s;
6248 int is_term_option = FALSE;
6249 int key;
6250
6251 expand_option_flags = opt_flags;
6252
6253 xp->xp_context = EXPAND_SETTINGS;
6254 if (*arg == NUL)
6255 {
6256 xp->xp_pattern = arg;
6257 return;
6258 }
6259 p = arg + STRLEN(arg) - 1;
6260 if (*p == ' ' && *(p - 1) != '\\')
6261 {
6262 xp->xp_pattern = p + 1;
6263 return;
6264 }
6265 while (p > arg)
6266 {
6267 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006268 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 if (*p == ' ' || *p == ',')
6270 {
6271 while (s > arg && *(s - 1) == '\\')
6272 --s;
6273 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006274 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 if (*p == ' ' && ((p - s) & 1) == 0)
6276 {
6277 ++p;
6278 break;
6279 }
6280 --p;
6281 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006282 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 {
6284 xp->xp_context = EXPAND_BOOL_SETTINGS;
6285 p += 2;
6286 }
6287 if (STRNCMP(p, "inv", 3) == 0)
6288 {
6289 xp->xp_context = EXPAND_BOOL_SETTINGS;
6290 p += 3;
6291 }
6292 xp->xp_pattern = arg = p;
6293 if (*arg == '<')
6294 {
6295 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006296 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 return;
6298 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006299 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 {
6301 xp->xp_context = EXPAND_NOTHING;
6302 return;
6303 }
6304 nextchar = *++p;
6305 is_term_option = TRUE;
6306 expand_option_name[2] = KEY2TERMCAP0(key);
6307 expand_option_name[3] = KEY2TERMCAP1(key);
6308 }
6309 else
6310 {
6311 if (p[0] == 't' && p[1] == '_')
6312 {
6313 p += 2;
6314 if (*p != NUL)
6315 ++p;
6316 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006317 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 nextchar = *++p;
6319 is_term_option = TRUE;
6320 expand_option_name[2] = p[-2];
6321 expand_option_name[3] = p[-1];
6322 }
6323 else
6324 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006325 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6327 p++;
6328 if (*p == NUL)
6329 return;
6330 nextchar = *p;
6331 *p = NUL;
6332 opt_idx = findoption(arg);
6333 *p = nextchar;
6334 if (opt_idx == -1 || options[opt_idx].var == NULL)
6335 {
6336 xp->xp_context = EXPAND_NOTHING;
6337 return;
6338 }
6339 flags = options[opt_idx].flags;
6340 if (flags & P_BOOL)
6341 {
6342 xp->xp_context = EXPAND_NOTHING;
6343 return;
6344 }
6345 }
6346 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006347 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6349 {
6350 ++p;
6351 nextchar = '=';
6352 }
6353 if ((nextchar != '=' && nextchar != ':')
6354 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6355 {
6356 xp->xp_context = EXPAND_UNSUCCESSFUL;
6357 return;
6358 }
6359 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6360 {
6361 xp->xp_context = EXPAND_OLD_SETTING;
6362 if (is_term_option)
6363 expand_option_idx = -1;
6364 else
6365 expand_option_idx = opt_idx;
6366 xp->xp_pattern = p + 1;
6367 return;
6368 }
6369 xp->xp_context = EXPAND_NOTHING;
6370 if (is_term_option || (flags & P_NUM))
6371 return;
6372
6373 xp->xp_pattern = p + 1;
6374
6375 if (flags & P_EXPAND)
6376 {
6377 p = options[opt_idx].var;
6378 if (p == (char_u *)&p_bdir
6379 || p == (char_u *)&p_dir
6380 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006381 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 || p == (char_u *)&p_rtp
6383#ifdef FEAT_SEARCHPATH
6384 || p == (char_u *)&p_cdpath
6385#endif
6386#ifdef FEAT_SESSION
6387 || p == (char_u *)&p_vdir
6388#endif
6389 )
6390 {
6391 xp->xp_context = EXPAND_DIRECTORIES;
6392 if (p == (char_u *)&p_path
6393#ifdef FEAT_SEARCHPATH
6394 || p == (char_u *)&p_cdpath
6395#endif
6396 )
6397 xp->xp_backslash = XP_BS_THREE;
6398 else
6399 xp->xp_backslash = XP_BS_ONE;
6400 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006401 else if (p == (char_u *)&p_ft)
6402 {
6403 xp->xp_context = EXPAND_FILETYPE;
6404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 else
6406 {
6407 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006408 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 if (p == (char_u *)&p_tags)
6410 xp->xp_backslash = XP_BS_THREE;
6411 else
6412 xp->xp_backslash = XP_BS_ONE;
6413 }
6414 }
6415
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006416 // For an option that is a list of file names, find the start of the
6417 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6419 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006420 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 if (*p == ' ' || *p == ',')
6422 {
6423 s = p;
6424 while (s > xp->xp_pattern && *(s - 1) == '\\')
6425 --s;
6426 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6427 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6428 {
6429 xp->xp_pattern = p + 1;
6430 break;
6431 }
6432 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006433
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006434#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006435 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006436 if (options[opt_idx].var == (char_u *)&p_sps
6437 && STRNCMP(p, "file:", 5) == 0)
6438 {
6439 xp->xp_pattern = p + 5;
6440 break;
6441 }
6442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444}
6445
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006446/*
6447 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
6448 *
6449 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
6450 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
6451 *
6452 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
6453 * regular expression 'regmatch', then stores the match in matches[idx] and
6454 * returns TRUE.
6455 *
6456 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6457 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
6458 *
6459 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6460 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
6461 */
6462 static int
6463match_str(
6464 char_u *str,
6465 regmatch_T *regmatch,
6466 char_u **matches,
6467 int idx,
6468 int test_only,
6469 int fuzzy,
6470 char_u *fuzzystr,
6471 fuzmatch_str_T *fuzmatch)
6472{
6473 if (!fuzzy)
6474 {
6475 if (vim_regexec(regmatch, str, (colnr_T)0))
6476 {
6477 if (!test_only)
6478 matches[idx] = vim_strsave(str);
6479 return TRUE;
6480 }
6481 }
6482 else
6483 {
6484 int score;
6485
6486 score = fuzzy_match_str(str, fuzzystr);
6487 if (score != 0)
6488 {
6489 if (!test_only)
6490 {
6491 fuzmatch[idx].idx = idx;
6492 fuzmatch[idx].str = vim_strsave(str);
6493 fuzmatch[idx].score = score;
6494 }
6495
6496 return TRUE;
6497 }
6498 }
6499
6500 return FALSE;
6501}
6502
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006504ExpandSettings(
6505 expand_T *xp,
6506 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006507 char_u *fuzzystr,
6508 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01006509 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006510 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006512 int num_normal = 0; // Nr of matching non-term-code settings
6513 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 int opt_idx;
6515 int match;
6516 int count = 0;
6517 char_u *str;
6518 int loop;
6519 int is_term_opt;
6520 char_u name_buf[MAX_KEY_NAME_LEN];
6521 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006522 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006523 int fuzzy;
6524 fuzmatch_str_T *fuzmatch = NULL;
6525
Christian Brabandtcb747892022-05-08 21:10:56 +01006526 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006528 // do this loop twice:
6529 // loop == 0: count the number of matching options
6530 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 for (loop = 0; loop <= 1; ++loop)
6532 {
6533 regmatch->rm_ic = ic;
6534 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6535 {
K.Takataeeec2542021-06-02 13:28:16 +02006536 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006537 {
6538 if (match_str((char_u *)names[match], regmatch, *matches,
6539 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 {
6541 if (loop == 0)
6542 num_normal++;
6543 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006544 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 }
6548 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6549 opt_idx++)
6550 {
6551 if (options[opt_idx].var == NULL)
6552 continue;
6553 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6554 && !(options[opt_idx].flags & P_BOOL))
6555 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006556 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 if (is_term_opt && num_normal > 0)
6558 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006559
6560 if (match_str(str, regmatch, *matches, count, (loop == 0),
6561 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 {
6563 if (loop == 0)
6564 {
6565 if (is_term_opt)
6566 num_term++;
6567 else
6568 num_normal++;
6569 }
6570 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006571 count++;
6572 }
6573 else if (!fuzzy && options[opt_idx].shortname != NULL
6574 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006575 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006576 {
6577 // Compare against the abbreviated option name (for regular
6578 // expression match). Fuzzy matching (previous if) already
6579 // matches against both the expanded and abbreviated names.
6580 if (loop == 0)
6581 {
6582 if (is_term_opt)
6583 num_term++;
6584 else
6585 num_normal++;
6586 }
6587 else
6588 (*matches)[count++] = vim_strsave(str);
6589 }
6590 else if (is_term_opt)
6591 {
6592 name_buf[0] = '<';
6593 name_buf[1] = 't';
6594 name_buf[2] = '_';
6595 name_buf[3] = str[2];
6596 name_buf[4] = str[3];
6597 name_buf[5] = '>';
6598 name_buf[6] = NUL;
6599
6600 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6601 fuzzy, fuzzystr, fuzmatch))
6602 {
6603 if (loop == 0)
6604 num_term++;
6605 else
6606 count++;
6607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 }
6609 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006610
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 /*
6612 * Check terminal key codes, these are not in the option table
6613 */
6614 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6615 {
6616 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6617 {
6618 if (!isprint(str[0]) || !isprint(str[1]))
6619 continue;
6620
6621 name_buf[0] = 't';
6622 name_buf[1] = '_';
6623 name_buf[2] = str[0];
6624 name_buf[3] = str[1];
6625 name_buf[4] = NUL;
6626
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006627 if (match_str(name_buf, regmatch, *matches, count,
6628 (loop == 0), fuzzy, fuzzystr, fuzmatch))
6629 {
6630 if (loop == 0)
6631 num_term++;
6632 else
6633 count++;
6634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 else
6636 {
6637 name_buf[0] = '<';
6638 name_buf[1] = 't';
6639 name_buf[2] = '_';
6640 name_buf[3] = str[0];
6641 name_buf[4] = str[1];
6642 name_buf[5] = '>';
6643 name_buf[6] = NUL;
6644
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006645 if (match_str(name_buf, regmatch, *matches, count,
6646 (loop == 0), fuzzy, fuzzystr,
6647 fuzmatch))
6648 {
6649 if (loop == 0)
6650 num_term++;
6651 else
6652 count++;
6653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 }
6655 }
6656
6657 /*
6658 * Check special key names.
6659 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006660 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6662 {
6663 name_buf[0] = '<';
6664 STRCPY(name_buf + 1, str);
6665 STRCAT(name_buf, ">");
6666
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006667 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6668 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 {
6670 if (loop == 0)
6671 num_term++;
6672 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006673 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 }
6675 }
6676 }
6677 if (loop == 0)
6678 {
6679 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006680 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006682 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 else
6684 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006685 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006687 *matches = ALLOC_MULT(char_u *, *numMatches);
6688 if (*matches == NULL)
6689 {
6690 *matches = (char_u **)"";
6691 return FAIL;
6692 }
6693 }
6694 else
6695 {
6696 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
6697 if (fuzmatch == NULL)
6698 {
6699 *matches = (char_u **)"";
6700 return FAIL;
6701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 }
6703 }
6704 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006705
6706 if (fuzzy &&
6707 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
6708 return FAIL;
6709
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 return OK;
6711}
6712
6713 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006714ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006716 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 char_u *buf;
6718
6719 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006720 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 if (*file == NULL)
6722 return FAIL;
6723
6724 /*
6725 * For a terminal key code expand_option_idx is < 0.
6726 */
6727 if (expand_option_idx < 0)
6728 {
6729 var = find_termcode(expand_option_name + 2);
6730 if (var == NULL)
6731 expand_option_idx = findoption(expand_option_name);
6732 }
6733
6734 if (expand_option_idx >= 0)
6735 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006736 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 option_value2string(&options[expand_option_idx], expand_option_flags);
6738 var = NameBuff;
6739 }
6740 else if (var == NULL)
6741 var = (char_u *)"";
6742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006743 // A backslash is required before some characters. This is the reverse of
6744 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 buf = vim_strsave_escaped(var, escape_chars);
6746
6747 if (buf == NULL)
6748 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006749 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 return FAIL;
6751 }
6752
6753#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006754 // For MS-Windows et al. we don't double backslashes at the start and
6755 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006756 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 if (var[0] == '\\' && var[1] == '\\'
6758 && expand_option_idx >= 0
6759 && (options[expand_option_idx].flags & P_EXPAND)
6760 && vim_isfilec(var[2])
6761 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006762 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763#endif
6764
6765 *file[0] = buf;
6766 *num_file = 1;
6767 return OK;
6768}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769
6770/*
6771 * Get the value for the numeric or string option *opp in a nice format into
6772 * NameBuff[]. Must not be called with a hidden option!
6773 */
6774 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006775option_value2string(
6776 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006777 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
6779 char_u *varp;
6780
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006781 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
6783 if (opp->flags & P_NUM)
6784 {
6785 long wc = 0;
6786
6787 if (wc_use_keyname(varp, &wc))
6788 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6789 else if (wc != 0)
6790 STRCPY(NameBuff, transchar((int)wc));
6791 else
6792 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6793 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006794 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 {
6796 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006797 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 NameBuff[0] = NUL;
6799#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006800 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006801 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 STRCPY(NameBuff, "*****");
6803#endif
6804 else if (opp->flags & P_EXPAND)
6805 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006806 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 else if ((char_u **)opp->var == &p_pt)
6808 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6809 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006810 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
6812}
6813
6814/*
6815 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6816 * printed as a keyname.
6817 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6818 */
6819 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006820wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
6822 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6823 {
6824 *wcp = *(long *)varp;
6825 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6826 return TRUE;
6827 }
6828 return FALSE;
6829}
6830
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 * Return TRUE if "x" is present in 'shortmess' option, or
6833 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6834 */
6835 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006836shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006838 return p_shm != NULL &&
6839 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 || (vim_strchr(p_shm, 'a') != NULL
6841 && vim_strchr((char_u *)SHM_A, x) != NULL));
6842}
6843
6844/*
6845 * paste_option_changed() - Called after p_paste was set or reset.
6846 */
6847 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006848paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850 static int old_p_paste = FALSE;
6851 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006852 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853#ifdef FEAT_CMDL_INFO
6854 static int save_ru = 0;
6855#endif
6856#ifdef FEAT_RIGHTLEFT
6857 static int save_ri = 0;
6858 static int save_hkmap = 0;
6859#endif
6860 buf_T *buf;
6861
6862 if (p_paste)
6863 {
6864 /*
6865 * Paste switched from off to on.
6866 * Save the current values, so they can be restored later.
6867 */
6868 if (!old_p_paste)
6869 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006870 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006871 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {
6873 buf->b_p_tw_nopaste = buf->b_p_tw;
6874 buf->b_p_wm_nopaste = buf->b_p_wm;
6875 buf->b_p_sts_nopaste = buf->b_p_sts;
6876 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006877 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006878#ifdef FEAT_VARTABS
6879 if (buf->b_p_vsts_nopaste)
6880 vim_free(buf->b_p_vsts_nopaste);
6881 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6882 ? vim_strsave(buf->b_p_vsts) : NULL;
6883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 }
6885
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006886 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006888 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889#ifdef FEAT_CMDL_INFO
6890 save_ru = p_ru;
6891#endif
6892#ifdef FEAT_RIGHTLEFT
6893 save_ri = p_ri;
6894 save_hkmap = p_hkmap;
6895#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006896 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006897 p_ai_nopaste = p_ai;
6898 p_et_nopaste = p_et;
6899 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 p_tw_nopaste = p_tw;
6901 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006902#ifdef FEAT_VARTABS
6903 if (p_vsts_nopaste)
6904 vim_free(p_vsts_nopaste);
6905 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 }
6908
6909 /*
6910 * Always set the option values, also when 'paste' is set when it is
6911 * already on.
6912 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006913 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006914 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006916 buf->b_p_tw = 0; // textwidth is 0
6917 buf->b_p_wm = 0; // wrapmargin is 0
6918 buf->b_p_sts = 0; // softtabstop is 0
6919 buf->b_p_ai = 0; // no auto-indent
6920 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006921#ifdef FEAT_VARTABS
6922 if (buf->b_p_vsts)
6923 free_string_option(buf->b_p_vsts);
6924 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006925 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 }
6928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006929 // set global options
6930 p_sm = 0; // no showmatch
6931 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006934 status_redraw_all(); // redraw to remove the ruler
6935 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936#endif
6937#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006938 p_ri = 0; // no reverse insert
6939 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006941 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 p_tw = 0;
6943 p_wm = 0;
6944 p_sts = 0;
6945 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006946#ifdef FEAT_VARTABS
6947 if (p_vsts)
6948 free_string_option(p_vsts);
6949 p_vsts = empty_option;
6950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 }
6952
6953 /*
6954 * Paste switched from on to off: Restore saved values.
6955 */
6956 else if (old_p_paste)
6957 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006958 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006959 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 {
6961 buf->b_p_tw = buf->b_p_tw_nopaste;
6962 buf->b_p_wm = buf->b_p_wm_nopaste;
6963 buf->b_p_sts = buf->b_p_sts_nopaste;
6964 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006965 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006966#ifdef FEAT_VARTABS
6967 if (buf->b_p_vsts)
6968 free_string_option(buf->b_p_vsts);
6969 buf->b_p_vsts = buf->b_p_vsts_nopaste
6970 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006971 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006972 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006973 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006974 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006975 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 }
6978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006979 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006981 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006984 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 p_ru = save_ru;
6986#endif
6987#ifdef FEAT_RIGHTLEFT
6988 p_ri = save_ri;
6989 p_hkmap = save_hkmap;
6990#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006991 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006992 p_ai = p_ai_nopaste;
6993 p_et = p_et_nopaste;
6994 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 p_tw = p_tw_nopaste;
6996 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006997#ifdef FEAT_VARTABS
6998 if (p_vsts)
6999 free_string_option(p_vsts);
7000 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 }
7003
7004 old_p_paste = p_paste;
7005}
7006
7007/*
7008 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7009 *
7010 * Reset 'compatible' and set the values for options that didn't get set yet
7011 * to the Vim defaults.
7012 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007013 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 */
7015 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007016vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007018 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007019 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007020 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021
7022 if (!option_was_set((char_u *)"cp"))
7023 {
7024 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007025 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7027 set_option_default(opt_idx, OPT_FREE, FALSE);
7028 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007029 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007031
7032 if (fname != NULL)
7033 {
7034 p = vim_getenv(envname, &dofree);
7035 if (p == NULL)
7036 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007037 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007038 p = FullName_save(fname, FALSE);
7039 if (p != NULL)
7040 {
7041 vim_setenv(envname, p);
7042 vim_free(p);
7043 }
7044 }
7045 else if (dofree)
7046 vim_free(p);
7047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048}
7049
7050/*
7051 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7052 */
7053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007054change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007056 int opt_idx;
7057
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 if (p_cp != on)
7059 {
7060 p_cp = on;
7061 compatible_set();
7062 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007063 opt_idx = findoption((char_u *)"cp");
7064 if (opt_idx >= 0)
7065 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066}
7067
7068/*
7069 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007070 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 */
7072 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007073option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074{
7075 int idx;
7076
7077 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007078 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 return FALSE;
7080 if (options[idx].flags & P_WAS_SET)
7081 return TRUE;
7082 return FALSE;
7083}
7084
7085/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007086 * Reset the flag indicating option "name" was set.
7087 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007088 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007089reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007090{
7091 int idx = findoption(name);
7092
7093 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007094 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007095 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007096 return OK;
7097 }
7098 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007099}
7100
7101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 * compatible_set() - Called when 'compatible' has been set or unset.
7103 *
7104 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7105 * flag) to a Vi compatible value.
7106 * When 'compatible' is unset: Set all options that have a different default
7107 * for Vim (without the P_VI_DEF flag) to that default.
7108 */
7109 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007110compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111{
7112 int opt_idx;
7113
Bram Moolenaardac13472019-09-16 21:06:21 +02007114 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7116 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7117 set_option_default(opt_idx, OPT_FREE, p_cp);
7118 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007119 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120}
7121
Bram Moolenaardac13472019-09-16 21:06:21 +02007122#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124/*
7125 * fill_breakat_flags() -- called when 'breakat' changes value.
7126 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007127 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007128fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007130 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 int i;
7132
7133 for (i = 0; i < 256; i++)
7134 breakat_flags[i] = FALSE;
7135
7136 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007137 for (p = p_breakat; *p; p++)
7138 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140#endif
7141
7142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 * Check if backspacing over something is allowed.
7144 */
7145 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007146can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007147 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007149#ifdef FEAT_JOB_CHANNEL
7150 if (what == BS_START && bt_prompt(curbuf))
7151 return FALSE;
7152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 switch (*p_bs)
7154 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007155 case '3': return TRUE;
7156 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 case '1': return (what != BS_START);
7158 case '0': return FALSE;
7159 }
7160 return vim_strchr(p_bs, what) != NULL;
7161}
7162
7163/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007164 * Return the effective 'scrolloff' value for the current window, using the
7165 * global value when appropriate.
7166 */
7167 long
7168get_scrolloff_value(void)
7169{
7170 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7171}
7172
7173/*
7174 * Return the effective 'sidescrolloff' value for the current window, using the
7175 * global value when appropriate.
7176 */
7177 long
7178get_sidescrolloff_value(void)
7179{
7180 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7181}
7182
7183/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007184 * Get the local or global value of 'backupcopy'.
7185 */
7186 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007187get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007188{
7189 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7190}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007191
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007192#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007193/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007194 * Get the local or global value of 'formatlistpat'.
7195 */
7196 char_u *
7197get_flp_value(buf_T *buf)
7198{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007199 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7200 return p_flp;
7201 return buf->b_p_flp;
7202}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007203#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007204
7205/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007206 * Get the local or global value of the 'virtualedit' flags.
7207 */
7208 unsigned int
7209get_ve_flags(void)
7210{
Gary Johnson51ad8502021-08-03 18:33:08 +02007211 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7212 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007213}
7214
Bram Moolenaaree857022019-11-09 23:26:40 +01007215#if defined(FEAT_LINEBREAK) || defined(PROTO)
7216/*
7217 * Get the local or global value of 'showbreak'.
7218 */
7219 char_u *
7220get_showbreak_value(win_T *win)
7221{
7222 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7223 return p_sbr;
7224 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7225 return empty_option;
7226 return win->w_p_sbr;
7227}
7228#endif
7229
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007230#if defined(FEAT_EVAL) || defined(PROTO)
7231/*
7232 * Get window or buffer local options.
7233 */
7234 dict_T *
7235get_winbuf_options(int bufopt)
7236{
7237 dict_T *d;
7238 int opt_idx;
7239
7240 d = dict_alloc();
7241 if (d == NULL)
7242 return NULL;
7243
Bram Moolenaardac13472019-09-16 21:06:21 +02007244 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007245 {
7246 struct vimoption *opt = &options[opt_idx];
7247
7248 if ((bufopt && (opt->indir & PV_BUF))
7249 || (!bufopt && (opt->indir & PV_WIN)))
7250 {
7251 char_u *varp = get_varp(opt);
7252
7253 if (varp != NULL)
7254 {
7255 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007256 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007257 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007258 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007259 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007260 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007261 }
7262 }
7263 }
7264
7265 return d;
7266}
7267#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007268
Bram Moolenaardac13472019-09-16 21:06:21 +02007269#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007270/*
7271 * This is called when 'culopt' is changed
7272 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007273 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007274fill_culopt_flags(char_u *val, win_T *wp)
7275{
7276 char_u *p;
7277 char_u culopt_flags_new = 0;
7278
7279 if (val == NULL)
7280 p = wp->w_p_culopt;
7281 else
7282 p = val;
7283 while (*p != NUL)
7284 {
7285 if (STRNCMP(p, "line", 4) == 0)
7286 {
7287 p += 4;
7288 culopt_flags_new |= CULOPT_LINE;
7289 }
7290 else if (STRNCMP(p, "both", 4) == 0)
7291 {
7292 p += 4;
7293 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7294 }
7295 else if (STRNCMP(p, "number", 6) == 0)
7296 {
7297 p += 6;
7298 culopt_flags_new |= CULOPT_NBR;
7299 }
7300 else if (STRNCMP(p, "screenline", 10) == 0)
7301 {
7302 p += 10;
7303 culopt_flags_new |= CULOPT_SCRLINE;
7304 }
7305
7306 if (*p != ',' && *p != NUL)
7307 return FAIL;
7308 if (*p == ',')
7309 ++p;
7310 }
7311
7312 // Can't have both "line" and "screenline".
7313 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7314 return FAIL;
7315 wp->w_p_culopt_flags = culopt_flags_new;
7316
7317 return OK;
7318}
7319#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007320
7321/*
7322 * Get the value of 'magic' adjusted for Vim9 script.
7323 */
7324 int
7325magic_isset(void)
7326{
7327 switch (magic_overruled)
7328 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007329 case OPTION_MAGIC_ON: return TRUE;
7330 case OPTION_MAGIC_OFF: return FALSE;
7331 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007332 }
7333#ifdef FEAT_EVAL
7334 if (in_vim9script())
7335 return TRUE;
7336#endif
7337 return p_magic;
7338}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007339
7340/*
7341 * Set the callback function value for an option that accepts a function name,
7342 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7343 * Returns OK if the option is successfully set to a function, otherwise
7344 * returns FAIL.
7345 */
7346 int
7347option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7348{
7349#ifdef FEAT_EVAL
7350 typval_T *tv;
7351 callback_T cb;
7352
7353 if (optval == NULL || *optval == NUL)
7354 {
7355 free_callback(optcb);
7356 return OK;
7357 }
7358
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007359 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007360 || (STRNCMP(optval, "function(", 9) == 0)
7361 || (STRNCMP(optval, "funcref(", 8) == 0))
7362 // Lambda expression or a funcref
7363 tv = eval_expr(optval, NULL);
7364 else
7365 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007366 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007367 if (tv == NULL)
7368 return FAIL;
7369
7370 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007371 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007372 {
7373 free_tv(tv);
7374 return FAIL;
7375 }
7376
7377 free_callback(optcb);
7378 set_callback(optcb, &cb);
7379 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007380
7381 // when using Vim9 style "import.funcname" it needs to be expanded to
7382 // "import#funcname".
7383 expand_autload_callback(optcb);
7384
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007385 return OK;
7386#else
7387 return FAIL;
7388#endif
7389}