blob: 2acc0d475ffc6f26ca2b6f4f4aca4291e498f55c [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:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000013 * - Put it in the options array in optiondefs.h (copy an existing entry).
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000016 * - Add a PV_XX macro definition to the optiondefs.h file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000019 * - For a window string option, add code to check_win_options() and
20 * clear_winopt().
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 * - For a buffer option, add some code to buf_copy_options().
22 * - For a buffer string option, add code to check_buf_options().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000023 * - If it's a numeric option, add any necessary bounds checks to
24 * set_num_option().
25 * - If it's a list of flags, add some code in did_set_string_option(), search
26 * for WW_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 * - When adding an option with expansion (P_EXPAND), but with a different
28 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020029 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * options.txt, and any other related places.
31 * - Add an entry in runtime/optwin.vim.
32 * When making changes:
33 * - Adjust the help for the option in doc/option.txt.
34 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
35 * comment at the help for the 'compatible' option.
36 */
37
38#define IN_OPTION_C
39#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020040#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010043static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static char_u *option_expand(int opt_idx, char_u *val);
46static void didset_options(void);
47static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000050#else
51# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
52#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010053static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
54static 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 +020055static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020057static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010059static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
61static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020062static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000063static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000066static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void check_winopt(winopt_T *wop);
68static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Christian Brabandt757593c2023-08-22 21:44:10 +020071#if defined(FEAT_EVAL) || defined(PROTO)
72static char *(p_bin_dep_opts[]) = {"textwidth", "wrapmargin", "modeline", "expandtab", NULL};
73static char *(p_paste_dep_opts[]) = {"autoindent", "expandtab", "ruler", "showmatch", "smarttab",
74 "softtabstop", "textwidth", "wrapmargin",
75#ifdef FEAT_RIGHTLEFT
76 "hkmap", "revins",
77#endif
78#ifdef FEAT_VARTABS
79 "varsofttabstop",
80#endif
81 NULL};
82static void didset_options_sctx(int opt_flags, char **buf);
83#endif
84
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086/*
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000087 * Initialize the 'shell' option to a default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000089 static void
90set_init_default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000091{
92 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000094 // Find default value for 'shell' option.
95 // Don't use it if it is empty.
Bram Moolenaar7c626922005-02-07 22:01:03 +000096 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010097#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +000098 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010099 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200102#if defined(MSWIN)
103 {
104 // For MS-Windows put the path in quotes instead of escaping spaces.
105 char_u *cmd;
106 size_t len;
107
108 if (vim_strchr(p, ' ') != NULL)
109 {
110 len = STRLEN(p) + 3; // two quotes and a trailing NUL
111 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200112 if (cmd != NULL)
113 {
114 vim_snprintf((char *)cmd, len, "\"%s\"", p);
115 set_string_default("sh", cmd);
116 vim_free(cmd);
117 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200118 }
119 else
120 set_string_default("sh", p);
121 }
122#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100123 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200124#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000125}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000127/*
128 * Set the default for 'backupskip' to include environment variables for
129 * temp files.
130 */
131 static void
132set_init_default_backupskip(void)
133{
134 int opt_idx;
135 long_u n;
136 char_u *p;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100137#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000138 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100139#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000140 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100141#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000142 int len;
143 garray_T ga;
Bram Moolenaar14338022023-03-15 22:05:44 +0000144 char_u *item;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200145
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000146 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000148 ga_init2(&ga, 1, 100);
149 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
150 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000151 int mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100152#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000153 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100154# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000155 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100156# else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000157 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000159 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100160#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000161 p = vim_getenv((char_u *)names[n], &mustfree);
162 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000164 // First time count the NUL, otherwise count the ','.
165 len = (int)STRLEN(p) + 3;
166 item = alloc(len);
Bram Moolenaar14338022023-03-15 22:05:44 +0000167 if (item != NULL)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000168 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000169 STRCPY(item, p);
170 add_pathsep(item);
171 STRCAT(item, "*");
172 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
173 == NULL
174 && ga_grow(&ga, len) == OK)
175 {
176 if (ga.ga_len > 0)
177 STRCAT(ga.ga_data, ",");
178 STRCAT(ga.ga_data, item);
179 ga.ga_len += len;
180 }
181 vim_free(item);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000184 if (mustfree)
185 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000187 if (ga.ga_data != NULL)
188 {
189 set_string_default("bsk", ga.ga_data);
190 vim_free(ga.ga_data);
191 }
192}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000194/*
195 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
196 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
197 */
198 static void
199set_init_default_maxmemtot(void)
200{
201 int opt_idx;
202 long_u n;
203
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000205 if (opt_idx < 0)
206 return;
207
208#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
209 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
ichizok02560422022-04-05 14:18:44 +0100212#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000213 // Use amount of memory available at this moment.
214 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100215#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000216 // Use amount of memory available to Vim.
217 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100218#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000219 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000221 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
222 opt_idx = findoption((char_u *)"maxmem");
223 if (opt_idx >= 0)
224 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000225#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000226 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
227 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000228#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000229 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000232}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000234/*
235 * Initialize the 'cdpath' option to a default value.
236 */
237 static void
238set_init_default_cdpath(void)
239{
240 int opt_idx;
241 char_u *cdpath;
242 char_u *buf;
243 int i;
244 int j;
245 int mustfree = FALSE;
246
247 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
248 if (cdpath == NULL)
249 return;
250
251 buf = alloc((STRLEN(cdpath) << 1) + 2);
252 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000254 buf[0] = ','; // start with ",", current dir first
255 j = 1;
256 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000258 if (vim_ispathlistsep(cdpath[i]))
259 buf[j++] = ',';
260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000262 if (cdpath[i] == ' ' || cdpath[i] == ',')
263 buf[j++] = '\\';
264 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000267 buf[j] = NUL;
268 opt_idx = findoption((char_u *)"cdpath");
269 if (opt_idx >= 0)
270 {
271 options[opt_idx].def_val[VI_DEFAULT] = buf;
272 options[opt_idx].flags |= P_DEF_ALLOCED;
273 }
274 else
275 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000277 if (mustfree)
278 vim_free(cdpath);
279}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000281/*
282 * Initialize the 'printencoding' option to a default value.
283 */
284 static void
285set_init_default_printencoding(void)
286{
ichizok02560422022-04-05 14:18:44 +0100287#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000288 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100289 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100291# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000292 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100293# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000294 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100295# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000296 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100297# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000298 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000300 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000302}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
304#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000305/*
306 * Initialize the 'printexpr' option to a default value.
307 */
308 static void
309set_init_default_printexpr(void)
310{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100311 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100313# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000314 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100315# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
317
ichizok02560422022-04-05 14:18:44 +0100318# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# endif
321 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000322}
323#endif
324
325#ifdef UNIX
326/*
327 * Force restricted-mode on for "nologin" or "false" $SHELL
328 */
329 static void
330set_init_restricted_mode(void)
331{
332 char_u *p;
333
334 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000335 if (p == NULL)
336 return;
337 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
338 restricted = TRUE;
339 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000340}
341#endif
342
343#ifdef CLEAN_RUNTIMEPATH
344/*
345 * When Vim is started with the "--clean" argument, set the default value
346 * for the 'runtimepath' and 'packpath' options.
347 */
348 static void
349set_init_clean_rtp(void)
350{
351 int opt_idx;
352
353 opt_idx = findoption((char_u *)"runtimepath");
354 if (opt_idx >= 0)
355 {
356 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
357 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
358 }
359 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000360 if (opt_idx < 0)
361 return;
362 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
363 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000364}
365#endif
366
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200367#ifdef UNIX
368/*
369 * Change 'runtimepath' and 'packdir' to '$XDG_CONFIG_HOME/vim' if the only
370 * vimrc found is located in '$XDG_CONFIG_HOME/vim/vimrc'.
371 * In case the '$XDG_CONFIG_HOME' variable is not set, '$HOME/.config' is used
372 * as a fallback as is defined in the XDG base dir specification:
373 * <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html>
374 */
375 static void
376set_init_xdg_rtp(void)
377{
378 int opt_idx;
379 int has_xdg_env = TRUE;
380 int should_free_xdg_dir = FALSE;
381 char_u *vimrc1 = NULL;
382 char_u *vimrc2 = NULL;
383 char_u *xdg_dir = NULL;
384 char_u *xdg_rtp = NULL;
385 char_u *vimrc_xdg = NULL;
386
Christian Brabandtb09fa352024-04-21 14:52:20 +0200387 // initialize chartab, so we can expand $HOME
388 (void)init_chartab();
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200389 vimrc1 = expand_env_save((char_u *)USR_VIMRC_FILE);
390 vimrc2 = expand_env_save((char_u *)USR_VIMRC_FILE2);
391
392 xdg_dir = mch_getenv("XDG_CONFIG_HOME");
393 if (!xdg_dir)
394 {
395 xdg_dir = expand_env_save((char_u *)"~/.config");
396 should_free_xdg_dir = TRUE;
397 has_xdg_env = FALSE;
398 }
399 vimrc_xdg = concat_fnames(xdg_dir, (char_u *)"vim/vimrc", TRUE);
400
401 if (file_is_readable(vimrc1) || file_is_readable(vimrc2) ||
402 !file_is_readable(vimrc_xdg))
403 goto theend;
404
405 xdg_rtp = has_xdg_env ? (char_u *)XDG_RUNTIMEPATH
406 : (char_u *)XDG_RUNTIMEPATH_FB;
407
408 if ((opt_idx = findoption((char_u *)"runtimepath")) < 0)
409 goto theend;
410
411 options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
412 p_rtp = xdg_rtp;
413
414 if ((opt_idx = findoption((char_u *)"packpath")) < 0)
415 goto theend;
416
417 options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
418 p_pp = xdg_rtp;
419
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200420#if defined(XDG_VDIR) && defined(FEAT_SESSION)
421 if ((opt_idx = findoption((char_u *)"viewdir")) < 0)
422 goto theend;
423
424 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)XDG_VDIR;
425 p_vdir = (char_u *)XDG_VDIR;
426#endif
427
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200428theend:
429 vim_free(vimrc1);
430 vim_free(vimrc2);
431 vim_free(vimrc_xdg);
432 if (should_free_xdg_dir)
433 vim_free(xdg_dir);
434}
435#endif
436
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000437/*
438 * Expand environment variables and things like "~" for the defaults.
439 * If option_expand() returns non-NULL the variable is expanded. This can
440 * only happen for non-indirect options.
441 * Also set the default to the expanded value, so ":set" does not list
442 * them.
443 * Don't set the P_ALLOCED flag, because we don't want to free the
444 * default.
445 */
446 static void
447set_init_expand_env(void)
448{
449 int opt_idx;
450 char_u *p;
451
452 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
453 {
454 if ((options[opt_idx].flags & P_GETTEXT)
455 && options[opt_idx].var != NULL)
456 p = (char_u *)_(*(char **)options[opt_idx].var);
457 else
458 p = option_expand(opt_idx, NULL);
459 if (p != NULL && (p = vim_strsave(p)) != NULL)
460 {
461 *(char_u **)options[opt_idx].var = p;
462 // VIMEXP
463 // Defaults for all expanded options are currently the same for Vi
464 // and Vim. When this changes, add some code here! Also need to
465 // split P_DEF_ALLOCED in two.
466 if (options[opt_idx].flags & P_DEF_ALLOCED)
467 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
468 options[opt_idx].def_val[VI_DEFAULT] = p;
469 options[opt_idx].flags |= P_DEF_ALLOCED;
470 }
471 }
472}
473
474/*
475 * Initialize the 'LANG' environment variable to a default value.
476 */
477 static void
478set_init_lang_env(void)
479{
480#if defined(MSWIN) && defined(FEAT_GETTEXT)
481 // If $LANG isn't set, try to get a good value for it. This makes the
482 // right language be used automatically. Don't do this for English.
483 if (mch_getenv((char_u *)"LANG") == NULL)
484 {
485 char buf[20];
486 long_u n;
487
488 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
489 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
490 // only the first two.
491 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
492 (LPTSTR)buf, 20);
493 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
494 {
495 // There are a few exceptions (probably more)
496 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
497 STRCPY(buf, "zh_TW");
498 else if (STRNICMP(buf, "chs", 3) == 0
499 || STRNICMP(buf, "zhc", 3) == 0)
500 STRCPY(buf, "zh_CN");
501 else if (STRNICMP(buf, "jp", 2) == 0)
502 STRCPY(buf, "ja");
503 else
504 buf[2] = NUL; // truncate to two-letter code
505 vim_setenv((char_u *)"LANG", (char_u *)buf);
506 }
507 }
508#elif defined(MACOS_CONVERT)
509 // Moved to os_mac_conv.c to avoid dependency problems.
510 mac_lang_init();
511#endif
512}
513
514/*
515 * Initialize the 'encoding' option to a default value.
516 */
517 static void
518set_init_default_encoding(void)
519{
520 char_u *p;
521 int opt_idx;
522
Igor Todorovski497e5282024-01-12 17:59:18 +0100523# if defined(MSWIN) || defined(__MVS__)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000524 // MS-Windows has builtin support for conversion to and from Unicode, using
525 // "utf-8" for 'encoding' should work best for most users.
Igor Todorovski497e5282024-01-12 17:59:18 +0100526 // z/OS built should default to UTF-8 mode as setlocale does not respect utf-8 environment variable locales
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000527 p = vim_strsave((char_u *)ENC_DFLT);
528# else
529 // enc_locale() will try to find the encoding of the current locale.
530 // This works best for properly configured systems, old and new.
531 p = enc_locale();
532# endif
533 if (p == NULL)
534 return;
535
536 // Try setting 'encoding' and check if the value is valid.
537 // If not, go back to the default encoding.
538 char_u *save_enc = p_enc;
539 p_enc = p;
540 if (STRCMP(p_enc, "gb18030") == 0)
541 {
542 // We don't support "gb18030", but "cp936" is a good substitute
543 // for practical purposes, thus use that. It's not an alias to
544 // still support conversion between gb18030 and utf-8.
545 p_enc = vim_strsave((char_u *)"cp936");
546 vim_free(p);
547 }
548 if (mb_init() == NULL)
549 {
550 opt_idx = findoption((char_u *)"encoding");
551 if (opt_idx >= 0)
552 {
553 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
554 options[opt_idx].flags |= P_DEF_ALLOCED;
555 }
556
557#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
558 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
559 {
560 // Adjust the default for 'isprint' and 'iskeyword' to match
561 // latin1. Also set the defaults for when 'nocompatible' is
562 // set.
563 set_string_option_direct((char_u *)"isp", -1,
564 ISP_LATIN1, OPT_FREE, SID_NONE);
565 set_string_option_direct((char_u *)"isk", -1,
566 ISK_LATIN1, OPT_FREE, SID_NONE);
567 opt_idx = findoption((char_u *)"isp");
568 if (opt_idx >= 0)
569 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
570 opt_idx = findoption((char_u *)"isk");
571 if (opt_idx >= 0)
572 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
573 (void)init_chartab();
574 }
575#endif
576
577#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
578 // Win32 console: When GetACP() returns a different value from
579 // GetConsoleCP() set 'termencoding'.
580 if (
581# ifdef VIMDLL
582 (!gui.in_use && !gui.starting) &&
583# endif
584 GetACP() != GetConsoleCP())
585 {
586 char buf[50];
587
588 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
589 // Use an alternative value.
590 if (GetConsoleCP() == 0)
591 sprintf(buf, "cp%ld", (long)GetACP());
592 else
593 sprintf(buf, "cp%ld", (long)GetConsoleCP());
594 p_tenc = vim_strsave((char_u *)buf);
595 if (p_tenc != NULL)
596 {
597 opt_idx = findoption((char_u *)"termencoding");
598 if (opt_idx >= 0)
599 {
600 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
601 options[opt_idx].flags |= P_DEF_ALLOCED;
602 }
603 convert_setup(&input_conv, p_tenc, p_enc);
604 convert_setup(&output_conv, p_enc, p_tenc);
605 }
606 else
607 p_tenc = empty_option;
608 }
609#endif
610#if defined(MSWIN)
611 // $HOME may have characters in active code page.
612 init_homedir();
613#endif
614 }
615 else
616 {
617 vim_free(p_enc);
618 p_enc = save_enc;
619 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000620}
621
622/*
623 * Initialize the options, first part.
624 *
625 * Called only once from main(), just after creating the first buffer.
626 * If "clean_arg" is TRUE Vim was started with --clean.
627 */
628 void
629set_init_1(int clean_arg)
630{
631#ifdef FEAT_LANGMAP
632 langmap_init();
633#endif
634
635 // Be Vi compatible by default
636 p_cp = TRUE;
637
638 // Use POSIX compatibility when $VIM_POSIX is set.
639 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
640 {
641 set_string_default("cpo", (char_u *)CPO_ALL);
642 set_string_default("shm", (char_u *)SHM_POSIX);
643 }
644
645 set_init_default_shell();
646 set_init_default_backupskip();
647 set_init_default_maxmemtot();
648 set_init_default_cdpath();
649 set_init_default_printencoding();
650#ifdef FEAT_POSTSCRIPT
651 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652#endif
653
654 /*
655 * Set all the options (except the terminal options) to their default
656 * value. Also set the global value for local options.
657 */
658 set_options_default(0);
659
matveytadbb1bf2022-02-01 17:26:12 +0000660#ifdef UNIX
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200661 set_init_xdg_rtp();
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000662 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000663#endif
664
Bram Moolenaar07268702018-03-01 21:57:32 +0100665#ifdef CLEAN_RUNTIMEPATH
666 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000667 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100668#endif
669
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670#ifdef FEAT_GUI
671 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100672 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673#endif
674
675 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100676 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100677 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 check_buf_options(curbuf);
679 check_win_options(curwin);
680 check_options();
681
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100682 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 didset_options();
684
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000685#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100686 // Use the current chartab for the generic chartab. This is not in
687 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000688 init_spell_chartab();
689#endif
690
K.Takatace3189d2023-02-15 19:13:43 +0000691 set_init_default_encoding();
692
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000693 // Expand environment variables and things like "~" for the defaults.
694 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100696 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100699 // Detect use of mlterm.
700 // Mlterm is a terminal emulator akin to xterm that has some special
701 // abilities (bidi namely).
702 // NOTE: mlterm's author is being asked to 'set' a variable
703 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100705 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#endif
707
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200708 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000710 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
712#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100713 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 set_helplang_default(get_mess_lang());
715#endif
716}
717
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200718static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
719
720/*
721 * Set the "fileencodings" option to the default value for when 'encoding' is
722 * utf-8.
723 */
724 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000725set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200726{
727 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
728 OPT_FREE, 0);
729}
730
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731/*
732 * Set an option to its default value.
733 * This does not take care of side effects!
734 */
735 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100736set_option_default(
737 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100738 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
739 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100741 char_u *varp; // pointer to variable for current option
742 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000744 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
746
747 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
748 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100749 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {
751 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
752 if (flags & P_STRING)
753 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200754 // 'fencs' default value depends on 'encoding'
755 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
756 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100757 // Use set_string_option_direct() for local options to handle
758 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200759 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200760 set_string_option_direct(NULL, opt_idx,
761 options[opt_idx].def_val[dvi], opt_flags, 0);
762 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200764 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
765 free_string_option(*(char_u **)(varp));
766 *(char_u **)varp = options[opt_idx].def_val[dvi];
767 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 }
769 }
770 else if (flags & P_NUM)
771 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000772 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 win_comp_scroll(curwin);
774 else
775 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100776 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
777
778 if ((long *)varp == &curwin->w_p_so
779 || (long *)varp == &curwin->w_p_siso)
780 // 'scrolloff' and 'sidescrolloff' local values have a
781 // different default value than the global default.
782 *(long *)varp = -1;
783 else
784 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100785 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 if (both)
787 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100788 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 }
790 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100791 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100793 // the cast to long is required for Manx C, long_i is needed for
794 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000795 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000796#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100797 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000798 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
799 *(int *)varp = FALSE;
800#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100801 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (both)
803 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
804 *(int *)varp;
805 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000806
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100807 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000808 flagsp = insecure_flag(opt_idx, opt_flags);
809 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
811
812#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200813 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814#endif
815}
816
817/*
818 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200819 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 */
821 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100822set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100823 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
825 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000827 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
Bram Moolenaardac13472019-09-16 21:06:21 +0200829 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200830 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200831 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100832 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200833# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200834 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200835 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200836# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100837 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 set_option_default(i, opt_flags, p_cp);
839
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100840 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000841 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200843 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844}
845
846/*
847 * Set the Vi-default value of a string option.
848 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100849 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100851 static void
852set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
854 char_u *p;
855 int opt_idx;
856
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100857 if (escape && vim_strchr(val, ' ') != NULL)
858 p = vim_strsave_escaped(val, (char_u *)" ");
859 else
860 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000861 if (p == NULL) // we don't want a NULL
862 return;
863
864 opt_idx = findoption((char_u *)name);
865 if (opt_idx < 0)
Christian Brabandt29269a72024-04-16 22:44:31 +0200866 {
867 vim_free(p);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000868 return;
Christian Brabandt29269a72024-04-16 22:44:31 +0200869 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000870
871 if (options[opt_idx].flags & P_DEF_ALLOCED)
872 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
873 options[opt_idx].def_val[VI_DEFAULT] = p;
874 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875}
876
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100877 void
878set_string_default(char *name, char_u *val)
879{
880 set_string_default_esc(name, val, FALSE);
881}
882
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200884 * For an option value that contains comma separated items, find "newval" in
885 * "origval". Return NULL if not found.
886 */
887 static char_u *
888find_dup_item(char_u *origval, char_u *newval, long_u flags)
889{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200890 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200891 size_t newlen;
892 char_u *s;
893
894 if (origval == NULL)
895 return NULL;
896
897 newlen = STRLEN(newval);
898 for (s = origval; *s != NUL; ++s)
899 {
900 if ((!(flags & P_COMMA)
901 || s == origval
902 || (s[-1] == ',' && !(bs & 1)))
903 && STRNCMP(s, newval, newlen) == 0
904 && (!(flags & P_COMMA)
905 || s[newlen] == ','
906 || s[newlen] == NUL))
907 return s;
908 // Count backslashes. Only a comma with an even number of backslashes
909 // or a single backslash preceded by a comma before it is recognized as
910 // a separator.
911 if ((s > origval + 1
912 && s[-1] == '\\'
913 && s[-2] != ',')
914 || (s == origval + 1
915 && s[-1] == '\\'))
916 ++bs;
917 else
918 bs = 0;
919 }
920 return NULL;
921}
922
923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 * Set the Vi-default value of a number option.
925 * Used for 'lines' and 'columns'.
926 */
927 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100928set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000930 int opt_idx;
931
932 opt_idx = findoption((char_u *)name);
933 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000934 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935}
936
Dominique Pelle748b3082022-01-08 12:41:16 +0000937#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200938/*
939 * Set all window-local and buffer-local options to the Vim default.
940 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200941 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200942 */
943 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200944set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200945{
946 win_T *save_curwin = curwin;
947 int i;
948
949 curwin = wp;
950 curbuf = curwin->w_buffer;
951 block_autocmds();
952
Bram Moolenaardac13472019-09-16 21:06:21 +0200953 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200954 {
955 struct vimoption *p = &(options[i]);
956 char_u *varp = get_varp_scope(p, OPT_LOCAL);
957
958 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200959 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200960 && !(options[i].flags & P_NODEFAULT)
961 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200962 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200963 }
964
965 unblock_autocmds();
966 curwin = save_curwin;
967 curbuf = curwin->w_buffer;
968}
Dominique Pelle748b3082022-01-08 12:41:16 +0000969#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200970
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000971#if defined(EXITFREE) || defined(PROTO)
972/*
973 * Free all options.
974 */
975 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100976free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000977{
978 int i;
979
Bram Moolenaardac13472019-09-16 21:06:21 +0200980 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000981 {
982 if (options[i].indir == PV_NONE)
983 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100984 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100985 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000986 free_string_option(*(char_u **)options[i].var);
987 if (options[i].flags & P_DEF_ALLOCED)
988 free_string_option(options[i].def_val[VI_DEFAULT]);
989 }
990 else if (options[i].var != VAR_WIN
991 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100992 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200993 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000994 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000995 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000996 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000997}
998#endif
999
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001/*
1002 * Initialize the options, part two: After getting Rows and Columns and
1003 * setting 'term'.
1004 */
1005 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001006set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001008 int idx;
1009
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001010 // 'scroll' defaults to half the window height. The stored default is zero,
1011 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001012 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001013 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001014 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 comp_col();
1016
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001017 // 'window' is only for backwards compatibility with Vi.
1018 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +00001019 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001020 p_window = Rows - 1;
1021 set_number_default("window", Rows - 1);
1022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001023 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +01001024#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001025 // If 'background' wasn't set by the user, try guessing the value,
1026 // depending on the terminal name. Only need to check for terminals
1027 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001028 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001029 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
1030 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001032 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001033 // don't mark it as set, when starting the GUI it may be
1034 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +00001035 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 }
1037#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001038
1039#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001040 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +00001041#endif
1042#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001043 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +00001044#endif
1045#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001046 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +00001047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048}
1049
1050/*
1051 * Initialize the options, part three: After reading the .vimrc
1052 */
1053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001054set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
Bram Moolenaar4f974752019-02-17 17:44:42 +01001056#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057/*
1058 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
1059 * This is done after other initializations, where 'shell' might have been
1060 * set, but only if they have not been set before.
1061 */
1062 char_u *p;
1063 int idx_srr;
1064 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001065# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 int idx_sp;
1067 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001068# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
1070 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001071 if (idx_srr < 0)
1072 do_srr = FALSE;
1073 else
1074 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001075# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001077 if (idx_sp < 0)
1078 do_sp = FALSE;
1079 else
1080 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001081# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001082 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (p != NULL)
1084 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001085 // Default for p_sp is "| tee", for p_srr is ">".
1086 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if ( fnamecmp(p, "csh") == 0
1088 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001089# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 || fnamecmp(p, "csh.exe") == 0
1091 || fnamecmp(p, "tcsh.exe") == 0
1092# endif
1093 )
1094 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001095# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 if (do_sp)
1097 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001098# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001100# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1104 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001105# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (do_srr)
1107 {
1108 p_srr = (char_u *)">&";
1109 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1110 }
1111 }
Mike Williams12795022021-06-28 20:53:58 +02001112# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001113 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1114 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001115 else if ( fnamecmp(p, "powershell") == 0
1116 || fnamecmp(p, "powershell.exe") == 0
1117 )
1118 {
1119# if defined(FEAT_QUICKFIX)
1120 if (do_sp)
1121 {
1122 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1123 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1124 }
1125# endif
1126 if (do_srr)
1127 {
1128 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1129 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1130 }
1131 }
1132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 else
Natanael Copa56318362021-05-06 18:46:35 +02001134 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 if ( fnamecmp(p, "sh") == 0
1136 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001137 || fnamecmp(p, "mksh") == 0
1138 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001140 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001142 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001143 || fnamecmp(p, "ash") == 0
1144 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001145 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001146# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 || fnamecmp(p, "cmd") == 0
1148 || fnamecmp(p, "sh.exe") == 0
1149 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001150 || fnamecmp(p, "mksh.exe") == 0
1151 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001153 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 || fnamecmp(p, "bash.exe") == 0
1155 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001156 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001157 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001159 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001161# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 if (do_sp)
1163 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001164# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001166# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001167 if (fnamecmp(p, "pwsh") == 0)
1168 p_sp = (char_u *)">%s 2>&1";
1169 else
1170 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001171# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1173 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (do_srr)
1176 {
1177 p_srr = (char_u *)">%s 2>&1";
1178 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1179 }
1180 }
1181 vim_free(p);
1182 }
1183#endif
1184
Bram Moolenaar4f974752019-02-17 17:44:42 +01001185#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001187 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1188 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001190 * set, but only if they have not been set before.
1191 * Default values depend on shell (cmd.exe is default shell):
1192 *
1193 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001194 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001195 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001196 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001197 * "sh" like shells - "-c" "\""
1198 *
1199 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 */
Mike Williams12795022021-06-28 20:53:58 +02001201 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1202 {
1203 int idx_opt;
1204
1205 idx_opt = findoption((char_u *)"shcf");
1206 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1207 {
1208 p_shcf = (char_u*)"-Command";
1209 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1210 }
1211
1212 idx_opt = findoption((char_u *)"sxq");
1213 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1214 {
1215 p_sxq = (char_u*)"\"";
1216 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1217 }
1218 }
1219 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 {
1221 int idx3;
1222
1223 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001224 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
1226 p_shcf = (char_u *)"-c";
1227 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1228 }
1229
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001230 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001232 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 {
1234 p_sxq = (char_u *)"\"";
1235 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001238 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1239 {
1240 int idx3;
1241
1242 /*
1243 * cmd.exe on Windows will strip the first and last double quote given
1244 * on the command line, e.g. most of the time things like:
1245 * cmd /c "my path/to/echo" "my args to echo"
1246 * become:
1247 * my path/to/echo" "my args to echo
1248 * when executed.
1249 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001250 * To avoid this, set shellxquote to surround the command in
1251 * parenthesis. This appears to make most commands work, without
1252 * breaking commands that worked previously, such as
1253 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001254 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001255 idx3 = findoption((char_u *)"sxq");
1256 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1257 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001258 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001259 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1260 }
1261
Bram Moolenaara64ba222012-02-12 23:23:31 +01001262 idx3 = findoption((char_u *)"shcf");
1263 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1264 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001265 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001266 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1267 }
1268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269#endif
1270
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001271 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001272 {
1273 int idx_ffs = findoption((char_u *)"ffs");
1274
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001275 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001276 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1277 set_fileformat(default_fileformat(), OPT_LOCAL);
1278 }
1279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281}
1282
1283#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1284/*
1285 * When 'helplang' is still at its default value, set it to "lang".
1286 * Only the first two characters of "lang" are used.
1287 */
1288 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001289set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290{
1291 int idx;
1292
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001293 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 return;
1295 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001296 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1297 return;
1298
1299 if (options[idx].flags & P_ALLOCED)
1300 free_string_option(p_hlg);
1301 p_hlg = vim_strsave(lang);
1302 if (p_hlg == NULL)
1303 p_hlg = empty_option;
1304 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001306 // zh_CN becomes "cn", zh_TW becomes "tw"
1307 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001308 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001309 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1310 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001311 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001312 // any C like setting, such as C.UTF-8, becomes "en"
1313 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1314 {
1315 p_hlg[0] = 'e';
1316 p_hlg[1] = 'n';
1317 }
1318 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001320 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321}
1322#endif
1323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324/*
1325 * 'title' and 'icon' only default to true if they have not been set or reset
1326 * in .vimrc and we can read the old value.
1327 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1328 * they can be reset. This reduces startup time when using X on a remote
1329 * machine.
1330 */
1331 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001332set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
1334 int idx1;
1335 long val;
1336
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001337 // If GUI is (going to be) used, we can always set the window title and
1338 // icon name. Saves a bit of time, because the X11 display server does
1339 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001341 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 {
1343#ifdef FEAT_GUI
1344 if (gui.starting || gui.in_use)
1345 val = TRUE;
1346 else
1347#endif
1348 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001349 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 p_title = val;
1351 }
1352 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001353 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001355 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001357
1358#ifdef FEAT_GUI
1359 if (gui.starting || gui.in_use)
1360 val = TRUE;
1361 else
1362#endif
1363 val = mch_can_restore_icon();
1364 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1365 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001368 void
1369ex_set(exarg_T *eap)
1370{
1371 int flags = 0;
1372
1373 if (eap->cmdidx == CMD_setlocal)
1374 flags = OPT_LOCAL;
1375 else if (eap->cmdidx == CMD_setglobal)
1376 flags = OPT_GLOBAL;
1377#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001378 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001379 ex_options(eap);
1380 else
1381#endif
1382 {
1383 if (eap->forceit)
1384 flags |= OPT_ONECOLUMN;
1385 (void)do_set(eap->arg, flags);
1386 }
1387}
1388
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001389/*
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001390 * :set boolean option prefix
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001391 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001392typedef enum {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001393 PREFIX_NO = 0, // "no" prefix
1394 PREFIX_NONE, // no prefix
1395 PREFIX_INV, // "inv" prefix
1396} set_prefix_T;
1397
1398/*
1399 * Return the prefix type for the option name in *argp.
1400 */
1401 static set_prefix_T
1402get_option_prefix(char_u **argp)
1403{
1404 int prefix = PREFIX_NONE;
1405 char_u *arg = *argp;
1406
1407 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1408 {
1409 prefix = PREFIX_NO;
1410 arg += 2;
1411 }
1412 else if (STRNCMP(arg, "inv", 3) == 0)
1413 {
1414 prefix = PREFIX_INV;
1415 arg += 3;
1416 }
1417
1418 *argp = arg;
1419 return prefix;
1420}
1421
1422/*
1423 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1424 * and the option name length in "*lenp". For a <t_xx> option, return the key
1425 * number in "*keyp".
1426 *
1427 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1428 * otherwise returns OK.
1429 */
1430 static int
1431parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1432{
1433 int key = 0;
1434 int len;
1435 int opt_idx;
1436
1437 if (*arg == '<')
1438 {
1439 opt_idx = -1;
1440 // look out for <t_>;>
1441 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1442 len = 5;
1443 else
1444 {
1445 len = 1;
1446 while (arg[len] != NUL && arg[len] != '>')
1447 ++len;
1448 }
1449 if (arg[len] != '>')
1450 return FAIL;
1451
1452 arg[len] = NUL; // put NUL after name
1453 if (arg[1] == 't' && arg[2] == '_') // could be term code
1454 opt_idx = findoption(arg + 1);
1455 arg[len++] = '>'; // restore '>'
1456 if (opt_idx == -1)
1457 key = find_key_option(arg + 1, TRUE);
1458 }
1459 else
1460 {
1461 int nextchar; // next non-white char after option name
1462
1463 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001464 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001465 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1466 len = 4;
1467 else
1468 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1469 ++len;
1470 nextchar = arg[len];
1471 arg[len] = NUL; // put NUL after name
1472 opt_idx = findoption(arg);
1473 arg[len] = nextchar; // restore nextchar
1474 if (opt_idx == -1)
1475 key = find_key_option(arg, FALSE);
1476 }
1477
1478 *keyp = key;
1479 *lenp = len;
1480 *opt_idxp = opt_idx;
1481
1482 return OK;
1483}
1484
1485/*
1486 * Get the option operator (+=, ^=, -=).
1487 */
1488 static set_op_T
1489get_opt_op(char_u *arg)
1490{
1491 set_op_T op = OP_NONE;
1492
1493 if (*arg != NUL && *(arg + 1) == '=')
1494 {
1495 if (*arg == '+')
1496 op = OP_ADDING; // "+="
1497 else if (*arg == '^')
1498 op = OP_PREPENDING; // "^="
1499 else if (*arg == '-')
1500 op = OP_REMOVING; // "-="
1501 }
1502
1503 return op;
1504}
1505
1506/*
1507 * Validate whether the value of the option in "opt_idx" can be changed.
1508 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1509 * if it can be changed.
1510 */
1511 static int
1512validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1513{
1514 // Skip all options that are not window-local (used when showing
1515 // an already loaded buffer in a window).
1516 if ((opt_flags & OPT_WINONLY)
1517 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1518 return FAIL;
1519
1520 // Skip all options that are window-local (used for :vimgrep).
1521 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1522 && options[opt_idx].var == VAR_WIN)
1523 return FAIL;
1524
1525 // Disallow changing some options from modelines.
1526 if (opt_flags & OPT_MODELINE)
1527 {
1528 if (flags & (P_SECURE | P_NO_ML))
1529 {
1530 *errmsg = e_not_allowed_in_modeline;
1531 return FAIL;
1532 }
1533 if ((flags & P_MLE) && !p_mle)
1534 {
1535 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1536 return FAIL;
1537 }
1538#ifdef FEAT_DIFF
1539 // In diff mode some options are overruled. This avoids that
1540 // 'foldmethod' becomes "marker" instead of "diff" and that
1541 // "wrap" gets set.
1542 if (curwin->w_p_diff
1543 && opt_idx >= 0 // shut up coverity warning
1544 && (
1545# ifdef FEAT_FOLDING
1546 options[opt_idx].indir == PV_FDM ||
1547# endif
1548 options[opt_idx].indir == PV_WRAP))
1549 return FAIL;
1550#endif
1551 }
1552
1553#ifdef HAVE_SANDBOX
1554 // Disallow changing some options in the sandbox
1555 if (sandbox != 0 && (flags & P_SECURE))
1556 {
1557 *errmsg = e_not_allowed_in_sandbox;
1558 return FAIL;
1559 }
1560#endif
1561
1562 return OK;
1563}
1564
Bram Moolenaar47403942022-09-21 21:12:53 +01001565/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001566 * Get the Vim/Vi default value for a string option.
1567 */
1568 static char_u *
1569stropt_get_default_val(
1570 int opt_idx,
1571 char_u *varp,
1572 int flags,
1573 int cp_val)
1574{
1575 char_u *newval;
1576
1577 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1578 ? VI_DEFAULT : VIM_DEFAULT];
1579 if ((char_u **)varp == &p_bg)
1580 {
1581 // guess the value of 'background'
1582#ifdef FEAT_GUI
1583 if (gui.in_use)
1584 newval = gui_bg_default();
1585 else
1586#endif
1587 newval = term_bg_default();
1588 }
1589 else if ((char_u **)varp == &p_fencs && enc_utf8)
1590 newval = fencs_utf8_default;
1591
1592 // expand environment variables and ~ since the default value was
1593 // already expanded, only required when an environment variable was set
1594 // later
1595 if (newval == NULL)
1596 newval = empty_option;
1597 else
1598 {
1599 char_u *s = option_expand(opt_idx, newval);
1600 if (s == NULL)
1601 s = newval;
1602 newval = vim_strsave(s);
1603 }
1604
1605 return newval;
1606}
1607
1608/*
1609 * Convert the 'backspace' option number value to a string: for adding,
1610 * prepending and removing string.
1611 */
1612 static void
1613opt_backspace_nr2str(
1614 char_u *varp,
1615 char_u **origval_p,
1616 char_u **origval_l_p,
1617 char_u **origval_g_p,
1618 char_u **oldval_p)
1619{
1620 int i = getdigits((char_u **)varp);
1621
1622 switch (i)
1623 {
1624 case 0:
1625 *(char_u **)varp = empty_option;
1626 break;
1627 case 1:
1628 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1629 break;
1630 case 2:
1631 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1632 break;
1633 case 3:
1634 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1635 break;
1636 }
1637 vim_free(*oldval_p);
1638 if (*origval_p == *oldval_p)
1639 *origval_p = *(char_u **)varp;
1640 if (*origval_l_p == *oldval_p)
1641 *origval_l_p = *(char_u **)varp;
1642 if (*origval_g_p == *oldval_p)
1643 *origval_g_p = *(char_u **)varp;
1644 *oldval_p = *(char_u **)varp;
1645}
1646
1647/*
1648 * Convert the 'whichwrap' option number value to a string, for backwards
1649 * compatibility with Vim 3.0.
1650 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1651 */
1652 static char_u *
1653opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1654{
1655 *whichwrap = NUL;
1656 int i = getdigits(argp);
1657 if (i & 1)
1658 STRCAT(whichwrap, "b,");
1659 if (i & 2)
1660 STRCAT(whichwrap, "s,");
1661 if (i & 4)
1662 STRCAT(whichwrap, "h,l,");
1663 if (i & 8)
1664 STRCAT(whichwrap, "<,>,");
1665 if (i & 16)
1666 STRCAT(whichwrap, "[,],");
1667 if (*whichwrap != NUL) // remove trailing ,
1668 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1669
1670 return whichwrap;
1671}
1672
1673/*
1674 * Copy the new string value into allocated memory for the option.
1675 * Can't use set_string_option_direct(), because we need to remove the
1676 * backslashes.
1677 */
1678 static char_u *
1679stropt_copy_value(
1680 char_u *origval,
1681 char_u **argp,
1682 set_op_T op,
1683 int flags UNUSED)
1684{
1685 char_u *arg = *argp;
1686 unsigned newlen;
1687 char_u *newval;
1688 char_u *s = NULL;
1689
1690 // get a bit too much
1691 newlen = (unsigned)STRLEN(arg) + 1;
1692 if (op != OP_NONE)
1693 newlen += (unsigned)STRLEN(origval) + 1;
1694 newval = alloc(newlen);
1695 if (newval == NULL) // out of mem, don't change
1696 return NULL;
1697 s = newval;
1698
1699 // Copy the string, skip over escaped chars.
1700 // For MS-DOS and WIN32 backslashes before normal file name characters
1701 // are not removed, and keep backslash at start, for "\\machine\path",
1702 // but do remove it for "\\\\machine\\path".
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001703 // The reverse is found in escape_option_str_cmdline().
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001704 while (*arg != NUL && !VIM_ISWHITE(*arg))
1705 {
1706 int i;
1707
1708 if (*arg == '\\' && arg[1] != NUL
1709#ifdef BACKSLASH_IN_FILENAME
1710 && !((flags & P_EXPAND)
1711 && vim_isfilec(arg[1])
1712 && !VIM_ISWHITE(arg[1])
1713 && (arg[1] != '\\'
1714 || (s == newval && arg[2] != '\\')))
1715#endif
1716 )
1717 ++arg; // remove backslash
1718 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1719 {
1720 // copy multibyte char
1721 mch_memmove(s, arg, (size_t)i);
1722 arg += i;
1723 s += i;
1724 }
1725 else
1726 *s++ = *arg++;
1727 }
1728 *s = NUL;
1729
1730 *argp = arg;
1731 return newval;
1732}
1733
1734/*
1735 * Expand environment variables and ~ in string option value 'newval'.
1736 */
1737 static char_u *
1738stropt_expand_envvar(
1739 int opt_idx,
1740 char_u *origval,
1741 char_u *newval,
1742 set_op_T op)
1743{
1744 char_u *s = option_expand(opt_idx, newval);
1745 if (s == NULL)
1746 return newval;
1747
1748 vim_free(newval);
1749 unsigned newlen = (unsigned)STRLEN(s) + 1;
1750 if (op != OP_NONE)
1751 newlen += (unsigned)STRLEN(origval) + 1;
1752
1753 newval = alloc(newlen);
1754 if (newval == NULL)
1755 return NULL;
1756
1757 STRCPY(newval, s);
1758
1759 return newval;
1760}
1761
1762/*
1763 * Concatenate the original and new values of a string option, adding a "," if
1764 * needed.
1765 */
1766 static void
1767stropt_concat_with_comma(
1768 char_u *origval,
1769 char_u *newval,
1770 set_op_T op,
1771 int flags)
1772{
1773 int len = 0;
1774
1775 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1776 if (op == OP_ADDING)
1777 {
1778 len = (int)STRLEN(origval);
1779 // strip a trailing comma, would get 2
1780 if (comma && len > 1
1781 && (flags & P_ONECOMMA) == P_ONECOMMA
1782 && origval[len - 1] == ','
1783 && origval[len - 2] != '\\')
1784 len--;
1785 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1786 mch_memmove(newval, origval, (size_t)len);
1787 }
1788 else
1789 {
1790 len = (int)STRLEN(newval);
1791 STRMOVE(newval + len + comma, origval);
1792 }
1793 if (comma)
1794 newval[len] = ',';
1795}
1796
1797/*
1798 * Remove a value from a string option. Copy string option value in "origval"
1799 * to "newval" and then remove the string "strval" of length "len".
1800 */
1801 static void
1802stropt_remove_val(
1803 char_u *origval,
1804 char_u *newval,
1805 int flags,
1806 char_u *strval,
1807 int len)
1808{
1809 // Remove newval[] from origval[]. (Note: "len" has been set above
1810 // and is used here).
1811 STRCPY(newval, origval);
1812 if (*strval)
1813 {
1814 // may need to remove a comma
1815 if (flags & P_COMMA)
1816 {
1817 if (strval == origval)
1818 {
1819 // include comma after string
1820 if (strval[len] == ',')
1821 ++len;
1822 }
1823 else
1824 {
1825 // include comma before string
1826 --strval;
1827 ++len;
1828 }
1829 }
1830 STRMOVE(newval + (strval - origval), strval + len);
1831 }
1832}
1833
1834/*
1835 * Remove flags that appear twice in the string option value 'newval'.
1836 */
1837 static void
1838stropt_remove_dupflags(char_u *newval, int flags)
1839{
1840 char_u *s = newval;
1841
1842 // Remove flags that appear twice.
1843 while (*s)
1844 {
1845 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1846 if (flags & P_ONECOMMA)
1847 {
1848 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1849 {
1850 // Remove the duplicated value and the next comma.
1851 STRMOVE(s, s + 2);
1852 continue;
1853 }
1854 }
1855 else
1856 {
1857 if ((!(flags & P_COMMA) || *s != ',')
1858 && vim_strchr(s + 1, *s) != NULL)
1859 {
1860 STRMOVE(s, s + 1);
1861 continue;
1862 }
1863 }
1864 ++s;
1865 }
1866}
1867
1868/*
1869 * Get the string value specified for a ":set" command. The following set
1870 * options are supported:
1871 * set {opt}&
1872 * set {opt}<
1873 * set {opt}={val}
1874 * set {opt}:{val}
1875 */
1876 static char_u *
1877stropt_get_newval(
1878 int nextchar,
1879 int opt_idx,
1880 char_u **argp,
1881 char_u *varp,
1882 char_u **origval_arg,
1883 char_u **origval_l_arg,
1884 char_u **origval_g_arg,
1885 char_u **oldval_arg,
1886 set_op_T *op_arg,
1887 int flags,
1888 int cp_val)
1889{
1890 char_u *arg = *argp;
1891 char_u *origval = *origval_arg;
1892 char_u *origval_l = *origval_l_arg;
1893 char_u *origval_g = *origval_g_arg;
1894 char_u *oldval = *oldval_arg;
1895 set_op_T op = *op_arg;
1896 char_u *save_arg = NULL;
1897 char_u *newval;
1898 char_u *s = NULL;
1899 char_u whichwrap[80];
1900
1901 if (nextchar == '&') // set to default val
1902 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1903 else if (nextchar == '<') // set to global val
1904 newval = vim_strsave(*(char_u **)get_varp_scope(
1905 &(options[opt_idx]), OPT_GLOBAL));
1906 else
1907 {
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001908 ++arg; // jump to after the '=' or ':'
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001909
1910 // Set 'keywordprg' to ":help" if an empty
1911 // value was passed to :set by the user.
1912 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1913 {
1914 save_arg = arg;
1915 arg = (char_u *)":help";
1916 }
1917 // Convert 'backspace' number to string
1918 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1919 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1920 &oldval);
1921 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1922 {
1923 // Convert 'whichwrap' number to string, for backwards
1924 // compatibility with Vim 3.0.
1925 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1926 save_arg = arg;
1927 arg = t;
1928 }
1929 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1930 // version 3.0
1931 else if (*arg == '>' && (varp == (char_u *)&p_dir
1932 || varp == (char_u *)&p_bdir))
1933 ++arg;
1934
1935 // Copy the new string into allocated memory.
1936 newval = stropt_copy_value(origval, &arg, op, flags);
1937 if (newval == NULL)
1938 goto done;
1939
1940 // Expand environment variables and ~.
1941 // Don't do it when adding without inserting a comma.
1942 if (op == OP_NONE || (flags & P_COMMA))
1943 {
1944 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1945 if (newval == NULL)
1946 goto done;
1947 }
1948
1949 // locate newval[] in origval[] when removing it and when adding to
1950 // avoid duplicates
1951 int len = 0;
1952 if (op == OP_REMOVING || (flags & P_NODUP))
1953 {
1954 len = (int)STRLEN(newval);
1955 s = find_dup_item(origval, newval, flags);
1956
1957 // do not add if already there
1958 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1959 {
1960 op = OP_NONE;
1961 STRCPY(newval, origval);
1962 }
1963
1964 // if no duplicate, move pointer to end of original value
1965 if (s == NULL)
1966 s = origval + (int)STRLEN(origval);
1967 }
1968
1969 // concatenate the two strings; add a ',' if needed
1970 if (op == OP_ADDING || op == OP_PREPENDING)
1971 stropt_concat_with_comma(origval, newval, op, flags);
1972 else if (op == OP_REMOVING)
1973 // Remove newval[] from origval[]. (Note: "len" has been set above
1974 // and is used here).
1975 stropt_remove_val(origval, newval, flags, s, len);
1976
1977 if (flags & P_FLAGLIST)
1978 // Remove flags that appear twice.
1979 stropt_remove_dupflags(newval, flags);
1980 }
1981
1982done:
1983 if (save_arg != NULL)
1984 arg = save_arg; // arg was temporarily changed, restore it
1985 *argp = arg;
1986 *origval_arg = origval;
1987 *origval_l_arg = origval_l;
1988 *origval_g_arg = origval_g;
1989 *oldval_arg = oldval;
1990 *op_arg = op;
1991
1992 return newval;
1993}
1994
1995/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001996 * Part of do_set() for string options.
1997 * Returns FAIL on failure, do not process further options.
1998 */
1999 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002000do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01002001 int opt_idx,
2002 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01002003 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01002004 int nextchar,
2005 set_op_T op_arg,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02002006 long_u flags,
Bram Moolenaar47403942022-09-21 21:12:53 +01002007 int cp_val,
2008 char_u *varp_arg,
2009 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +01002010 size_t errbuflen,
Bram Moolenaar47403942022-09-21 21:12:53 +01002011 int *value_checked,
2012 char **errmsg)
2013{
Bram Moolenaar6f981142022-09-22 12:48:58 +01002014 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01002015 set_op_T op = op_arg;
2016 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002017 char_u *oldval = NULL; // previous value if *varp
2018 char_u *newval;
2019 char_u *origval = NULL;
2020 char_u *origval_l = NULL;
2021 char_u *origval_g = NULL;
2022#if defined(FEAT_EVAL)
2023 char_u *saved_origval = NULL;
2024 char_u *saved_origval_l = NULL;
2025 char_u *saved_origval_g = NULL;
2026 char_u *saved_newval = NULL;
2027#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01002028
2029 // When using ":set opt=val" for a global option
2030 // with a local value the local value will be
2031 // reset, use the global value here.
2032 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2033 && ((int)options[opt_idx].indir & PV_BOTH))
2034 varp = options[opt_idx].var;
2035
2036 // The old value is kept until we are sure that the new value is valid.
2037 oldval = *(char_u **)varp;
2038
2039 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2040 {
2041 origval_l = *(char_u **)get_varp_scope(
2042 &(options[opt_idx]), OPT_LOCAL);
2043 origval_g = *(char_u **)get_varp_scope(
2044 &(options[opt_idx]), OPT_GLOBAL);
2045
2046 // A global-local string option might have an empty option as value to
2047 // indicate that the global value should be used.
2048 if (((int)options[opt_idx].indir & PV_BOTH)
2049 && origval_l == empty_option)
2050 origval_l = origval_g;
2051 }
2052
2053 // When setting the local value of a global option, the old value may be
2054 // the global value.
2055 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
2056 origval = *(char_u **)get_varp(&options[opt_idx]);
2057 else
2058 origval = oldval;
2059
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002060 // Get the new value for the option
2061 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
2062 &origval_l, &origval_g, &oldval, &op, flags,
2063 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01002064
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002065 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01002066 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00002067 if (newval == NULL)
2068 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01002069
2070#if defined(FEAT_EVAL)
2071 if (!starting
2072# ifdef FEAT_CRYPT
2073 && options[opt_idx].indir != PV_KEY
2074# endif
2075 && origval != NULL && newval != NULL)
2076 {
2077 // origval may be freed by did_set_string_option(), make a copy.
2078 saved_origval = vim_strsave(origval);
2079 // newval (and varp) may become invalid if the buffer is closed by
2080 // autocommands.
2081 saved_newval = vim_strsave(newval);
2082 if (origval_l != NULL)
2083 saved_origval_l = vim_strsave(origval_l);
2084 if (origval_g != NULL)
2085 saved_origval_g = vim_strsave(origval_g);
2086 }
2087#endif
2088
2089 {
2090 long_u *p = insecure_flag(opt_idx, opt_flags);
2091 int secure_saved = secure;
2092
2093 // When an option is set in the sandbox, from a modeline or in secure
2094 // mode, then deal with side effects in secure mode. Also when the
2095 // value was set with the P_INSECURE flag and is not completely
2096 // replaced.
2097 if ((opt_flags & OPT_MODELINE)
2098#ifdef HAVE_SANDBOX
2099 || sandbox != 0
2100#endif
2101 || (op != OP_NONE && (*p & P_INSECURE)))
2102 secure = 1;
2103
2104 // Handle side effects, and set the global value for ":set" on local
2105 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2106 // be triggered that can cause havoc.
2107 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002108 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002109 errbuflen, opt_flags, op, value_checked);
Bram Moolenaar47403942022-09-21 21:12:53 +01002110
2111 secure = secure_saved;
2112 }
2113
2114#if defined(FEAT_EVAL)
2115 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002116 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002117 saved_origval_l, saved_origval_g, saved_newval);
2118 vim_free(saved_origval);
2119 vim_free(saved_origval_l);
2120 vim_free(saved_origval_g);
2121 vim_free(saved_newval);
2122#endif
2123
Bram Moolenaar6f981142022-09-22 12:48:58 +01002124 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002125 return *errmsg == NULL ? OK : FAIL;
2126}
2127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002129 * Set a boolean option.
2130 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002131 */
2132 static char *
2133do_set_option_bool(
2134 int opt_idx,
2135 int opt_flags,
2136 set_prefix_T prefix,
2137 long_u flags,
2138 char_u *varp,
2139 int nextchar,
2140 int afterchar,
2141 int cp_val)
2142
2143{
2144 varnumber_T value;
2145
2146 if (nextchar == '=' || nextchar == ':')
2147 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002148 if (opt_idx < 0 || varp == NULL)
2149 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002150
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002151 // ":set opt!": invert
2152 // ":set opt&": reset to default value
2153 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002154 if (nextchar == '!')
2155 value = *(int *)(varp) ^ 1;
2156 else if (nextchar == '&')
2157 value = (int)(long)(long_i)options[opt_idx].def_val[
2158 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2159 else if (nextchar == '<')
2160 {
2161 // For 'autoread' -1 means to use global value.
2162 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2163 value = -1;
2164 else
2165 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2166 }
2167 else
2168 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002169 // ":set invopt": invert
2170 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002171 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2172 return e_trailing_characters;
2173 if (prefix == PREFIX_INV)
2174 value = *(int *)(varp) ^ 1;
2175 else
2176 value = prefix == PREFIX_NO ? 0 : 1;
2177 }
2178
2179 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2180}
2181
2182/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002183 * Set a numeric option.
2184 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002185 */
2186 static char *
2187do_set_option_numeric(
2188 int opt_idx,
2189 int opt_flags,
2190 char_u **argp,
2191 int nextchar,
2192 set_op_T op,
2193 long_u flags,
2194 int cp_val,
2195 char_u *varp,
2196 char *errbuf,
2197 size_t errbuflen)
2198{
2199 char_u *arg = *argp;
2200 varnumber_T value;
2201 int i;
2202 char *errmsg = NULL;
2203
Bram Moolenaar546933f2023-02-06 16:40:49 +00002204 if (opt_idx < 0 || varp == NULL)
2205 return NULL; // "cannot happen"
2206 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002207 /*
2208 * Different ways to set a number option:
2209 * & set to default value
2210 * < set to global value
2211 * <xx> accept special key codes for 'wildchar'
2212 * c accept any non-digit for 'wildchar'
2213 * [-]0-9 set number
2214 * other error
2215 */
2216 ++arg;
2217 if (nextchar == '&')
2218 value = (long)(long_i)options[opt_idx].def_val[
2219 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2220 else if (nextchar == '<')
2221 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002222 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002223 // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002224 value = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002225 else if (opt_flags == OPT_LOCAL
2226 && ((long *)varp == &curwin->w_p_siso
2227 || (long *)varp == &curwin->w_p_so))
2228 // for 'scrolloff'/'sidescrolloff' -1 means using the global value
2229 value = -1;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002230 else
2231 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2232 }
2233 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2234 && (*arg == '<'
2235 || *arg == '^'
2236 || (*arg != NUL
2237 && (!arg[1] || VIM_ISWHITE(arg[1]))
2238 && !VIM_ISDIGIT(*arg))))
2239 {
2240 value = string_to_key(arg, FALSE);
2241 if (value == 0 && (long *)varp != &p_wcm)
2242 {
2243 errmsg = e_invalid_argument;
2244 goto skip;
2245 }
2246 }
2247 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2248 {
2249 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002250 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002251 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2252 {
2253 errmsg = e_number_required_after_equal;
2254 goto skip;
2255 }
2256 }
2257 else
2258 {
2259 errmsg = e_number_required_after_equal;
2260 goto skip;
2261 }
2262
2263 if (op == OP_ADDING)
2264 value = *(long *)varp + value;
2265 else if (op == OP_PREPENDING)
2266 value = *(long *)varp * value;
2267 else if (op == OP_REMOVING)
2268 value = *(long *)varp - value;
2269
2270 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2271 opt_flags);
2272
2273skip:
2274 *argp = arg;
2275 return errmsg;
2276}
2277
2278/*
2279 * Set a key code (t_xx) option
2280 */
2281 static char *
2282do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2283{
2284 char_u *arg = *argp;
2285 char_u *p;
2286
2287 if (nextchar == '&')
2288 {
2289 if (add_termcap_entry(key_name, TRUE) == FAIL)
2290 return e_not_found_in_termcap;
2291 }
2292 else
2293 {
2294 ++arg; // jump to after the '=' or ':'
2295 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2296 if (*p == '\\' && p[1] != NUL)
2297 ++p;
2298 nextchar = *p;
2299 *p = NUL;
2300 add_termcode(key_name, arg, FALSE);
2301 *p = nextchar;
2302 }
2303 if (full_screen)
2304 ttest(FALSE);
2305 redraw_all_later(UPD_CLEAR);
2306
2307 *argp = arg;
2308 return NULL;
2309}
2310
2311/*
2312 * Set an option to a new value.
2313 */
2314 static char *
2315do_set_option_value(
2316 int opt_idx,
2317 int opt_flags,
2318 char_u **argp,
2319 set_prefix_T prefix,
2320 set_op_T op,
2321 long_u flags,
2322 char_u *varp,
2323 char_u *key_name,
2324 int nextchar,
2325 int afterchar,
2326 int cp_val,
2327 int *stopopteval,
2328 char *errbuf,
2329 size_t errbuflen)
2330{
2331 int value_checked = FALSE;
2332 char *errmsg = NULL;
2333 char_u *arg = *argp;
2334
2335 if (flags & P_BOOL)
2336 {
2337 // boolean option
2338 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2339 nextchar, afterchar, cp_val);
2340 if (errmsg != NULL)
2341 goto skip;
2342 }
2343 else
2344 {
2345 // numeric or string option
2346 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2347 || prefix != PREFIX_NONE)
2348 {
2349 errmsg = e_invalid_argument;
2350 goto skip;
2351 }
2352
2353 if (flags & P_NUM)
2354 {
2355 // numeric option
2356 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2357 op, flags, cp_val, varp,
2358 errbuf, errbuflen);
2359 if (errmsg != NULL)
2360 goto skip;
2361 }
2362 else if (opt_idx >= 0)
2363 {
2364 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002365 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002366 flags, cp_val, varp, errbuf, errbuflen,
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002367 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002368 {
2369 if (errmsg != NULL)
2370 goto skip;
2371 *stopopteval = TRUE;
2372 goto skip;
2373 }
2374 }
2375 else
2376 {
2377 // key code option
2378 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2379 if (errmsg != NULL)
2380 goto skip;
2381 }
2382 }
2383
2384 if (opt_idx >= 0)
2385 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2386
2387skip:
2388 *argp = arg;
2389 return errmsg;
2390}
2391
2392/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002393 * Set an option to a new value.
2394 * Return NULL if OK, return an untranslated error message when something is
2395 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2396 */
2397 static char *
2398do_set_option(
2399 int opt_flags,
2400 char_u **argp,
2401 char_u *arg_start,
2402 char_u **startarg,
2403 int *did_show,
2404 int *stopopteval,
2405 char *errbuf,
2406 size_t errbuflen)
2407{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002408 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002409 char_u *arg;
2410 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2411 set_op_T op;
2412 long_u flags; // flags for current option
2413 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002414 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002415 int nextchar; // next non-white char after option name
2416 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002417 char *errmsg = NULL;
2418 int key;
2419 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002420
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002421 prefix = get_option_prefix(argp);
2422 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002423
2424 // find end of name
2425 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002426 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2427 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002428
2429 // remember character after option name
2430 afterchar = arg[len];
2431
2432 if (in_vim9script())
2433 {
2434 char_u *p = skipwhite(arg + len);
2435
2436 // disallow white space before =val, +=val, -=val, ^=val
2437 if (p > arg + len && (p[0] == '='
2438 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2439 && p[1] == '=')))
2440 {
2441 errmsg = e_no_white_space_allowed_between_option_and;
2442 arg = p;
2443 *startarg = p;
2444 goto skip;
2445 }
2446 }
2447 else
2448 // skip white space, allow ":set ai ?", ":set hlsearch !"
2449 while (VIM_ISWHITE(arg[len]))
2450 ++len;
2451
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002452 op = get_opt_op(arg + len);
2453 if (op != OP_NONE)
2454 len++;
2455
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002456 nextchar = arg[len];
2457
2458 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2459 {
2460 if (in_vim9script() && arg > arg_start
2461 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2462 errmsg = e_no_white_space_allowed_between_option_and;
2463 else
2464 errmsg = e_unknown_option;
2465 goto skip;
2466 }
2467
2468 if (opt_idx >= 0)
2469 {
2470 if (options[opt_idx].var == NULL) // hidden option: skip
2471 {
2472 // Only give an error message when requesting the value of
2473 // a hidden option, ignore setting it.
2474 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2475 && (!(options[opt_idx].flags & P_BOOL)
2476 || nextchar == '?'))
2477 errmsg = e_option_not_supported;
2478 goto skip;
2479 }
2480
2481 flags = options[opt_idx].flags;
2482 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2483 }
2484 else
2485 {
2486 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002487 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002488 if (key < 0)
2489 {
2490 key_name[0] = KEY2TERMCAP0(key);
2491 key_name[1] = KEY2TERMCAP1(key);
2492 }
2493 else
2494 {
2495 key_name[0] = KS_KEY;
2496 key_name[1] = (key & 0xff);
2497 }
2498 }
2499
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002500 // Make sure the option value can be changed.
2501 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002502 goto skip;
2503
Bram Moolenaar40b48722023-02-05 17:04:50 +00002504 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002505 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2506 {
2507 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002508 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2509 {
2510 if (arg[3] == 'm') // "opt&vim": set to Vim default
2511 {
2512 cp_val = FALSE;
2513 arg += 3;
2514 }
2515 else // "opt&vi": set to Vi default
2516 {
2517 cp_val = TRUE;
2518 arg += 2;
2519 }
2520 }
2521 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2522 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2523 {
2524 errmsg = e_trailing_characters;
2525 goto skip;
2526 }
2527 }
2528
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002529 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2530 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002531 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002532 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002533 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2534 && !(flags & P_BOOL)))
2535 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002536 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002537 if (*did_show)
2538 msg_putchar('\n'); // cursor below last one
2539 else
2540 {
2541 gotocmdline(TRUE); // cursor at status line
2542 *did_show = TRUE; // remember that we did a line
2543 }
2544 if (opt_idx >= 0)
2545 {
2546 showoneopt(&options[opt_idx], opt_flags);
2547#ifdef FEAT_EVAL
2548 if (p_verbose > 0)
2549 {
2550 // Mention where the option was last set.
2551 if (varp == options[opt_idx].var)
2552 last_set_msg(options[opt_idx].script_ctx);
2553 else if ((int)options[opt_idx].indir & PV_WIN)
2554 last_set_msg(curwin->w_p_script_ctx[
2555 (int)options[opt_idx].indir & PV_MASK]);
2556 else if ((int)options[opt_idx].indir & PV_BUF)
2557 last_set_msg(curbuf->b_p_script_ctx[
2558 (int)options[opt_idx].indir & PV_MASK]);
2559 }
2560#endif
2561 }
2562 else
2563 {
2564 char_u *p;
2565
2566 p = find_termcode(key_name);
2567 if (p == NULL)
2568 {
2569 errmsg = e_key_code_not_set;
2570 goto skip;
2571 }
2572 else
2573 (void)show_one_termcode(key_name, p, TRUE);
2574 }
2575 if (nextchar != '?'
2576 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2577 errmsg = e_trailing_characters;
2578 }
2579 else
2580 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002581 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2582 flags, varp, key_name, nextchar,
2583 afterchar, cp_val, stopopteval, errbuf,
2584 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002585 }
2586
2587skip:
2588 *argp = arg;
2589 return errmsg;
2590}
2591
2592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 * Parse 'arg' for option settings.
2594 *
2595 * 'arg' may be IObuff, but only when no errors can be present and option
2596 * does not need to be expanded with option_expand().
2597 * "opt_flags":
2598 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002599 * OPT_GLOBAL for ":setglobal"
2600 * OPT_LOCAL for ":setlocal" and a modeline
2601 * OPT_MODELINE for a modeline
2602 * OPT_WINONLY to only set window-local options
2603 * OPT_NOWIN to skip setting window-local options
2604 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002606 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 */
2608 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002609do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002610 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002611 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002613 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002615 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
2617 if (*arg == NUL)
2618 {
2619 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002620 did_show = TRUE;
2621 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
2623
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002624 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002626 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002627 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002629 // ":set all" show all options.
2630 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 arg += 3;
2632 if (*arg == '&')
2633 {
2634 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002635 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002637 didset_options();
2638 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002639 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 }
2641 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002644 did_show = TRUE;
2645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002647 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 {
2649 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002650 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002651 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 arg += 7;
2653 }
2654 else
2655 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002656 int stopopteval = FALSE;
2657 char *errmsg = NULL;
Christian Brabandtb39b2402023-11-29 11:34:05 +01002658 char errbuf[ERR_BUFLEN];
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002659 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002661 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2662 &did_show, &stopopteval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002663 ERR_BUFLEN);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002664 if (stopopteval)
2665 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002667 // Advance to next argument.
2668 // - skip until a blank found, taking care of backslashes
2669 // - skip blanks
2670 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 for (i = 0; i < 2 ; ++i)
2672 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002673 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if (*arg++ == '\\' && *arg != NUL)
2675 ++arg;
2676 arg = skipwhite(arg);
2677 if (*arg != '=')
2678 break;
2679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002681 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002683 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2684 i = (int)STRLEN(IObuff) + 2;
2685 if (i + (arg - startarg) < IOSIZE)
2686 {
2687 // append the argument with the error
2688 STRCAT(IObuff, ": ");
2689 mch_memmove(IObuff + i, startarg, (arg - startarg));
2690 IObuff[i + (arg - startarg)] = NUL;
2691 }
2692 // make sure all characters are printable
2693 trans_characters(IObuff, IOSIZE);
2694
2695 ++no_wait_return; // wait_return() done later
2696 emsg((char *)IObuff); // show error highlighted
2697 --no_wait_return;
2698
2699 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702
2703 arg = skipwhite(arg);
2704 }
2705
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002706theend:
2707 if (silent_mode && did_show)
2708 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002710 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002711 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002712 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002713 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002714 out_flush();
2715 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002716 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002717 }
2718
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 return OK;
2720}
2721
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002722/*
2723 * Call this when an option has been given a new value through a user command.
2724 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2725 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002726 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002727did_set_option(
2728 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002729 int opt_flags, // possibly with OPT_MODELINE
2730 int new_value, // value was replaced completely
2731 int value_checked) // value was checked to be safe, no need to set the
2732 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002733{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002734 long_u *p;
2735
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002736 options[opt_idx].flags |= P_WAS_SET;
2737
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002738 // When an option is set in the sandbox, from a modeline or in secure mode
2739 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2740 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002741 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002742 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002743#ifdef HAVE_SANDBOX
2744 || sandbox != 0
2745#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002746 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002747 *p = *p | P_INSECURE;
2748 else if (new_value)
2749 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002750}
2751
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752/*
2753 * Convert a key name or string into a key value.
2754 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002755 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002757 int
2758string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759{
2760 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002761 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 if (*arg == '^')
2763 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002764 if (multi_byte)
2765 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 return *arg;
2767}
2768
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769/*
2770 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2771 * maketitle() to create and display it.
2772 * When switching the title or icon off, call mch_restore_title() to get
2773 * the old value back.
2774 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002775 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002776did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
2778 if (starting != NO_SCREEN
2779#ifdef FEAT_GUI
2780 && !gui.starting
2781#endif
2782 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
2786/*
2787 * set_options_bin - called when 'bin' changes value.
2788 */
2789 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002790set_options_bin(
2791 int oldval,
2792 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002793 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002795 // The option values that are changed when 'bin' changes are
2796 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (newval)
2798 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002799 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
2801 if (!(opt_flags & OPT_GLOBAL))
2802 {
2803 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2804 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2805 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2806 curbuf->b_p_et_nobin = curbuf->b_p_et;
2807 }
2808 if (!(opt_flags & OPT_LOCAL))
2809 {
2810 p_tw_nobin = p_tw;
2811 p_wm_nobin = p_wm;
2812 p_ml_nobin = p_ml;
2813 p_et_nobin = p_et;
2814 }
2815 }
2816
2817 if (!(opt_flags & OPT_GLOBAL))
2818 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002819 curbuf->b_p_tw = 0; // no automatic line wrap
2820 curbuf->b_p_wm = 0; // no automatic line wrap
2821 curbuf->b_p_ml = 0; // no modelines
2822 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 }
2824 if (!(opt_flags & OPT_LOCAL))
2825 {
2826 p_tw = 0;
2827 p_wm = 0;
2828 p_ml = FALSE;
2829 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002830 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 }
2832 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002833 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
2835 if (!(opt_flags & OPT_GLOBAL))
2836 {
2837 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2838 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2839 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2840 curbuf->b_p_et = curbuf->b_p_et_nobin;
2841 }
2842 if (!(opt_flags & OPT_LOCAL))
2843 {
2844 p_tw = p_tw_nobin;
2845 p_wm = p_wm_nobin;
2846 p_ml = p_ml_nobin;
2847 p_et = p_et_nobin;
2848 }
2849 }
Christian Brabandt757593c2023-08-22 21:44:10 +02002850#if defined(FEAT_EVAL) || defined(PROTO)
2851 // Remember where the dependent option were reset
2852 didset_options_sctx(opt_flags, p_bin_dep_opts);
2853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854}
2855
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856/*
2857 * Expand environment variables for some string options.
2858 * These string options cannot be indirect!
2859 * If "val" is NULL expand the current value of the option.
2860 * Return pointer to NameBuff, or NULL when not expanded.
2861 */
2862 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002863option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2867 return NULL;
2868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002869 // If val is longer than MAXPATHL no meaningful expansion can be done,
2870 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 if (val != NULL && STRLEN(val) > MAXPATHL)
2872 return NULL;
2873
2874 if (val == NULL)
2875 val = *(char_u **)options[opt_idx].var;
2876
2877 /*
2878 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2879 * Escape spaces when expanding 'tags', they are used to separate file
2880 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002881 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 */
2883 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002884 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002885#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002886 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2887#endif
2888 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 return NULL;
2891
2892 return NameBuff;
2893}
2894
2895/*
2896 * After setting various option values: recompute variables that depend on
2897 * option values.
2898 */
2899 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002900didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002902 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 (void)init_chartab();
2904
Bram Moolenaardac13472019-09-16 21:06:21 +02002905 didset_string_options();
2906
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002907#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002908 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002909 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002910 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002911 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002912#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002913 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002914 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002915#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002917 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002918#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002919 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002920}
2921
2922/*
2923 * More side effects of setting options.
2924 */
2925 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002926didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002927{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002929 (void)highlight_changed();
2930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002931 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002932 check_opt_wim();
2933
Bram Moolenaareed9d462021-02-15 20:38:25 +01002934 // Parse default for 'listchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002935 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE, NULL, 0);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 // Parse default for 'fillchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002938 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002939
2940#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002942 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002943#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002944#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002945 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002946 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002947 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002948 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950}
2951
2952/*
2953 * Check for string options that are NULL (normally only termcap options).
2954 */
2955 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002956check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957{
2958 int opt_idx;
2959
2960 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2961 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2962 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2963}
2964
2965/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002966 * Return the option index found by a pointer into term_strings[].
2967 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002969 int
2970get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002972 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2975 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002976 return opt_idx;
2977 return -1; // cannot happen: didn't find it!
2978}
2979
2980/*
2981 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2982 * Return the option index or -1 if not found.
2983 */
2984 int
2985set_term_option_alloced(char_u **p)
2986{
2987 int opt_idx = get_term_opt_idx(p);
2988
2989 if (opt_idx >= 0)
2990 options[opt_idx].flags |= P_ALLOCED;
2991 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992}
2993
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002994#if defined(FEAT_EVAL) || defined(PROTO)
2995/*
2996 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2997 * Return FALSE when it wasn't.
2998 * Return -1 for an unknown option.
2999 */
3000 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003001was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003002{
3003 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003004 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003005
3006 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003007 {
3008 flagp = insecure_flag(idx, opt_flags);
3009 return (*flagp & P_INSECURE) != 0;
3010 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01003011 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003012 return -1;
3013}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003014
3015/*
3016 * Get a pointer to the flags used for the P_INSECURE flag of option
3017 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01003018 * NOTE: Caller must make sure that "curwin" is set to the window from which
3019 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003020 */
3021 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003022insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003023{
3024 if (opt_flags & OPT_LOCAL)
3025 switch ((int)options[opt_idx].indir)
3026 {
3027#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003028 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003029#endif
3030#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00003031# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003032 case PV_FDE: return &curwin->w_p_fde_flags;
3033 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00003034# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00003035# ifdef FEAT_BEVAL
3036 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
3037# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003038 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003039 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003040# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003041 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003042# endif
3043#endif
3044 }
3045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003046 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003047 return &options[opt_idx].flags;
3048}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003049#endif
3050
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003051/*
3052 * Redraw the window title and/or tab page text later.
3053 */
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02003054 void
3055redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003056{
3057 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003058 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003059}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003060
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003062 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
3063 * characters or characters in "allowed".
3064 */
Bram Moolenaare677df82019-09-02 22:31:11 +02003065 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003066valid_name(char_u *val, char *allowed)
3067{
3068 char_u *s;
3069
3070 for (s = val; *s != NUL; ++s)
3071 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
3072 return FALSE;
3073 return TRUE;
3074}
3075
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003076#if defined(FEAT_EVAL) || defined(PROTO)
3077/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003078 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003079 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003080 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003081 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003082set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003083{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003084 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3085 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003086 sctx_T new_script_ctx = script_ctx;
3087
Bram Moolenaar51258742020-05-03 17:19:33 +02003088 // Modeline already has the line number set.
3089 if (!(opt_flags & OPT_MODELINE))
3090 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003091
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003092 // Remember where the option was set. For local options need to do that
3093 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003094 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003095 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003096 if (both || (opt_flags & OPT_LOCAL))
3097 {
3098 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003099 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003100 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003101 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003102 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003103 if (both)
3104 // also setting the "all buffers" value
3105 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3106 new_script_ctx;
3107 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003108 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003109}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003110
3111/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003112 * Get the script context of global option "name".
3113 *
3114 */
3115 sctx_T *
3116get_option_sctx(char *name)
3117{
3118 int idx = findoption((char_u *)name);
3119
3120 if (idx >= 0)
3121 return &options[idx].script_ctx;
3122 siemsg("no such option: %s", name);
3123 return NULL;
3124}
3125
3126/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003127 * Set the script_ctx for a termcap option.
3128 * "name" must be the two character code, e.g. "RV".
3129 * When "name" is NULL use "opt_idx".
3130 */
3131 void
3132set_term_option_sctx_idx(char *name, int opt_idx)
3133{
3134 char_u buf[5];
3135 int idx;
3136
3137 if (name == NULL)
3138 idx = opt_idx;
3139 else
3140 {
3141 buf[0] = 't';
3142 buf[1] = '_';
3143 buf[2] = name[0];
3144 buf[3] = name[1];
3145 buf[4] = 0;
3146 idx = findoption(buf);
3147 }
3148 if (idx >= 0)
3149 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3150}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003151#endif
3152
Bram Moolenaarf4140482020-02-15 23:06:45 +01003153#if defined(FEAT_EVAL)
3154/*
3155 * Apply the OptionSet autocommand.
3156 */
3157 static void
3158apply_optionset_autocmd(
3159 int opt_idx,
3160 long opt_flags,
3161 long oldval,
3162 long oldval_g,
3163 long newval,
3164 char *errmsg)
3165{
3166 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3167
3168 // Don't do this while starting up, failure or recursively.
3169 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3170 return;
3171
3172 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3173 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3174 oldval_g);
3175 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3176 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3177 (opt_flags & OPT_LOCAL) ? "local" : "global");
3178 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3179 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3180 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3181 if (opt_flags & OPT_LOCAL)
3182 {
3183 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3184 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3185 }
3186 if (opt_flags & OPT_GLOBAL)
3187 {
3188 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3189 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3190 }
3191 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3192 {
3193 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3194 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3195 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3196 }
3197 if (opt_flags & OPT_MODELINE)
3198 {
3199 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3200 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3201 }
3202 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3203 NULL, FALSE, NULL);
3204 reset_v_option_vars();
3205}
3206#endif
3207
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003208#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003209/*
3210 * Process the updated 'arabic' option value.
3211 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003212 char *
3213did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003214{
3215 char *errmsg = NULL;
3216
3217 if (curwin->w_p_arab)
3218 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003219 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003220 if (!p_tbidi)
3221 {
3222 // set rightleft mode
3223 if (!curwin->w_p_rl)
3224 {
3225 curwin->w_p_rl = TRUE;
3226 changed_window_setting();
3227 }
3228
3229 // Enable Arabic shaping (major part of what Arabic requires)
3230 if (!p_arshape)
3231 {
3232 p_arshape = TRUE;
3233 redraw_later_clear();
3234 }
3235 }
3236
3237 // Arabic requires a utf-8 encoding, inform the user if it's not
3238 // set.
3239 if (STRCMP(p_enc, "utf-8") != 0)
3240 {
3241 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3242
3243 msg_source(HL_ATTR(HLF_W));
3244 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3245#ifdef FEAT_EVAL
3246 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3247#endif
3248 }
3249
3250 // set 'delcombine'
3251 p_deco = TRUE;
3252
3253# ifdef FEAT_KEYMAP
3254 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003255 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3256 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003257# endif
3258 }
3259 else
3260 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003261 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003262 if (!p_tbidi)
3263 {
3264 // reset rightleft mode
3265 if (curwin->w_p_rl)
3266 {
3267 curwin->w_p_rl = FALSE;
3268 changed_window_setting();
3269 }
3270
3271 // 'arabicshape' isn't reset, it is a global option and
3272 // another window may still need it "on".
3273 }
3274
3275 // 'delcombine' isn't reset, it is a global option and another
3276 // window may still want it "on".
3277
3278# ifdef FEAT_KEYMAP
3279 // Revert to the default keymap
3280 curbuf->b_p_iminsert = B_IMODE_NONE;
3281 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3282# endif
3283 }
3284
3285 return errmsg;
3286}
3287#endif
3288
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003289#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3290/*
3291 * Process the updated 'autochdir' option value.
3292 */
3293 char *
3294did_set_autochdir(optset_T *args UNUSED)
3295{
3296 // Change directories when the 'acd' option is set now.
3297 DO_AUTOCHDIR;
3298 return NULL;
3299}
3300#endif
3301
3302#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3303/*
3304 * Process the updated 'ballooneval' option value.
3305 */
3306 char *
3307did_set_ballooneval(optset_T *args)
3308{
3309 if (balloonEvalForTerm)
3310 return NULL;
3311
3312 if (p_beval && !args->os_oldval.boolean)
3313 gui_mch_enable_beval_area(balloonEval);
3314 else if (!p_beval && args->os_oldval.boolean)
3315 gui_mch_disable_beval_area(balloonEval);
3316
3317 return NULL;
3318}
3319#endif
3320
3321#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3322/*
3323 * Process the updated 'balloonevalterm' option value.
3324 */
3325 char *
3326did_set_balloonevalterm(optset_T *args UNUSED)
3327{
3328 mch_bevalterm_changed();
3329 return NULL;
3330}
3331#endif
3332
3333/*
3334 * Process the updated 'binary' option value.
3335 */
3336 char *
3337did_set_binary(optset_T *args)
3338{
3339 // when 'bin' is set also set some other options
3340 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3341 redraw_titles();
3342
3343 return NULL;
3344}
3345
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003346/*
3347 * Process the updated 'buflisted' option value.
3348 */
3349 char *
3350did_set_buflisted(optset_T *args)
3351{
3352 // when 'buflisted' changes, trigger autocommands
3353 if (args->os_oldval.boolean != curbuf->b_p_bl)
3354 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3355 NULL, NULL, TRUE, curbuf);
3356 return NULL;
3357}
3358
3359/*
3360 * Process the new 'cmdheight' option value.
3361 */
3362 char *
3363did_set_cmdheight(optset_T *args)
3364{
3365 long old_value = args->os_oldval.number;
3366 char *errmsg = NULL;
3367
3368 // if p_ch changed value, change the command line height
3369 if (p_ch < 1)
3370 {
3371 errmsg = e_argument_must_be_positive;
3372 p_ch = 1;
3373 }
3374 if (p_ch > Rows - min_rows() + 1)
3375 p_ch = Rows - min_rows() + 1;
3376
3377 // Only compute the new window layout when startup has been
3378 // completed. Otherwise the frame sizes may be wrong.
3379 if ((p_ch != old_value
3380 || tabline_height() + topframe->fr_height != Rows - p_ch)
3381 && full_screen
3382#ifdef FEAT_GUI
3383 && !gui.starting
3384#endif
3385 )
3386 command_height();
3387
3388 return errmsg;
3389}
3390
3391/*
3392 * Process the updated 'compatible' option value.
3393 */
3394 char *
3395did_set_compatible(optset_T *args UNUSED)
3396{
3397 compatible_set();
3398 return NULL;
3399}
3400
3401#if defined(FEAT_CONCEAL) || defined(PROTO)
3402/*
3403 * Process the new 'conceallevel' option value.
3404 */
3405 char *
3406did_set_conceallevel(optset_T *args UNUSED)
3407{
3408 char *errmsg = NULL;
3409
3410 if (curwin->w_p_cole < 0)
3411 {
3412 errmsg = e_argument_must_be_positive;
3413 curwin->w_p_cole = 0;
3414 }
3415 else if (curwin->w_p_cole > 3)
3416 {
3417 errmsg = e_invalid_argument;
3418 curwin->w_p_cole = 3;
3419 }
3420
3421 return errmsg;
3422}
3423#endif
3424
3425#if defined(FEAT_DIFF) || defined(PROTO)
3426/*
3427 * Process the updated 'diff' option value.
3428 */
3429 char *
3430did_set_diff(optset_T *args UNUSED)
3431{
3432 // May add or remove the buffer from the list of diff buffers.
3433 diff_buf_adjust(curwin);
3434# ifdef FEAT_FOLDING
3435 if (foldmethodIsDiff(curwin))
3436 foldUpdateAll(curwin);
3437# endif
3438 return NULL;
3439}
3440#endif
3441
3442/*
3443 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3444 * option value.
3445 */
3446 char *
3447did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3448{
3449 // redraw the window title and tab page text
3450 redraw_titles();
3451 return NULL;
3452}
3453
3454/*
3455 * Process the updated 'equalalways' option value.
3456 */
3457 char *
3458did_set_equalalways(optset_T *args)
3459{
3460 if (p_ea && !args->os_oldval.boolean)
3461 win_equal(curwin, FALSE, 0);
3462
3463 return NULL;
3464}
3465
3466#if defined(FEAT_FOLDING) || defined(PROTO)
3467/*
3468 * Process the new 'foldcolumn' option value.
3469 */
3470 char *
3471did_set_foldcolumn(optset_T *args UNUSED)
3472{
3473 char *errmsg = NULL;
3474
3475 if (curwin->w_p_fdc < 0)
3476 {
3477 errmsg = e_argument_must_be_positive;
3478 curwin->w_p_fdc = 0;
3479 }
3480 else if (curwin->w_p_fdc > 12)
3481 {
3482 errmsg = e_invalid_argument;
3483 curwin->w_p_fdc = 12;
3484 }
3485
3486 return errmsg;
3487}
3488
3489/*
3490 * Process the new 'foldlevel' option value.
3491 */
3492 char *
3493did_set_foldlevel(optset_T *args UNUSED)
3494{
3495 if (curwin->w_p_fdl < 0)
3496 curwin->w_p_fdl = 0;
3497 newFoldLevel();
3498 return NULL;
3499}
3500
3501/*
3502 * Process the new 'foldminlines' option value.
3503 */
3504 char *
3505did_set_foldminlines(optset_T *args UNUSED)
3506{
3507 foldUpdateAll(curwin);
3508 return NULL;
3509}
3510
3511/*
3512 * Process the new 'foldnestmax' option value.
3513 */
3514 char *
3515did_set_foldnestmax(optset_T *args UNUSED)
3516{
3517 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3518 foldUpdateAll(curwin);
3519 return NULL;
3520}
3521#endif
3522
3523#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3524/*
3525 * Process the updated 'hlsearch' option value.
3526 */
3527 char *
3528did_set_hlsearch(optset_T *args UNUSED)
3529{
3530 // when 'hlsearch' is set or reset: reset no_hlsearch
3531 set_no_hlsearch(FALSE);
3532 return NULL;
3533}
3534#endif
3535
3536/*
3537 * Process the updated 'ignorecase' option value.
3538 */
3539 char *
3540did_set_ignorecase(optset_T *args UNUSED)
3541{
3542 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3543 if (p_hls)
3544 redraw_all_later(UPD_SOME_VALID);
3545 return NULL;
3546}
3547
3548#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3549/*
3550 * Process the updated 'imdisable' option value.
3551 */
3552 char *
3553did_set_imdisable(optset_T *args UNUSED)
3554{
3555 // Only de-activate it here, it will be enabled when changing mode.
3556 if (p_imdisable)
3557 im_set_active(FALSE);
3558 else if (State & MODE_INSERT)
3559 // When the option is set from an autocommand, it may need to take
3560 // effect right away.
3561 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3562 return NULL;
3563}
3564#endif
3565
3566/*
3567 * Process the new 'iminsert' option value.
3568 */
3569 char *
3570did_set_iminsert(optset_T *args UNUSED)
3571{
3572 char *errmsg = NULL;
3573
3574 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3575 {
3576 errmsg = e_invalid_argument;
3577 curbuf->b_p_iminsert = B_IMODE_NONE;
3578 }
3579 p_iminsert = curbuf->b_p_iminsert;
3580 if (termcap_active) // don't do this in the alternate screen
3581 showmode();
3582#if defined(FEAT_KEYMAP)
3583 // Show/unshow value of 'keymap' in status lines.
3584 status_redraw_curbuf();
3585#endif
3586
3587 return errmsg;
3588}
3589
3590/*
3591 * Process the new 'imsearch' option value.
3592 */
3593 char *
3594did_set_imsearch(optset_T *args UNUSED)
3595{
3596 char *errmsg = NULL;
3597
3598 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3599 {
3600 errmsg = e_invalid_argument;
3601 curbuf->b_p_imsearch = B_IMODE_NONE;
3602 }
3603 p_imsearch = curbuf->b_p_imsearch;
3604
3605 return errmsg;
3606}
3607
3608#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3609/*
3610 * Process the new 'imstyle' option value.
3611 */
3612 char *
3613did_set_imstyle(optset_T *args UNUSED)
3614{
3615 char *errmsg = NULL;
3616
3617 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3618 errmsg = e_invalid_argument;
3619
3620 return errmsg;
3621}
3622#endif
3623
3624/*
3625 * Process the updated 'insertmode' option value.
3626 */
3627 char *
3628did_set_insertmode(optset_T *args)
3629{
3630 // when 'insertmode' is set from an autocommand need to do work here
3631 if (p_im)
3632 {
3633 if ((State & MODE_INSERT) == 0)
3634 need_start_insertmode = TRUE;
3635 stop_insert_mode = FALSE;
3636 }
3637 // only reset if it was set previously
3638 else if (args->os_oldval.boolean)
3639 {
3640 need_start_insertmode = FALSE;
3641 stop_insert_mode = TRUE;
3642 if (restart_edit != 0 && mode_displayed)
3643 clear_cmdline = TRUE; // remove "(insert)"
3644 restart_edit = 0;
3645 }
3646
3647 return NULL;
3648}
3649
3650#if defined(FEAT_LANGMAP) || defined(PROTO)
3651/*
3652 * Process the updated 'langnoremap' option value.
3653 */
3654 char *
3655did_set_langnoremap(optset_T *args UNUSED)
3656{
3657 // 'langnoremap' -> !'langremap'
3658 p_lrm = !p_lnr;
3659 return NULL;
3660}
3661
3662/*
3663 * Process the updated 'langremap' option value.
3664 */
3665 char *
3666did_set_langremap(optset_T *args UNUSED)
3667{
3668 // 'langremap' -> !'langnoremap'
3669 p_lnr = !p_lrm;
3670 return NULL;
3671}
3672#endif
3673
3674/*
3675 * Process the new 'laststatus' option value.
3676 */
3677 char *
3678did_set_laststatus(optset_T *args UNUSED)
3679{
3680 last_status(FALSE); // (re)set last window status line
3681 return NULL;
3682}
3683
3684#if defined(FEAT_GUI) || defined(PROTO)
3685/*
3686 * Process the new 'linespace' option value.
3687 */
3688 char *
3689did_set_linespace(optset_T *args UNUSED)
3690{
3691 // Recompute gui.char_height and resize the Vim window to keep the
3692 // same number of lines.
3693 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3694 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3695 return NULL;
3696}
3697#endif
3698
3699/*
3700 * Process the updated 'lisp' option value.
3701 */
3702 char *
3703did_set_lisp(optset_T *args UNUSED)
3704{
3705 // When 'lisp' option changes include/exclude '-' in keyword characters.
3706 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3707 return NULL;
3708}
3709
3710/*
3711 * Process the new 'maxcombine' option value.
3712 */
3713 char *
3714did_set_maxcombine(optset_T *args UNUSED)
3715{
3716 if (p_mco > MAX_MCO)
3717 p_mco = MAX_MCO;
3718 else if (p_mco < 0)
3719 p_mco = 0;
3720 screenclear(); // will re-allocate the screen
3721 return NULL;
3722}
3723
3724/*
3725 * Process the updated 'modifiable' option value.
3726 */
3727 char *
3728did_set_modifiable(optset_T *args UNUSED)
3729{
3730 // when 'modifiable' is changed, redraw the window title
3731
3732# ifdef FEAT_TERMINAL
3733 // Cannot set 'modifiable' when in Terminal mode.
3734 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3735 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3736 {
3737 curbuf->b_p_ma = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003738 return e_cannot_make_terminal_with_running_job_modifiable;
3739 }
3740# endif
3741 redraw_titles();
3742
3743 return NULL;
3744}
3745
3746/*
3747 * Process the updated 'modified' option value.
3748 */
3749 char *
3750did_set_modified(optset_T *args)
3751{
3752 if (!args->os_newval.boolean)
3753 save_file_ff(curbuf); // Buffer is unchanged
3754 redraw_titles();
zeertzjq5bf6c212024-03-31 18:41:27 +02003755 curbuf->b_modified_was_set = args->os_newval.boolean;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003756 return NULL;
3757}
3758
3759#if defined(FEAT_GUI) || defined(PROTO)
3760/*
3761 * Process the updated 'mousehide' option value.
3762 */
3763 char *
3764did_set_mousehide(optset_T *args UNUSED)
3765{
3766 if (!p_mh)
3767 gui_mch_mousehide(FALSE);
3768 return NULL;
3769}
3770#endif
3771
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003772/*
3773 * Process the updated 'number' or 'relativenumber' option value.
3774 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003775 char *
3776did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003777{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003778#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003779 if (gui.in_use
3780 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3781 && curbuf->b_signlist != NULL)
3782 {
3783 // If the 'number' or 'relativenumber' options are modified and
3784 // 'signcolumn' is set to 'number', then clear the screen for a full
3785 // refresh. Otherwise the sign icons are not displayed properly in the
3786 // number column. If the 'number' option is set and only the
3787 // 'relativenumber' option is toggled, then don't refresh the screen
3788 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003789 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003790 redraw_all_later(UPD_CLEAR);
3791 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003792#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003793 return NULL;
3794}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003795
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003796#if defined(FEAT_LINEBREAK) || defined(PROTO)
3797/*
3798 * Process the new 'numberwidth' option value.
3799 */
3800 char *
3801did_set_numberwidth(optset_T *args UNUSED)
3802{
3803 char *errmsg = NULL;
3804
3805 // 'numberwidth' must be positive
3806 if (curwin->w_p_nuw < 1)
3807 {
3808 errmsg = e_argument_must_be_positive;
3809 curwin->w_p_nuw = 1;
3810 }
3811 if (curwin->w_p_nuw > 20)
3812 {
3813 errmsg = e_invalid_argument;
3814 curwin->w_p_nuw = 20;
3815 }
3816 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3817
3818 return errmsg;
3819}
3820#endif
3821
3822/*
3823 * Process the updated 'paste' option value. Called after p_paste was set or
3824 * reset. When 'paste' is set or reset also change other options.
3825 */
3826 char *
3827did_set_paste(optset_T *args UNUSED)
3828{
3829 static int old_p_paste = FALSE;
3830 static int save_sm = 0;
3831 static int save_sta = 0;
3832 static int save_ru = 0;
3833#ifdef FEAT_RIGHTLEFT
3834 static int save_ri = 0;
3835 static int save_hkmap = 0;
3836#endif
3837 buf_T *buf;
3838
3839 if (p_paste)
3840 {
3841 // Paste switched from off to on.
3842 // Save the current values, so they can be restored later.
3843 if (!old_p_paste)
3844 {
3845 // save options for each buffer
3846 FOR_ALL_BUFFERS(buf)
3847 {
3848 buf->b_p_tw_nopaste = buf->b_p_tw;
3849 buf->b_p_wm_nopaste = buf->b_p_wm;
3850 buf->b_p_sts_nopaste = buf->b_p_sts;
3851 buf->b_p_ai_nopaste = buf->b_p_ai;
3852 buf->b_p_et_nopaste = buf->b_p_et;
3853#ifdef FEAT_VARTABS
3854 if (buf->b_p_vsts_nopaste)
3855 vim_free(buf->b_p_vsts_nopaste);
3856 buf->b_p_vsts_nopaste =
3857 buf->b_p_vsts && buf->b_p_vsts != empty_option
3858 ? vim_strsave(buf->b_p_vsts) : NULL;
3859#endif
3860 }
3861
3862 // save global options
3863 save_sm = p_sm;
3864 save_sta = p_sta;
3865 save_ru = p_ru;
3866#ifdef FEAT_RIGHTLEFT
3867 save_ri = p_ri;
3868 save_hkmap = p_hkmap;
3869#endif
3870 // save global values for local buffer options
3871 p_ai_nopaste = p_ai;
3872 p_et_nopaste = p_et;
3873 p_sts_nopaste = p_sts;
3874 p_tw_nopaste = p_tw;
3875 p_wm_nopaste = p_wm;
3876#ifdef FEAT_VARTABS
3877 if (p_vsts_nopaste)
3878 vim_free(p_vsts_nopaste);
3879 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3880 ? vim_strsave(p_vsts) : NULL;
3881#endif
3882 }
3883
3884 // Always set the option values, also when 'paste' is set when it is
3885 // already on. Set options for each buffer.
3886 FOR_ALL_BUFFERS(buf)
3887 {
3888 buf->b_p_tw = 0; // textwidth is 0
3889 buf->b_p_wm = 0; // wrapmargin is 0
3890 buf->b_p_sts = 0; // softtabstop is 0
3891 buf->b_p_ai = 0; // no auto-indent
3892 buf->b_p_et = 0; // no expandtab
3893#ifdef FEAT_VARTABS
3894 if (buf->b_p_vsts)
3895 free_string_option(buf->b_p_vsts);
3896 buf->b_p_vsts = empty_option;
3897 VIM_CLEAR(buf->b_p_vsts_array);
3898#endif
3899 }
3900
3901 // set global options
3902 p_sm = 0; // no showmatch
3903 p_sta = 0; // no smarttab
3904 if (p_ru)
3905 status_redraw_all(); // redraw to remove the ruler
3906 p_ru = 0; // no ruler
3907#ifdef FEAT_RIGHTLEFT
3908 p_ri = 0; // no reverse insert
3909 p_hkmap = 0; // no Hebrew keyboard
3910#endif
3911 // set global values for local buffer options
3912 p_tw = 0;
3913 p_wm = 0;
3914 p_sts = 0;
3915 p_ai = 0;
Christian Brabandt757593c2023-08-22 21:44:10 +02003916 p_et = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003917#ifdef FEAT_VARTABS
3918 if (p_vsts)
3919 free_string_option(p_vsts);
3920 p_vsts = empty_option;
3921#endif
3922 }
3923
3924 // Paste switched from on to off: Restore saved values.
3925 else if (old_p_paste)
3926 {
3927 // restore options for each buffer
3928 FOR_ALL_BUFFERS(buf)
3929 {
3930 buf->b_p_tw = buf->b_p_tw_nopaste;
3931 buf->b_p_wm = buf->b_p_wm_nopaste;
3932 buf->b_p_sts = buf->b_p_sts_nopaste;
3933 buf->b_p_ai = buf->b_p_ai_nopaste;
3934 buf->b_p_et = buf->b_p_et_nopaste;
3935#ifdef FEAT_VARTABS
3936 if (buf->b_p_vsts)
3937 free_string_option(buf->b_p_vsts);
3938 buf->b_p_vsts = buf->b_p_vsts_nopaste
3939 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3940 vim_free(buf->b_p_vsts_array);
3941 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3942 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3943 else
3944 buf->b_p_vsts_array = NULL;
3945#endif
3946 }
3947
3948 // restore global options
3949 p_sm = save_sm;
3950 p_sta = save_sta;
3951 if (p_ru != save_ru)
3952 status_redraw_all(); // redraw to draw the ruler
3953 p_ru = save_ru;
3954#ifdef FEAT_RIGHTLEFT
3955 p_ri = save_ri;
3956 p_hkmap = save_hkmap;
3957#endif
3958 // set global values for local buffer options
3959 p_ai = p_ai_nopaste;
3960 p_et = p_et_nopaste;
3961 p_sts = p_sts_nopaste;
3962 p_tw = p_tw_nopaste;
3963 p_wm = p_wm_nopaste;
3964#ifdef FEAT_VARTABS
3965 if (p_vsts)
3966 free_string_option(p_vsts);
3967 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
3968#endif
3969 }
3970
3971 old_p_paste = p_paste;
3972
Christian Brabandt757593c2023-08-22 21:44:10 +02003973#if defined(FEAT_EVAL) || defined(PROTO)
3974 // Remember where the dependent options were reset
3975 didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
3976#endif
3977
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003978 return NULL;
3979}
3980
3981
3982#ifdef FEAT_QUICKFIX
3983/*
3984 * Process the updated 'previewwindow' option value.
3985 */
3986 char *
Yegappan Lakshmanan3ee25962023-12-04 20:31:14 +01003987did_set_previewwindow(optset_T *args UNUSED)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003988{
3989 if (!curwin->w_p_pvw)
3990 return NULL;
3991
3992 // There can be only one window with 'previewwindow' set.
3993 win_T *win;
3994
3995 FOR_ALL_WINDOWS(win)
3996 if (win->w_p_pvw && win != curwin)
3997 {
3998 curwin->w_p_pvw = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003999 return e_preview_window_already_exists;
4000 }
4001
4002 return NULL;
4003}
4004#endif
4005
4006#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
4007/*
4008 * Process the new 'pyxversion' option value.
4009 */
4010 char *
4011did_set_pyxversion(optset_T *args UNUSED)
4012{
4013 char *errmsg = NULL;
4014
4015 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4016 errmsg = e_invalid_argument;
4017
4018 return errmsg;
4019}
4020#endif
4021
4022/*
4023 * Process the updated 'readonly' option value.
4024 */
4025 char *
4026did_set_readonly(optset_T *args)
4027{
4028 // when 'readonly' is reset globally, also reset readonlymode
4029 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
4030 readonlymode = FALSE;
4031
4032 // when 'readonly' is set may give W10 again
4033 if (curbuf->b_p_ro)
4034 curbuf->b_did_warn = FALSE;
4035
4036 redraw_titles();
4037
4038 return NULL;
4039}
4040
4041/*
4042 * Process the updated 'scrollbind' option value.
4043 */
4044 char *
4045did_set_scrollbind(optset_T *args UNUSED)
4046{
4047 // when 'scrollbind' is set: snapshot the current position to avoid a jump
4048 // at the end of normal_cmd()
4049 if (!curwin->w_p_scb)
4050 return NULL;
4051
4052 do_check_scrollbind(FALSE);
4053 curwin->w_scbind_pos = curwin->w_topline;
4054 return NULL;
4055}
4056
4057#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
4058/*
4059 * Process the updated 'shellslash' option value.
4060 */
4061 char *
4062did_set_shellslash(optset_T *args UNUSED)
4063{
4064 if (p_ssl)
4065 {
4066 psepc = '/';
4067 psepcN = '\\';
4068 pseps[0] = '/';
4069 }
4070 else
4071 {
4072 psepc = '\\';
4073 psepcN = '/';
4074 pseps[0] = '\\';
4075 }
4076
4077 // need to adjust the file name arguments and buffer names.
4078 buflist_slash_adjust();
4079 alist_slash_adjust();
4080# ifdef FEAT_EVAL
4081 scriptnames_slash_adjust();
4082# endif
4083 return NULL;
4084}
4085#endif
4086
4087/*
4088 * Process the new 'shiftwidth' or the 'tabstop' option value.
4089 */
4090 char *
4091did_set_shiftwidth_tabstop(optset_T *args)
4092{
4093 long *pp = (long *)args->os_varp;
4094 char *errmsg = NULL;
4095
4096 if (curbuf->b_p_sw < 0)
4097 {
4098 errmsg = e_argument_must_be_positive;
4099#ifdef FEAT_VARTABS
4100 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4101 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4102 ? tabstop_first(curbuf->b_p_vts_array)
4103 : curbuf->b_p_ts;
4104#else
4105 curbuf->b_p_sw = curbuf->b_p_ts;
4106#endif
4107 }
4108
4109#ifdef FEAT_FOLDING
4110 if (foldmethodIsIndent(curwin))
4111 foldUpdateAll(curwin);
4112#endif
4113 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4114 // parse 'cinoptions'.
4115 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4116 parse_cino(curbuf);
4117
4118 return errmsg;
4119}
4120
4121/*
4122 * Process the new 'showtabline' option value.
4123 */
4124 char *
4125did_set_showtabline(optset_T *args UNUSED)
4126{
4127 shell_new_rows(); // recompute window positions and heights
4128 return NULL;
4129}
4130
4131/*
4132 * Process the updated 'smoothscroll' option value.
4133 */
4134 char *
4135did_set_smoothscroll(optset_T *args UNUSED)
4136{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004137 if (!curwin->w_p_sms)
4138 curwin->w_skipcol = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004139
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004140 return NULL;
4141}
4142
4143#if defined(FEAT_SPELL) || defined(PROTO)
4144/*
4145 * Process the updated 'spell' option value.
4146 */
4147 char *
4148did_set_spell(optset_T *args UNUSED)
4149{
4150 if (curwin->w_p_spell)
4151 return parse_spelllang(curwin);
4152
4153 return NULL;
4154}
4155#endif
4156
4157/*
4158 * Process the updated 'swapfile' option value.
4159 */
4160 char *
4161did_set_swapfile(optset_T *args UNUSED)
4162{
4163 // when 'swf' is set, create swapfile, when reset remove swapfile
4164 if (curbuf->b_p_swf && p_uc)
4165 ml_open_file(curbuf); // create the swap file
4166 else
4167 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4168 // buf->b_p_swf
4169 mf_close_file(curbuf, TRUE); // remove the swap file
4170 return NULL;
4171}
4172
4173#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004174 char *
4175did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004176{
4177# ifdef FEAT_VTP
4178 // Do not turn on 'tgc' when 24-bit colors are not supported.
4179 if (
4180# ifdef VIMDLL
4181 !gui.in_use && !gui.starting &&
4182# endif
4183 !has_vtp_working())
4184 {
4185 p_tgc = 0;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004186 return e_24_bit_colors_are_not_supported_on_this_environment;
4187 }
4188 if (is_term_win32())
4189 swap_tcap();
4190# endif
4191# ifdef FEAT_GUI
4192 if (!gui.in_use && !gui.starting)
4193# endif
4194 highlight_gui_started();
4195# ifdef FEAT_VTP
4196 // reset t_Co
4197 if (is_term_win32())
4198 {
4199 control_console_color_rgb();
4200 set_termname(T_NAME);
4201 init_highlight(TRUE, FALSE);
4202 }
4203# endif
4204# ifdef FEAT_TERMINAL
4205 term_update_colors_all();
4206 term_update_palette_all();
4207 term_update_wincolor_all();
4208# endif
4209
4210 return NULL;
4211}
4212#endif
4213
4214/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004215 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004217 char *
4218did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004220 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004222 // when 'terse' is set change 'shortmess'
4223 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004225 // insert 's' in p_shm
4226 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004227 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004228 STRCPY(IObuff, p_shm);
4229 STRCAT(IObuff, "s");
4230 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004231 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004232 // remove 's' from p_shm
4233 else if (!p_terse && p != NULL)
4234 STRMOVE(p, p + 1);
4235 return NULL;
4236}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004238/*
4239 * Process the updated 'textauto' option value.
4240 */
4241 char *
4242did_set_textauto(optset_T *args)
4243{
4244 // when 'textauto' is set or reset also change 'fileformats'
4245 set_string_option_direct((char_u *)"ffs", -1,
4246 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4247 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004248
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004249 return NULL;
4250}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004252/*
4253 * Process the updated 'textmode' option value.
4254 */
4255 char *
4256did_set_textmode(optset_T *args)
4257{
4258 // when 'textmode' is set or reset also change 'fileformat'
4259 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4260
4261 return NULL;
4262}
4263
4264/*
4265 * Process the new 'textwidth' option value.
4266 */
4267 char *
4268did_set_textwidth(optset_T *args UNUSED)
4269{
4270 char *errmsg = NULL;
4271
4272 if (curbuf->b_p_tw < 0)
4273 {
4274 errmsg = e_argument_must_be_positive;
4275 curbuf->b_p_tw = 0;
4276 }
4277#ifdef FEAT_SYN_HL
4278 {
4279 win_T *wp;
4280 tabpage_T *tp;
4281
4282 FOR_ALL_TAB_WINDOWS(tp, wp)
4283 check_colorcolumn(wp);
4284 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004285#endif
4286
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004287 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288}
4289
4290/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004291 * Process the updated 'title' or the 'icon' option value.
4292 */
4293 char *
4294did_set_title_icon(optset_T *args UNUSED)
4295{
4296 // when 'title' changed, may need to change the title; same for 'icon'
4297 did_set_title();
4298 return NULL;
4299}
4300
4301/*
4302 * Process the new 'titlelen' option value.
4303 */
4304 char *
4305did_set_titlelen(optset_T *args)
4306{
4307 long old_value = args->os_oldval.number;
4308 char *errmsg = NULL;
4309
4310 // if 'titlelen' has changed, redraw the title
4311 if (p_titlelen < 0)
4312 {
4313 errmsg = e_argument_must_be_positive;
4314 p_titlelen = 85;
4315 }
4316 if (starting != NO_SCREEN && old_value != p_titlelen)
4317 need_maketitle = TRUE;
4318
4319 return errmsg;
4320}
4321
4322#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4323/*
4324 * Process the updated 'undofile' option value.
4325 */
4326 char *
4327did_set_undofile(optset_T *args)
4328{
4329 // Only take action when the option was set.
4330 if (!curbuf->b_p_udf && !p_udf)
4331 return NULL;
4332
4333 // When reset we do not delete the undo file, the option may be set again
4334 // without making any changes in between.
4335 char_u hash[UNDO_HASH_SIZE];
4336 buf_T *save_curbuf = curbuf;
4337
4338 FOR_ALL_BUFFERS(curbuf)
4339 {
4340 // When 'undofile' is set globally: for every buffer, otherwise
4341 // only for the current buffer: Try to read in the undofile,
4342 // if one exists, the buffer wasn't changed and the buffer was
4343 // loaded
4344 if ((curbuf == save_curbuf
4345 || (args->os_flags & OPT_GLOBAL)
4346 || args->os_flags == 0)
4347 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4348 {
4349#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004350 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004351 continue;
4352#endif
4353 u_compute_hash(hash);
4354 u_read_undo(NULL, hash, curbuf->b_fname);
4355 }
4356 }
4357 curbuf = save_curbuf;
4358
4359 return NULL;
4360}
4361#endif
4362
4363/*
4364 * Process the new global 'undolevels' option value.
4365 */
4366 static void
4367update_global_undolevels(long value, long old_value)
4368{
4369 // sync undo before 'undolevels' changes
4370
4371 // use the old value, otherwise u_sync() may not work properly
4372 p_ul = old_value;
4373 u_sync(TRUE);
4374 p_ul = value;
4375}
4376
4377/*
4378 * Process the new buffer local 'undolevels' option value.
4379 */
4380 static void
4381update_buflocal_undolevels(long value, long old_value)
4382{
4383 // use the old value, otherwise u_sync() may not work properly
4384 curbuf->b_p_ul = old_value;
4385 u_sync(TRUE);
4386 curbuf->b_p_ul = value;
4387}
4388
4389/*
4390 * Process the new 'undolevels' option value.
4391 */
4392 char *
4393did_set_undolevels(optset_T *args)
4394{
4395 long *pp = (long *)args->os_varp;
4396
4397 if (pp == &p_ul) // global 'undolevels'
4398 update_global_undolevels(args->os_newval.number,
4399 args->os_oldval.number);
4400 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4401 update_buflocal_undolevels(args->os_newval.number,
4402 args->os_oldval.number);
4403
4404 return NULL;
4405}
4406
4407/*
4408 * Process the new 'updatecount' option value.
4409 */
4410 char *
4411did_set_updatecount(optset_T *args)
4412{
4413 long old_value = args->os_oldval.number;
4414 char *errmsg = NULL;
4415
4416 // when 'updatecount' changes from zero to non-zero, open swap files
4417 if (p_uc < 0)
4418 {
4419 errmsg = e_argument_must_be_positive;
4420 p_uc = 100;
4421 }
4422 if (p_uc && !old_value)
4423 ml_open_files();
4424
4425 return errmsg;
4426}
4427
4428/*
4429 * Process the updated 'weirdinvert' option value.
4430 */
4431 char *
4432did_set_weirdinvert(optset_T *args)
4433{
4434 // When 'weirdinvert' changed, set/reset 't_xs'.
4435 // Then set 'weirdinvert' according to value of 't_xs'.
4436 if (p_wiv && !args->os_oldval.boolean)
4437 T_XS = (char_u *)"y";
4438 else if (!p_wiv && args->os_oldval.boolean)
4439 T_XS = empty_option;
4440 p_wiv = (*T_XS != NUL);
4441
4442 return NULL;
4443}
4444
4445/*
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02004446 * Process the new 'wildchar' / 'wildcharm' option value.
4447 */
4448 char *
4449did_set_wildchar(optset_T *args)
4450{
4451 long c = *(long *)args->os_varp;
4452
4453 // Don't allow key values that wouldn't work as wildchar.
4454 if (c == Ctrl_C || c == '\n' || c == '\r' || c == K_KENTER)
4455 return e_invalid_argument;
4456
4457 return NULL;
4458}
4459
4460/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004461 * Process the new 'window' option value.
4462 */
4463 char *
4464did_set_window(optset_T *args UNUSED)
4465{
4466 if (p_window < 1)
4467 p_window = 1;
4468 else if (p_window >= Rows)
4469 p_window = Rows - 1;
4470 return NULL;
4471}
4472
4473/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004474 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004476 char *
4477did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004479 long *pp = (long *)args->os_varp;
4480 char *errmsg = NULL;
4481
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004482 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004484 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004485 p_wh = 1;
4486 }
4487 if (p_wmh > p_wh)
4488 {
4489 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4490 p_wh = p_wmh;
4491 }
4492 if (p_hh < 0)
4493 {
4494 errmsg = e_argument_must_be_positive;
4495 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
4497
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004498 // Change window height NOW
4499 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004501 if (pp == &p_wh && curwin->w_height < p_wh)
4502 win_setheight((int)p_wh);
4503 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4504 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004507 return errmsg;
4508}
4509
4510/*
4511 * Process the new 'winminheight' option value.
4512 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004513 char *
4514did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004515{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004516 char *errmsg = NULL;
4517
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004518 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004520 errmsg = e_argument_must_be_positive;
4521 p_wmh = 0;
4522 }
4523 if (p_wmh > p_wh)
4524 {
4525 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4526 p_wmh = p_wh;
4527 }
4528 win_setminheight();
4529
4530 return errmsg;
4531}
4532
4533/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004534 * Process the new 'winminwidth' option value.
4535 */
4536 char *
4537did_set_winminwidth(optset_T *args UNUSED)
4538{
4539 char *errmsg = NULL;
4540
4541 if (p_wmw < 0)
4542 {
4543 errmsg = e_argument_must_be_positive;
4544 p_wmw = 0;
4545 }
4546 if (p_wmw > p_wiw)
4547 {
4548 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4549 p_wmw = p_wiw;
4550 }
4551 win_setminwidth();
4552
4553 return errmsg;
4554}
4555
4556/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004557 * Process the new 'winwidth' option value.
4558 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004559 char *
4560did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004561{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004562 char *errmsg = NULL;
4563
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004564 if (p_wiw < 1)
4565 {
4566 errmsg = e_argument_must_be_positive;
4567 p_wiw = 1;
4568 }
4569 if (p_wmw > p_wiw)
4570 {
4571 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4572 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004574
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004575 // Change window width NOW
4576 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4577 win_setwidth((int)p_wiw);
4578
4579 return errmsg;
4580}
4581
4582/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004583 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004584 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004585 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004586did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004587{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004588 // Set w_leftcol or w_skipcol to zero.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004589 if (curwin->w_p_wrap)
4590 curwin->w_leftcol = 0;
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004591 else
4592 curwin->w_skipcol = 0;
4593
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004594 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004595}
4596
4597/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004598 * Set the value of a boolean option, and take care of side effects.
4599 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004600 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004601 static char *
4602set_bool_option(
4603 int opt_idx, // index in options[] table
4604 char_u *varp, // pointer to the option variable
4605 int value, // new value
4606 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004607{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004608 int old_value = *(int *)varp;
4609#if defined(FEAT_EVAL)
4610 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004612 char *errmsg = NULL;
4613
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004614 // Disallow changing some options from secure mode
4615 if ((secure
4616#ifdef HAVE_SANDBOX
4617 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004618#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004619 ) && (options[opt_idx].flags & P_SECURE))
4620 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004621
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004622#if defined(FEAT_EVAL)
4623 // Save the global value before changing anything. This is needed as for
4624 // a global-only option setting the "local value" in fact sets the global
4625 // value (since there is only one value).
4626 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4627 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4628 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004630
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004631 *(int *)varp = value; // set the new value
4632#ifdef FEAT_EVAL
4633 // Remember where the option was set.
4634 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004635#endif
4636
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004638 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004641 // May set global value for local option.
4642 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4643 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004644
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004645 // Handle side effects of changing a bool option.
4646 if (options[opt_idx].opt_did_set_cb != NULL)
4647 {
4648 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004649
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004650 CLEAR_FIELD(args);
4651 args.os_varp = varp;
4652 args.os_flags = opt_flags;
4653 args.os_oldval.boolean = old_value;
4654 args.os_newval.boolean = value;
4655 args.os_errbuf = NULL;
4656 errmsg = options[opt_idx].opt_did_set_cb(&args);
zeertzjqdeba02d2023-11-02 21:01:19 +01004657 if (errmsg != NULL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004658 return errmsg;
4659 }
4660
4661 // after handling side effects, call autocommand
4662
4663 options[opt_idx].flags |= P_WAS_SET;
4664
4665#if defined(FEAT_EVAL)
4666 apply_optionset_autocmd(opt_idx, opt_flags,
4667 (long)(old_value ? TRUE : FALSE),
4668 (long)(old_global_value ? TRUE : FALSE),
4669 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004670#endif
4671
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004672 comp_col(); // in case 'ruler' or 'showcmd' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01004673
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004674 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004675 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
4676 && (options[opt_idx].flags & P_HLONLY) == 0)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004677 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004678
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004679 if ((opt_flags & OPT_NO_REDRAW) == 0)
4680 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004681
4682 return errmsg;
4683}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004684
4685/*
4686 * Check the bounds of numeric options.
4687 */
4688 static char *
4689check_num_option_bounds(
4690 long *pp,
4691 long old_value,
4692 long old_Rows,
4693 long old_Columns,
4694 char *errbuf,
4695 size_t errbuflen,
4696 char *errmsg)
4697{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 if (Rows < min_rows() && full_screen)
4699 {
4700 if (errbuf != NULL)
4701 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004702 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004703 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 errmsg = errbuf;
4705 }
4706 Rows = min_rows();
4707 }
4708 if (Columns < MIN_COLUMNS && full_screen)
4709 {
4710 if (errbuf != NULL)
4711 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004712 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004713 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 errmsg = errbuf;
4715 }
4716 Columns = MIN_COLUMNS;
4717 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004718 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004720 // If the screen (shell) height has been changed, assume it is the
4721 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 if (old_Rows != Rows || old_Columns != Columns)
4723 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004724 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 if (updating_screen)
4726 *pp = old_value;
4727 else if (full_screen
4728#ifdef FEAT_GUI
4729 && !gui.starting
4730#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004731 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 set_shellsize((int)Columns, (int)Rows, TRUE);
4733 else
4734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004735 // Postpone the resizing; check the size and cmdline position for
4736 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 check_shellsize();
4738 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4739 cmdline_row = Rows - p_ch;
4740 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004741 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004742 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 }
4744
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 if (curbuf->b_p_ts <= 0)
4746 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004747 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 curbuf->b_p_ts = 8;
4749 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004750 else if (curbuf->b_p_ts > TABSTOP_MAX)
4751 {
4752 errmsg = e_invalid_argument;
4753 curbuf->b_p_ts = 8;
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 if (p_tm < 0)
4756 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004757 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 p_tm = 0;
4759 }
4760 if ((curwin->w_p_scr <= 0
4761 || (curwin->w_p_scr > curwin->w_height
4762 && curwin->w_height > 0))
4763 && full_screen)
4764 {
4765 if (pp == &(curwin->w_p_scr))
4766 {
4767 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004768 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 win_comp_scroll(curwin);
4770 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004771 // If 'scroll' became invalid because of a side effect silently adjust
4772 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 else if (curwin->w_p_scr <= 0)
4774 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004775 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 curwin->w_p_scr = curwin->w_height;
4777 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004778 if (p_hi < 0)
4779 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004780 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004781 p_hi = 0;
4782 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004783 else if (p_hi > 10000)
4784 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004785 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004786 p_hi = 10000;
4787 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004788 if (p_re < 0 || p_re > 2)
4789 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004790 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004791 p_re = 0;
4792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 if (p_report < 0)
4794 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004795 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 p_report = 1;
4797 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004798 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004800 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 p_sj = Rows / 2;
4802 else
4803 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004804 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 p_sj = 1;
4806 }
4807 }
4808 if (p_so < 0 && full_screen)
4809 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004810 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 p_so = 0;
4812 }
4813 if (p_siso < 0 && full_screen)
4814 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004815 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 p_siso = 0;
4817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 if (p_cwh < 1)
4819 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004820 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 p_cwh = 1;
4822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 if (p_ut < 0)
4824 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004825 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 p_ut = 2000;
4827 }
4828 if (p_ss < 0)
4829 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004830 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 p_ss = 0;
4832 }
4833
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004834 return errmsg;
4835}
4836
4837/*
4838 * Set the value of a number option, and take care of side effects.
4839 * Returns NULL for success, or an error message for an error.
4840 */
4841 static char *
4842set_num_option(
4843 int opt_idx, // index in options[] table
4844 char_u *varp, // pointer to the option variable
4845 long value, // new value
4846 char *errbuf, // buffer for error messages
4847 size_t errbuflen, // length of "errbuf"
4848 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4849 // OPT_MODELINE, etc.
4850{
4851 char *errmsg = NULL;
4852 long old_value = *(long *)varp;
4853#if defined(FEAT_EVAL)
4854 long old_global_value = 0; // only used when setting a local and
4855 // global option
4856#endif
4857 long old_Rows = Rows; // remember old Rows
4858 long old_Columns = Columns; // remember old Columns
4859 long *pp = (long *)varp;
4860
4861 // Disallow changing some options from secure mode.
4862 if ((secure
4863#ifdef HAVE_SANDBOX
4864 || sandbox != 0
4865#endif
4866 ) && (options[opt_idx].flags & P_SECURE))
4867 return e_not_allowed_here;
4868
4869#if defined(FEAT_EVAL)
4870 // Save the global value before changing anything. This is needed as for
4871 // a global-only option setting the "local value" in fact sets the global
4872 // value (since there is only one value).
4873 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4874 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4875 OPT_GLOBAL);
4876#endif
4877
4878 *pp = value;
4879#ifdef FEAT_EVAL
4880 // Remember where the option was set.
4881 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4882#endif
4883#ifdef FEAT_GUI
4884 need_mouse_correct = TRUE;
4885#endif
4886
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004887 // Invoke the option specific callback function to validate and apply the
4888 // new value.
4889 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004890 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004891 optset_T args;
4892
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004893 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004894 args.os_varp = varp;
4895 args.os_flags = opt_flags;
4896 args.os_oldval.number = old_value;
4897 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004898 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004899 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004900 }
4901
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004902 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004903 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4904 errbuf, errbuflen, errmsg);
4905
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004906 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4908 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4909
4910 options[opt_idx].flags |= P_WAS_SET;
4911
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004912#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004913 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4914 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004915#endif
4916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004917 comp_col(); // in case 'columns' or 'ls' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01004918
Bram Moolenaar913077c2012-03-28 19:59:04 +02004919 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004920 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
4921 && (options[opt_idx].flags & P_HLONLY) == 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004922 curwin->w_set_curswant = TRUE;
zeertzjqfcaed6a2024-02-18 09:33:54 +01004923
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004924 if ((opt_flags & OPT_NO_REDRAW) == 0)
4925 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
4927 return errmsg;
4928}
4929
4930/*
4931 * Called after an option changed: check if something needs to be redrawn.
4932 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004933 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004934check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004936 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004937 int doclear = (flags & P_RCLR) == P_RCLR;
4938 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004940 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942
4943 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
zeertzjqfcaed6a2024-02-18 09:33:54 +01004944 {
4945 if (flags & P_HLONLY)
4946 redraw_later(UPD_NOT_VALID);
4947 else
4948 changed_window_setting();
4949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004951 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004952 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004953 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004955 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956}
4957
4958/*
4959 * Find index for option 'arg'.
4960 * Return -1 if not found.
4961 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004962 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004963findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
4965 int opt_idx;
4966 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004967 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 int is_term_opt;
4969
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004970 // For first call: Initialize the quick-access table.
4971 // It contains the index for the first option that starts with a certain
4972 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if (quick_tab[1] == 0)
4974 {
4975 p = options[0].fullname;
4976 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4977 {
4978 if (s[0] != p[0])
4979 {
4980 if (s[0] == 't' && s[1] == '_')
4981 quick_tab[26] = opt_idx;
4982 else
4983 quick_tab[CharOrdLow(s[0])] = opt_idx;
4984 }
4985 p = s;
4986 }
4987 }
4988
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004989 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 return -1;
4992
4993 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4994 if (is_term_opt)
4995 opt_idx = quick_tab[26];
4996 else
4997 opt_idx = quick_tab[CharOrdLow(arg[0])];
zeertzjqf48558e2023-12-08 21:34:31 +01004998 for (; (s = options[opt_idx].fullname) != NULL && s[0] == arg[0]; opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005000 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 break;
5002 }
zeertzjqf48558e2023-12-08 21:34:31 +01005003 if (s != NULL && s[0] != arg[0])
5004 s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 if (s == NULL && !is_term_opt)
5006 {
5007 opt_idx = quick_tab[CharOrdLow(arg[0])];
5008 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
5009 {
5010 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005011 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 break;
5013 s = NULL;
5014 }
5015 }
5016 if (s == NULL)
5017 opt_idx = -1;
5018 return opt_idx;
5019}
5020
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005021#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
5022 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023/*
5024 * Get the value for an option.
5025 *
5026 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005027 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01005028 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005029 * String option: gov_string, *stringval gets allocated string.
5030 * Hidden Number option: gov_hidden_number.
5031 * Hidden Toggle option: gov_hidden_bool.
5032 * Hidden String option: gov_hidden_string.
5033 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005034 *
5035 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005037 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01005038get_option_value(
5039 char_u *name,
5040 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005041 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005042 int *flagsp,
5043 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044{
5045 int opt_idx;
5046 char_u *varp;
5047
5048 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005049 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01005050 {
5051 int key;
5052
5053 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005054 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005055 {
5056 char_u key_name[2];
5057 char_u *p;
5058
Bram Moolenaar10c75c42021-12-28 20:53:30 +00005059 if (flagsp != NULL)
5060 *flagsp = 0; // terminal option has no flags
5061
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005062 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01005063 if (key < 0)
5064 {
5065 key_name[0] = KEY2TERMCAP0(key);
5066 key_name[1] = KEY2TERMCAP1(key);
5067 }
5068 else
5069 {
5070 key_name[0] = KS_KEY;
5071 key_name[1] = (key & 0xff);
5072 }
5073 p = find_termcode(key_name);
5074 if (p != NULL)
5075 {
5076 if (stringval != NULL)
5077 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005078 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01005079 }
5080 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005081 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01005082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005084 varp = get_varp_scope(&(options[opt_idx]), scope);
5085
5086 if (flagsp != NULL)
5087 // Return the P_xxxx option flags.
5088 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089
5090 if (options[opt_idx].flags & P_STRING)
5091 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005092 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005093 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if (stringval != NULL)
5095 {
zeertzjq51f0bc32022-05-09 13:33:39 +01005096 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005097 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5098 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005100 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005101 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005102 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005105 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 *stringval = vim_strsave(*(char_u **)(varp));
5107 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005108 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
5110
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005111 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005112 return (options[opt_idx].flags & P_NUM)
5113 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (options[opt_idx].flags & P_NUM)
5115 *numval = *(long *)varp;
5116 else
5117 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005118 // Special case: 'modified' is b_changed, but we also want to consider
5119 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 if ((int *)varp == &curbuf->b_changed)
5121 *numval = curbufIsChanged();
5122 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005123 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005125 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126}
5127#endif
5128
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005129#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005130/*
5131 * Returns the option attributes and its value. Unlike the above function it
5132 * will return either global value or local value of the option depending on
5133 * what was requested, but it will never return global value if it was
5134 * requested to return local one and vice versa. Neither it will return
5135 * buffer-local value if it was requested to return window-local one.
5136 *
5137 * Pretends that option is absent if it is not present in the requested scope
5138 * (i.e. has no global, window-local or buffer-local value depending on
5139 * opt_type). Uses
5140 *
5141 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005142 * 0 hidden or unknown option, also option that does not have requested
5143 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005144 * see SOPT_* in vim.h for other flags
5145 *
5146 * Possible opt_type values: see SREQ_* in vim.h
5147 */
5148 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005149get_option_value_strict(
5150 char_u *name,
5151 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005152 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005153 int opt_type,
5154 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005155{
5156 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005157 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005158 struct vimoption *p;
5159 int r = 0;
5160
5161 opt_idx = findoption(name);
5162 if (opt_idx < 0)
5163 return 0;
5164
5165 p = &(options[opt_idx]);
5166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005167 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005168 if (p->var == NULL)
5169 return 0;
5170
5171 if (p->flags & P_BOOL)
5172 r |= SOPT_BOOL;
5173 else if (p->flags & P_NUM)
5174 r |= SOPT_NUM;
5175 else if (p->flags & P_STRING)
5176 r |= SOPT_STRING;
5177
5178 if (p->indir == PV_NONE)
5179 {
5180 if (opt_type == SREQ_GLOBAL)
5181 r |= SOPT_GLOBAL;
5182 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005183 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005184 }
5185 else
5186 {
5187 if (p->indir & PV_BOTH)
5188 r |= SOPT_GLOBAL;
5189 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005190 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005191
5192 if (p->indir & PV_WIN)
5193 {
5194 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005195 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005196 else
5197 r |= SOPT_WIN;
5198 }
5199 else if (p->indir & PV_BUF)
5200 {
5201 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005202 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005203 else
5204 r |= SOPT_BUF;
5205 }
5206 }
5207
5208 if (stringval == NULL)
5209 return r;
5210
5211 if (opt_type == SREQ_GLOBAL)
5212 varp = p->var;
5213 else
5214 {
5215 if (opt_type == SREQ_BUF)
5216 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005217 // Special case: 'modified' is b_changed, but we also want to
5218 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005219 if (p->indir == PV_MOD)
5220 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005221 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005222 varp = NULL;
5223 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005224# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005225 else if (p->indir == PV_KEY)
5226 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005227 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005228 *stringval = NULL;
5229 varp = NULL;
5230 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005231# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005232 else
5233 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005234 buf_T *save_curbuf = curbuf;
5235
5236 // only getting a pointer, no need to use aucmd_prepbuf()
5237 curbuf = (buf_T *)from;
5238 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005239 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005240 curbuf = save_curbuf;
5241 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005242 }
5243 }
5244 else if (opt_type == SREQ_WIN)
5245 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005246 win_T *save_curwin = curwin;
5247
5248 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005249 curbuf = curwin->w_buffer;
5250 varp = get_varp(p);
5251 curwin = save_curwin;
5252 curbuf = curwin->w_buffer;
5253 }
5254 if (varp == p->var)
5255 return (r | SOPT_UNSET);
5256 }
5257
5258 if (varp != NULL)
5259 {
5260 if (p->flags & P_STRING)
5261 *stringval = vim_strsave(*(char_u **)(varp));
5262 else if (p->flags & P_NUM)
5263 *numval = *(long *) varp;
5264 else
5265 *numval = *(int *)varp;
5266 }
5267
5268 return r;
5269}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005270
5271/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005272 * Iterate over options. First argument is a pointer to a pointer to a
5273 * structure inside options[] array, second is option type like in the above
5274 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005275 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005276 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005277 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005278 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005279 * first argument to NULL.
5280 *
5281 * Returns full option name for current option on each call.
5282 */
5283 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005284option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005285{
5286 struct vimoption *ret = NULL;
5287 do
5288 {
5289 if (*option == NULL)
5290 *option = (void *) options;
5291 else if (((struct vimoption *) (*option))->fullname == NULL)
5292 {
5293 *option = NULL;
5294 return NULL;
5295 }
5296 else
5297 *option = (void *) (((struct vimoption *) (*option)) + 1);
5298
5299 ret = ((struct vimoption *) (*option));
5300
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005301 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005302 if (ret->var == NULL)
5303 {
5304 ret = NULL;
5305 continue;
5306 }
5307
5308 switch (opt_type)
5309 {
5310 case SREQ_GLOBAL:
5311 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5312 ret = NULL;
5313 break;
5314 case SREQ_BUF:
5315 if (!(ret->indir & PV_BUF))
5316 ret = NULL;
5317 break;
5318 case SREQ_WIN:
5319 if (!(ret->indir & PV_WIN))
5320 ret = NULL;
5321 break;
5322 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005323 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005324 return NULL;
5325 }
5326 }
5327 while (ret == NULL);
5328
5329 return (char_u *)ret->fullname;
5330}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005331#endif
5332
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005334 * Return the flags for the option at 'opt_idx'.
5335 */
5336 long_u
5337get_option_flags(int opt_idx)
5338{
5339 return options[opt_idx].flags;
5340}
5341
5342/*
5343 * Set a flag for the option at 'opt_idx'.
5344 */
5345 void
5346set_option_flag(int opt_idx, long_u flag)
5347{
5348 options[opt_idx].flags |= flag;
5349}
5350
5351/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005352 * Returns TRUE if the option at 'opt_idx' is a global option
5353 */
5354 int
5355is_global_option(int opt_idx)
5356{
5357 return options[opt_idx].indir == PV_NONE;
5358}
5359
5360/*
5361 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5362 * local value.
5363 */
5364 int
5365is_global_local_option(int opt_idx)
5366{
5367 return options[opt_idx].indir & PV_BOTH;
5368}
5369
5370/*
5371 * Returns TRUE if the option at 'opt_idx' is a window-local option
5372 */
5373 int
5374is_window_local_option(int opt_idx)
5375{
5376 return options[opt_idx].var == VAR_WIN;
5377}
5378
5379/*
5380 * Returns TRUE if the option at 'opt_idx' is a hidden option
5381 */
5382 int
5383is_hidden_option(int opt_idx)
5384{
5385 return options[opt_idx].var == NULL;
5386}
5387
5388#if defined(FEAT_CRYPT) || defined(PROTO)
5389/*
5390 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5391 */
5392 int
5393is_crypt_key_option(int opt_idx)
5394{
5395 return options[opt_idx].indir == PV_KEY;
5396}
5397#endif
5398
5399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 * Set the value of option "name".
5401 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005402 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005403 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005405 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005406set_option_value(
5407 char_u *name,
5408 long number,
5409 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005410 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 int opt_idx;
5413 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005414 long_u flags;
Christian Brabandtb39b2402023-11-29 11:34:05 +01005415 static char errbuf[ERR_BUFLEN];
5416 int errbuflen = ERR_BUFLEN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417
5418 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005419 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005420 {
5421 int key;
5422
5423 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005424 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005425 {
5426 char_u key_name[2];
5427
5428 if (key < 0)
5429 {
5430 key_name[0] = KEY2TERMCAP0(key);
5431 key_name[1] = KEY2TERMCAP1(key);
5432 }
5433 else
5434 {
5435 key_name[0] = KS_KEY;
5436 key_name[1] = (key & 0xff);
5437 }
5438 add_termcode(key_name, string, FALSE);
5439 if (full_screen)
5440 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005441 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005442 return NULL;
5443 }
5444
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005445 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 else
5448 {
5449 flags = options[opt_idx].flags;
5450#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005451 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005453 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005454 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005455 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005458 if (flags & P_STRING)
Christian Brabandtb39b2402023-11-29 11:34:05 +01005459 return set_string_option(opt_idx, string, opt_flags, errbuf, errbuflen);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005460
5461 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5462 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005464 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005466 int idx;
5467
5468 // Either we are given a string or we are setting option
5469 // to zero.
5470 for (idx = 0; string[idx] == '0'; ++idx)
5471 ;
5472 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005473 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005474 // There's another character after zeros or the string
5475 // is empty. In both cases, we are trying to set a
5476 // num option using a string.
5477 semsg(_(e_number_required_after_str_equal_str),
5478 name, string);
5479 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005480
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005483 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005484 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005485 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005486 errbuf, sizeof(errbuf), opt_flags);
5487 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005488 else
5489 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005491
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005493 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494}
5495
5496/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005497 * Call set_option_value() and when an error is returned report it.
5498 */
5499 void
5500set_option_value_give_err(
5501 char_u *name,
5502 long number,
5503 char_u *string,
5504 int opt_flags) // OPT_LOCAL or 0 (both)
5505{
5506 char *errmsg = set_option_value(name, number, string, opt_flags);
5507
5508 if (errmsg != NULL)
5509 emsg(_(errmsg));
5510}
5511
5512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 * Get the terminal code for a terminal option.
5514 * Returns NULL when not found.
5515 */
5516 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005517get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 int opt_idx;
5520 char_u *varp;
5521
5522 if (tname[0] != 't' || tname[1] != '_' ||
5523 tname[2] == NUL || tname[3] == NUL)
5524 return NULL;
5525 if ((opt_idx = findoption(tname)) >= 0)
5526 {
5527 varp = get_varp(&(options[opt_idx]));
5528 if (varp != NULL)
5529 varp = *(char_u **)(varp);
5530 return varp;
5531 }
5532 return find_termcode(tname + 2);
5533}
5534
5535 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005536get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
5538 int i;
5539
5540 i = findoption((char_u *)"hl");
5541 if (i >= 0)
5542 return options[i].def_val[VI_DEFAULT];
5543 return (char_u *)NULL;
5544}
5545
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005546 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005547get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005548{
5549 int i;
5550
5551 i = findoption((char_u *)"enc");
5552 if (i >= 0)
5553 return options[i].def_val[VI_DEFAULT];
5554 return (char_u *)NULL;
5555}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005556
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005557#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005558 int
5559is_option_allocated(char *name)
5560{
5561 int idx = findoption((char_u *)name);
5562
5563 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5564}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005565#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567/*
5568 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005569 * When "has_lt" is true there is a '<' before "*arg_arg".
5570 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 */
5572 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005573find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005575 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005577 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005579 // Don't use get_special_key_code() for t_xx, we don't want it to call
5580 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5582 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005583 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005585 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005587 key = find_special_key(&arg, &modifiers,
5588 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005589 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 key = 0;
5591 }
5592 return key;
5593}
5594
5595/*
5596 * if 'all' == 0: show changed options
5597 * if 'all' == 1: show all normal options
5598 * if 'all' == 2: show all terminal options
5599 */
5600 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005601showoptions(
5602 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005603 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604{
5605 struct vimoption *p;
5606 int col;
5607 int isterm;
5608 char_u *varp;
5609 struct vimoption **items;
5610 int item_count;
5611 int run;
5612 int row, rows;
5613 int cols;
5614 int i;
5615 int len;
5616
5617#define INC 20
5618#define GAP 3
5619
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005620 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 if (items == NULL)
5622 return;
5623
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005624 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005626 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005628 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005630 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005632 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005634 // Do the loop two times:
5635 // 1. display the short items
5636 // 2. display the long items (only strings and numbers)
5637 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 for (run = 1; run <= 2 && !got_int; ++run)
5639 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005640 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 item_count = 0;
5642 for (p = &options[0]; p->fullname != NULL; p++)
5643 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005644 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005645 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005646 continue;
5647
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 varp = NULL;
5649 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005650 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 {
5652 if (p->indir != PV_NONE && !isterm)
5653 varp = get_varp_scope(p, opt_flags);
5654 }
5655 else
5656 varp = get_varp(p);
5657 if (varp != NULL
5658 && ((all == 2 && isterm)
5659 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005660 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005662 if (opt_flags & OPT_ONECOLUMN)
5663 len = Columns;
5664 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005665 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 else
5667 {
5668 option_value2string(p, opt_flags);
5669 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5670 }
5671 if ((len <= INC - GAP && run == 1) ||
5672 (len > INC - GAP && run == 2))
5673 items[item_count++] = p;
5674 }
5675 }
5676
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005677 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 if (run == 1)
5679 {
5680 cols = (Columns + GAP - 3) / INC;
5681 if (cols == 0)
5682 cols = 1;
5683 rows = (item_count + cols - 1) / cols;
5684 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005685 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 rows = item_count;
5687 for (row = 0; row < rows && !got_int; ++row)
5688 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005689 msg_putchar('\n'); // go to next line
5690 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 break;
5692 col = 0;
5693 for (i = row; i < item_count; i += rows)
5694 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005695 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 showoneopt(items[i], opt_flags);
5697 col += INC;
5698 }
5699 out_flush();
5700 ui_breakcheck();
5701 }
5702 }
5703 vim_free(items);
5704}
5705
5706/*
5707 * Return TRUE if option "p" has its default value.
5708 */
5709 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005710optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
5712 int dvi;
5713
5714 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005715 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005716 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005718 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005720 // the cast to long is required for Manx C, long_i is
5721 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005722 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005723 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5725}
5726
5727/*
5728 * showoneopt: show the value of one option
5729 * must not be called with a hidden option!
5730 */
5731 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005732showoneopt(
5733 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005734 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005736 char_u *varp;
5737 int save_silent = silent_mode;
5738
5739 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005740 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
5742 varp = get_varp_scope(p, opt_flags);
5743
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005744 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5746 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005747 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005749 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005751 msg_puts(" ");
5752 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 if (!(p->flags & P_BOOL))
5754 {
5755 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005756 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 option_value2string(p, opt_flags);
5758 msg_outtrans(NameBuff);
5759 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005760
5761 silent_mode = save_silent;
5762 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763}
5764
5765/*
5766 * Write modified options as ":set" commands to a file.
5767 *
5768 * There are three values for "opt_flags":
5769 * OPT_GLOBAL: Write global option values and fresh values of
5770 * buffer-local options (used for start of a session
5771 * file).
5772 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5773 * curwin (used for a vimrc file).
5774 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5775 * and local values for window-local options of
5776 * curwin. Local values are also written when at the
5777 * default value, because a modeline or autocommand
5778 * may have set them when doing ":edit file" and the
5779 * user has set them back at the default or fresh
5780 * value.
5781 * When "local_only" is TRUE, don't write fresh
5782 * values, only local values (for ":mkview").
5783 * (fresh value = value used for a new buffer or window for a local option).
5784 *
5785 * Return FAIL on error, OK otherwise.
5786 */
5787 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005788makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
5790 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005791 char_u *varp; // currently used value
5792 char_u *varp_fresh; // local value
5793 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 char *cmd;
5795 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005796 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
5798 /*
5799 * The options that don't have a default (terminal name, columns, lines)
5800 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005801 * Do the loop over "options[]" twice: once for options with the
5802 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005804 for (pri = 1; pri >= 0; --pri)
5805 {
5806 for (p = &options[0]; !istermoption(p); p++)
5807 if (!(p->flags & P_NO_MKRC)
5808 && !istermoption(p)
5809 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005811 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5813 continue;
5814
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005815 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5816 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5818 continue;
5819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005820 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005822 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 continue;
5824
Bram Moolenaard23b7142021-04-17 21:04:34 +02005825 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5826 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005827 continue;
5828
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 round = 2;
5830 if (p->indir != PV_NONE)
5831 {
5832 if (p->var == VAR_WIN)
5833 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005834 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 if (!(opt_flags & OPT_LOCAL))
5836 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005837 // When fresh value of window-local option is not at the
5838 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5840 {
5841 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005842 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 {
5844 round = 1;
5845 varp_local = varp;
5846 varp = varp_fresh;
5847 }
5848 }
5849 }
5850 }
5851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005852 // Round 1: fresh value for window-local options.
5853 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 for ( ; round <= 2; varp = varp_local, ++round)
5855 {
5856 if (round == 1 || (opt_flags & OPT_GLOBAL))
5857 cmd = "set";
5858 else
5859 cmd = "setlocal";
5860
5861 if (p->flags & P_BOOL)
5862 {
5863 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5864 return FAIL;
5865 }
5866 else if (p->flags & P_NUM)
5867 {
5868 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5869 return FAIL;
5870 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005871 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005873 int do_endif = FALSE;
5874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005875 // Don't set 'syntax' and 'filetype' again if the value is
5876 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005877 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005878#if defined(FEAT_SYN_HL)
5879 p->indir == PV_SYN ||
5880#endif
5881 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {
5883 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5884 *(char_u **)(varp)) < 0
5885 || put_eol(fd) < 0)
5886 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005887 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 }
5889 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005890 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005892 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 {
5894 if (put_line(fd, "endif") == FAIL)
5895 return FAIL;
5896 }
5897 }
5898 }
5899 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 return OK;
5902}
5903
5904#if defined(FEAT_FOLDING) || defined(PROTO)
5905/*
5906 * Generate set commands for the local fold options only. Used when
5907 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5908 */
5909 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005910makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005912 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005914 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 == FAIL
5916# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005917 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005919 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 == FAIL
5921 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5922 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5923 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5924 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5925 )
5926 return FAIL;
5927
5928 return OK;
5929}
5930#endif
5931
5932 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005933put_setstring(
5934 FILE *fd,
5935 char *cmd,
5936 char *name,
5937 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005938 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939{
5940 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005941 char_u *buf = NULL;
5942 char_u *part = NULL;
5943 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
5945 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5946 return FAIL;
5947 if (*valuep != NULL)
5948 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005949 // Output 'pastetoggle' as key names. For other
5950 // options some characters have to be escaped with
5951 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 if (valuep == &p_pt)
5953 {
5954 s = *valuep;
5955 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005956 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 return FAIL;
5958 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005959 // expand the option value, replace $HOME by ~
5960 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005962 int size = (int)STRLEN(*valuep) + 1;
5963
5964 // replace home directory in the whole option value into "buf"
5965 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005966 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005967 goto fail;
5968 home_replace(NULL, *valuep, buf, size, FALSE);
5969
5970 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005971 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005972 // can be expanded when read back.
5973 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5974 && vim_strchr(*valuep, ',') != NULL)
5975 {
5976 part = alloc(size);
5977 if (part == NULL)
5978 goto fail;
5979
5980 // write line break to clear the option, e.g. ':set rtp='
5981 if (put_eol(fd) == FAIL)
5982 goto fail;
5983
5984 p = buf;
5985 while (*p != NUL)
5986 {
5987 // for each comma separated option part, append value to
5988 // the option, :set rtp+=value
5989 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5990 goto fail;
5991 (void)copy_option_part(&p, part, size, ",");
5992 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5993 goto fail;
5994 }
5995 vim_free(buf);
5996 vim_free(part);
5997 return OK;
5998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02006000 {
6001 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02006003 }
6004 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 }
6006 else if (put_escstr(fd, *valuep, 2) == FAIL)
6007 return FAIL;
6008 }
6009 if (put_eol(fd) < 0)
6010 return FAIL;
6011 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006012fail:
6013 vim_free(buf);
6014 vim_free(part);
6015 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016}
6017
6018 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006019put_setnum(
6020 FILE *fd,
6021 char *cmd,
6022 char *name,
6023 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024{
6025 long wc;
6026
6027 if (fprintf(fd, "%s %s=", cmd, name) < 0)
6028 return FAIL;
6029 if (wc_use_keyname((char_u *)valuep, &wc))
6030 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006031 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
6033 return FAIL;
6034 }
6035 else if (fprintf(fd, "%ld", *valuep) < 0)
6036 return FAIL;
6037 if (put_eol(fd) < 0)
6038 return FAIL;
6039 return OK;
6040}
6041
6042 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006043put_setbool(
6044 FILE *fd,
6045 char *cmd,
6046 char *name,
6047 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006049 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00006050 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
6052 || put_eol(fd) < 0)
6053 return FAIL;
6054 return OK;
6055}
6056
6057/*
6058 * Clear all the terminal options.
6059 * If the option has been allocated, free the memory.
6060 * Terminal options are never hidden or indirect.
6061 */
6062 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006063clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006065 // Reset a few things before clearing the old options. This may cause
6066 // outputting a few things that the terminal doesn't understand, but the
6067 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006068 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006069 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006071 // When starting the GUI close the display opened for the clipboard.
6072 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 if (gui.starting)
6074 clear_xterm_clip();
6075#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006076 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006078 free_termoptions();
6079}
6080
6081 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006082free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006083{
6084 struct vimoption *p;
6085
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006086 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 if (istermoption(p))
6088 {
6089 if (p->flags & P_ALLOCED)
6090 free_string_option(*(char_u **)(p->var));
6091 if (p->flags & P_DEF_ALLOCED)
6092 free_string_option(p->def_val[VI_DEFAULT]);
6093 *(char_u **)(p->var) = empty_option;
6094 p->def_val[VI_DEFAULT] = empty_option;
6095 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006096#ifdef FEAT_EVAL
6097 // remember where the option was cleared
6098 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 }
6101 clear_termcodes();
6102}
6103
6104/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006105 * Free the string for one term option, if it was allocated.
6106 * Set the string to empty_option and clear allocated flag.
6107 * "var" points to the option value.
6108 */
6109 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006110free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006111{
6112 struct vimoption *p;
6113
6114 for (p = &options[0]; p->fullname != NULL; p++)
6115 if (p->var == var)
6116 {
6117 if (p->flags & P_ALLOCED)
6118 free_string_option(*(char_u **)(p->var));
6119 *(char_u **)(p->var) = empty_option;
6120 p->flags &= ~P_ALLOCED;
6121 break;
6122 }
6123}
6124
6125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 * Set the terminal option defaults to the current value.
6127 * Used after setting the terminal name.
6128 */
6129 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006130set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131{
6132 struct vimoption *p;
6133
6134 for (p = &options[0]; p->fullname != NULL; p++)
6135 {
6136 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6137 {
6138 if (p->flags & P_DEF_ALLOCED)
6139 {
6140 free_string_option(p->def_val[VI_DEFAULT]);
6141 p->flags &= ~P_DEF_ALLOCED;
6142 }
6143 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6144 if (p->flags & P_ALLOCED)
6145 {
6146 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006147 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
6149 }
6150 }
6151}
6152
6153/*
6154 * return TRUE if 'p' starts with 't_'
6155 */
6156 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006157istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158{
6159 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6160}
6161
Bram Moolenaardac13472019-09-16 21:06:21 +02006162/*
6163 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6164 */
6165 int
6166istermoption_idx(int opt_idx)
6167{
6168 return istermoption(&options[opt_idx]);
6169}
6170
Bram Moolenaar113e1072019-01-20 15:30:40 +01006171#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006173 * Unset local option value, similar to ":set opt<".
6174 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006175 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006176unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006177{
6178 struct vimoption *p;
6179 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006180 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006181
6182 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006183 if (opt_idx < 0)
6184 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006185 p = &(options[opt_idx]);
6186
6187 switch ((int)p->indir)
6188 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006189 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006190 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006191 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006192 break;
6193 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006194 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006195 break;
6196 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006197 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006198 break;
6199 case PV_AR:
6200 buf->b_p_ar = -1;
6201 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006202 case PV_BKC:
6203 clear_string_option(&buf->b_p_bkc);
6204 buf->b_bkc_flags = 0;
6205 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006206 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006207 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006208 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006209 case PV_TC:
6210 clear_string_option(&buf->b_p_tc);
6211 buf->b_tc_flags = 0;
6212 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006213 case PV_SISO:
6214 curwin->w_p_siso = -1;
6215 break;
6216 case PV_SO:
6217 curwin->w_p_so = -1;
6218 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006219# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006220 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006221 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006222 break;
6223 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006224 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006225 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006226# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006227 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006228 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006229 break;
6230 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006231 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006232 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006233# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006234 case PV_TSRFU:
6235 clear_string_option(&buf->b_p_tsrfu);
6236 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006237# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006238 case PV_FP:
6239 clear_string_option(&buf->b_p_fp);
6240 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006241# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006242 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006243 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006244 break;
6245 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006246 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006247 break;
6248 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006249 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006250 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006251# endif
6252# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006253 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006254 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006255 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006256# endif
6257# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006258 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006259 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006260 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006261# endif
6262# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006263 case PV_SBR:
6264 clear_string_option(&((win_T *)from)->w_p_sbr);
6265 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006266# endif
6267# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006268 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006269 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006270 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006271# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006272 case PV_UL:
6273 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6274 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006275 case PV_LW:
6276 clear_string_option(&buf->b_p_lw);
6277 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006278 case PV_MENC:
6279 clear_string_option(&buf->b_p_menc);
6280 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006281 case PV_LCS:
6282 clear_string_option(&((win_T *)from)->w_p_lcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006283 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE,
6284 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006285 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006286 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006287 case PV_FCS:
6288 clear_string_option(&((win_T *)from)->w_p_fcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006289 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE,
6290 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006291 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006292 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006293 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006294 clear_string_option(&((win_T *)from)->w_p_ve);
6295 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006296 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006297 }
6298}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006299#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006300
6301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006303 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 */
6305 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006306get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006308 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 {
6310 if (p->var == VAR_WIN)
6311 return (char_u *)GLOBAL_WO(get_varp(p));
6312 return p->var;
6313 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006314 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 {
6316 switch ((int)p->indir)
6317 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006318 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006320 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6321 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6322 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006324 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6325 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6326 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006327 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006328 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006329 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006330 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6331 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006333 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6334 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006336 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6337 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006338#ifdef FEAT_COMPL_FUNC
6339 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6340#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006341#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6342 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6343#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006344#if defined(FEAT_CRYPT)
6345 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6346#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006347#ifdef FEAT_LINEBREAK
6348 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6349#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006350#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006351 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006352#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006353 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006354 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006355 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006356 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006357 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006358 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006359 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006360
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006362 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 }
6364 return get_varp(p);
6365}
6366
6367/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006368 * Get pointer to option variable at 'opt_idx', depending on local or global
6369 * scope.
6370 */
6371 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006372get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006373{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006374 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006375}
6376
6377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 * Get pointer to option variable.
6379 */
6380 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006381get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006383 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 if (p->var == NULL)
6385 return NULL;
6386
6387 switch ((int)p->indir)
6388 {
6389 case PV_NONE: return p->var;
6390
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006391 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006392 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006394 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006396 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006398 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006400 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006402 case PV_TC: return *curbuf->b_p_tc != NUL
6403 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006404 case PV_BKC: return *curbuf->b_p_bkc != NUL
6405 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006406 case PV_SISO: return curwin->w_p_siso >= 0
6407 ? (char_u *)&(curwin->w_p_siso) : p->var;
6408 case PV_SO: return curwin->w_p_so >= 0
6409 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006411 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006413 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6415#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006416 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006418 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006420#ifdef FEAT_COMPL_FUNC
6421 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6422 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6423#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006424 case PV_FP: return *curbuf->b_p_fp != NUL
6425 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006427 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006429 case PV_GP: return *curbuf->b_p_gp != NUL
6430 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6431 case PV_MP: return *curbuf->b_p_mp != NUL
6432 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006434#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6435 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6436 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6437#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006438#if defined(FEAT_CRYPT)
6439 case PV_CM: return *curbuf->b_p_cm != NUL
6440 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6441#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006442#ifdef FEAT_LINEBREAK
6443 case PV_SBR: return *curwin->w_p_sbr != NUL
6444 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6445#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006446#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006447 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006448 ? (char_u *)&(curwin->w_p_stl) : p->var;
6449#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006450 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6451 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006452 case PV_LW: return *curbuf->b_p_lw != NUL
6453 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006454 case PV_MENC: return *curbuf->b_p_menc != NUL
6455 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456#ifdef FEAT_ARABIC
6457 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6458#endif
6459 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006460 case PV_LCS: return *curwin->w_p_lcs != NUL
6461 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006462 case PV_FCS: return *curwin->w_p_fcs != NUL
6463 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006464 case PV_VE: return *curwin->w_p_ve != NUL
6465 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006466#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006467 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6468#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006469#ifdef FEAT_SYN_HL
6470 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6471 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006472 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006473 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475#ifdef FEAT_DIFF
6476 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6477#endif
6478#ifdef FEAT_FOLDING
6479 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6480 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6481 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6482 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6483 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6484 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6485 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6486# ifdef FEAT_EVAL
6487 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6488 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6489# endif
6490 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6491#endif
6492 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006493 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006494#ifdef FEAT_LINEBREAK
6495 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6496#endif
Colin Kennedy21570352024-03-03 16:16:47 +01006497 case PV_WFB: return (char_u *)&(curwin->w_p_wfb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006499 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006500#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6502#endif
6503#ifdef FEAT_RIGHTLEFT
6504 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6505 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6506#endif
6507 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006508 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6510#ifdef FEAT_LINEBREAK
6511 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006512 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6513 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006515 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006517 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006518#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006519 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6520 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6521#endif
6522#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006523 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6524 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6525 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527
6528 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6529 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6532 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6534 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6536 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6537 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006538 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541#ifdef FEAT_FOLDING
6542 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006545#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006546 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006548#ifdef FEAT_COMPL_FUNC
6549 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006550 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006551#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006552#ifdef FEAT_EVAL
6553 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6554#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006555 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006557 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006563 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6565 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6566 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6567 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6568#ifdef FEAT_FIND_ID
6569# ifdef FEAT_EVAL
6570 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6571# endif
6572#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006573#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6575 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6576#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006577#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006578 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580#ifdef FEAT_CRYPT
6581 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006584 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6586 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6587 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6588 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6589 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006591 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6598#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006599 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006600 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6601#endif
6602#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006603 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6604 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6605 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006606 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607#endif
6608 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6609 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6610 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6611 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006612#ifdef FEAT_PERSISTENT_UNDO
6613 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6616#ifdef FEAT_KEYMAP
6617 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6618#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006619#ifdef FEAT_SIGNS
6620 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6621#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006622#ifdef FEAT_VARTABS
6623 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6624 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6625#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006626 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006628 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 return (char_u *)&(curbuf->b_p_wm);
6630}
6631
6632/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006633 * Return a pointer to the variable for option at 'opt_idx'
6634 */
6635 char_u *
6636get_option_var(int opt_idx)
6637{
6638 return options[opt_idx].var;
6639}
6640
Dominique Pelle748b3082022-01-08 12:41:16 +00006641#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006642/*
6643 * Return the full name of the option at 'opt_idx'
6644 */
6645 char_u *
6646get_option_fullname(int opt_idx)
6647{
6648 return (char_u *)options[opt_idx].fullname;
6649}
Dominique Pelle748b3082022-01-08 12:41:16 +00006650#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006651
6652/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006653 * Return the did_set callback function for the option at 'opt_idx'
6654 */
6655 opt_did_set_cb_T
6656get_option_did_set_cb(int opt_idx)
6657{
6658 return options[opt_idx].opt_did_set_cb;
6659}
6660
6661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 * Get the value of 'equalprg', either the buffer-local one or the global one.
6663 */
6664 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006665get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
6667 if (*curbuf->b_p_ep == NUL)
6668 return p_ep;
6669 return curbuf->b_p_ep;
6670}
6671
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672/*
6673 * Copy options from one window to another.
6674 * Used when splitting a window.
6675 */
6676 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006677win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678{
6679 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6680 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006681 after_copy_winopt(wp_to);
6682}
6683
6684/*
6685 * After copying window options: update variables depending on options.
6686 */
6687 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006688after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006689{
6690#ifdef FEAT_LINEBREAK
6691 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006692#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006693#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006694 fill_culopt_flags(NULL, wp);
6695 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006696#endif
zeertzjq6a8d2e12024-01-17 20:54:49 +01006697 set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
6698 set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006699}
6700
6701 static char_u *
6702copy_option_val(char_u *val)
6703{
6704 if (val == empty_option)
6705 return empty_option; // no need to allocate memory
6706 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708
6709/*
6710 * Copy the options from one winopt_T to another.
6711 * Doesn't free the old option values in "to", use clear_winopt() for that.
6712 * The 'scroll' option is not copied, because it depends on the window height.
6713 * The 'previewwindow' option is reset, there can be only one preview window.
6714 */
6715 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006716copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717{
6718#ifdef FEAT_ARABIC
6719 to->wo_arab = from->wo_arab;
6720#endif
6721 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006722 to->wo_lcs = copy_option_val(from->wo_lcs);
6723 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006725 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006726 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006727 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006728#ifdef FEAT_LINEBREAK
6729 to->wo_nuw = from->wo_nuw;
6730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731#ifdef FEAT_RIGHTLEFT
6732 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006733 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006735#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006736 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006737#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006738#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006739 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006742#ifdef FEAT_DIFF
6743 to->wo_wrap_save = from->wo_wrap_save;
6744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745#ifdef FEAT_LINEBREAK
6746 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006747 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006748 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006750 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006752 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006753 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006754 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006755 to->wo_crb_save = from->wo_crb_save;
Christian Brabandt4a8eb6e2023-08-13 19:43:42 +02006756 to->wo_siso = from->wo_siso;
6757 to->wo_so = from->wo_so;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006758#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006759 to->wo_spell = from->wo_spell;
6760#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006761#ifdef FEAT_SYN_HL
6762 to->wo_cuc = from->wo_cuc;
6763 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006764 to->wo_culopt = copy_option_val(from->wo_culopt);
6765 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767#ifdef FEAT_DIFF
6768 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006769 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006771#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006772 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006773 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006774#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006775#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006776 to->wo_twk = copy_option_val(from->wo_twk);
6777 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779#ifdef FEAT_FOLDING
6780 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006781 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006783 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006784 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 to->wo_fml = from->wo_fml;
6786 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006787 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006788 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006789 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006790 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 to->wo_fdn = from->wo_fdn;
6792# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006793 to->wo_fde = copy_option_val(from->wo_fde);
6794 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006796 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006798#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006799 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006800#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006801
6802#ifdef FEAT_EVAL
6803 // Copy the script context so that we know where the value was last set.
6804 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6805 sizeof(to->wo_script_ctx));
6806#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006807 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808}
6809
6810/*
6811 * Check string options in a window for a NULL value.
6812 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006813 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006814check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
6816 check_winopt(&win->w_onebuf_opt);
6817 check_winopt(&win->w_allbuf_opt);
6818}
6819
6820/*
6821 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6822 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006823 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006824check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825{
6826#ifdef FEAT_FOLDING
6827 check_string_option(&wop->wo_fdi);
6828 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006829 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830# ifdef FEAT_EVAL
6831 check_string_option(&wop->wo_fde);
6832 check_string_option(&wop->wo_fdt);
6833# endif
6834 check_string_option(&wop->wo_fmr);
6835#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006836#ifdef FEAT_SIGNS
6837 check_string_option(&wop->wo_scl);
6838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839#ifdef FEAT_RIGHTLEFT
6840 check_string_option(&wop->wo_rlc);
6841#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006842#ifdef FEAT_LINEBREAK
6843 check_string_option(&wop->wo_sbr);
6844#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006845#ifdef FEAT_STL_OPT
6846 check_string_option(&wop->wo_stl);
6847#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006848#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006849 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006850 check_string_option(&wop->wo_cc);
6851#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006852#ifdef FEAT_CONCEAL
6853 check_string_option(&wop->wo_cocu);
6854#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006855#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006856 check_string_option(&wop->wo_twk);
6857 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006858#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006859#ifdef FEAT_LINEBREAK
6860 check_string_option(&wop->wo_briopt);
6861#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006862 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006863 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006864 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006865 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866}
6867
6868/*
6869 * Free the allocated memory inside a winopt_T.
6870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006872clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873{
6874#ifdef FEAT_FOLDING
6875 clear_string_option(&wop->wo_fdi);
6876 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006877 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878# ifdef FEAT_EVAL
6879 clear_string_option(&wop->wo_fde);
6880 clear_string_option(&wop->wo_fdt);
6881# endif
6882 clear_string_option(&wop->wo_fmr);
6883#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006884#ifdef FEAT_SIGNS
6885 clear_string_option(&wop->wo_scl);
6886#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006887#ifdef FEAT_LINEBREAK
6888 clear_string_option(&wop->wo_briopt);
6889#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006890 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891#ifdef FEAT_RIGHTLEFT
6892 clear_string_option(&wop->wo_rlc);
6893#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006894#ifdef FEAT_LINEBREAK
6895 clear_string_option(&wop->wo_sbr);
6896#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006897#ifdef FEAT_STL_OPT
6898 clear_string_option(&wop->wo_stl);
6899#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006900#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006901 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006902 clear_string_option(&wop->wo_cc);
6903#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006904#ifdef FEAT_CONCEAL
6905 clear_string_option(&wop->wo_cocu);
6906#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006907#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006908 clear_string_option(&wop->wo_twk);
6909 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006910#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006911 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006912 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006913 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914}
6915
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006916#ifdef FEAT_EVAL
6917// Index into the options table for a buffer-local option enum.
6918static int buf_opt_idx[BV_COUNT];
6919# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6920
6921/*
6922 * Initialize buf_opt_idx[] if not done already.
6923 */
6924 static void
6925init_buf_opt_idx(void)
6926{
6927 static int did_init_buf_opt_idx = FALSE;
6928 int i;
6929
6930 if (did_init_buf_opt_idx)
6931 return;
6932 did_init_buf_opt_idx = TRUE;
6933 for (i = 0; !istermoption_idx(i); i++)
6934 if (options[i].indir & PV_BUF)
6935 buf_opt_idx[options[i].indir & PV_MASK] = i;
6936}
6937#else
6938# define COPY_OPT_SCTX(buf, bv)
6939#endif
6940
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941/*
6942 * Copy global option values to local options for one buffer.
6943 * Used when creating a new buffer and sometimes when entering a buffer.
6944 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006945 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6947 * appropriate.
6948 * BCO_NOHELP Don't copy the values to a help buffer.
6949 */
6950 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006951buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952{
6953 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006954 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 int dont_do_help;
6956 int did_isk = FALSE;
6957
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006958 // Skip this when the option defaults have not been set yet. Happens when
6959 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 if (p_cpo != NULL)
6961 {
6962 /*
6963 * Always copy when entering and 'cpo' contains 'S'.
6964 * Don't copy when already initialized.
6965 * Don't copy when 'cpo' contains 's' and not entering.
6966 * 'S' BCO_ENTER initialized 's' should_copy
6967 * yes yes X X TRUE
6968 * yes no yes X FALSE
6969 * no X yes X FALSE
6970 * X no no yes FALSE
6971 * X no no no TRUE
6972 * no yes no X TRUE
6973 */
6974 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6975 && (buf->b_p_initialized
6976 || (!(flags & BCO_ENTER)
6977 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6978 should_copy = FALSE;
6979
6980 if (should_copy || (flags & BCO_ALWAYS))
6981 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006982#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006983 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006984 init_buf_opt_idx();
6985#endif
6986 // Don't copy the options specific to a help buffer when
6987 // BCO_NOHELP is given or the options were initialized already
6988 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6990 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006991 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {
6993 save_p_isk = buf->b_p_isk;
6994 buf->b_p_isk = NULL;
6995 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006996 // Always free the allocated strings. If not already initialized,
6997 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 if (!buf->b_p_initialized)
6999 {
7000 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007001 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02007004 switch (*p_ffs)
7005 {
7006 case 'm':
7007 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
7008 case 'd':
7009 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
7010 case 'u':
7011 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
7012 default:
7013 buf->b_p_ff = vim_strsave(p_ff);
7014 }
7015 if (buf->b_p_ff != NULL)
7016 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 buf->b_p_bh = empty_option;
7018 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 }
7020 else
7021 free_buf_options(buf, FALSE);
7022
7023 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007024 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 buf->b_p_ai_nopaste = p_ai_nopaste;
7026 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007027 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007029 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 buf->b_p_tw_nopaste = p_tw_nopaste;
7031 buf->b_p_tw_nobin = p_tw_nobin;
7032 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007033 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 buf->b_p_wm_nopaste = p_wm_nopaste;
7035 buf->b_p_wm_nobin = p_wm_nobin;
7036 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007037 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00007038 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007039 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02007040 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007041 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007043 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007045 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007047 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 buf->b_p_ml_nobin = p_ml_nobin;
7049 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007050 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02007051 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007052 buf->b_p_swf = FALSE;
7053 else
7054 {
7055 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00007056 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007059 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02007060#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02007061 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007062 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007064#ifdef FEAT_COMPL_FUNC
7065 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007066 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007067 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00007068 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007069 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007070 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007071#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007072#ifdef FEAT_EVAL
7073 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007074 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007075 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007078 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007080#ifdef FEAT_VARTABS
7081 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007082 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007083 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007084 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007085 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007086 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007087 buf->b_p_vsts_nopaste = p_vsts_nopaste
7088 ? vim_strsave(p_vsts_nopaste) : NULL;
7089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007091 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007093 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094#ifdef FEAT_FOLDING
7095 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007096 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097#endif
7098 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007099 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007100 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007101 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007102 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7103 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007105 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007107 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007109 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007111 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007112
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007114 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007116 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007118 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007119 buf->b_p_cinsd = vim_strsave(p_cinsd);
7120 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007121 buf->b_p_lop = vim_strsave(p_lop);
7122 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007123
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007124 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007127 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007129 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007131 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007133 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007135 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007136 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007137 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007138#endif
7139#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007140 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007141 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007142 (void)compile_cap_prog(&buf->b_s);
7143 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007144 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007145 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007146 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007147 buf->b_s.b_p_spo = vim_strsave(p_spo);
7148 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007150#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007152 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007154 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007156 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007157#if defined(FEAT_EVAL)
7158 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007159 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161#ifdef FEAT_CRYPT
7162 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007163 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007166 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167#ifdef FEAT_KEYMAP
7168 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007169 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 buf->b_kmap_state |= KEYMAP_INIT;
7171#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007172#ifdef FEAT_TERMINAL
7173 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007174 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007175#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007176 // This isn't really an option, but copying the langmap and IME
7177 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007179 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007181 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007183 // options that are normally global but also have a local value
7184 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007186 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007187 buf->b_p_bkc = empty_option;
7188 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189#ifdef FEAT_QUICKFIX
7190 buf->b_p_gp = empty_option;
7191 buf->b_p_mp = empty_option;
7192 buf->b_p_efm = empty_option;
7193#endif
7194 buf->b_p_ep = empty_option;
7195 buf->b_p_kp = empty_option;
7196 buf->b_p_path = empty_option;
7197 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007198 buf->b_p_tc = empty_option;
7199 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200#ifdef FEAT_FIND_ID
7201 buf->b_p_def = empty_option;
7202 buf->b_p_inc = empty_option;
7203# ifdef FEAT_EVAL
7204 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007205 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206# endif
7207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 buf->b_p_dict = empty_option;
7209 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007210#ifdef FEAT_COMPL_FUNC
7211 buf->b_p_tsrfu = empty_option;
7212#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007213 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007214 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007215#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7216 buf->b_p_bexpr = empty_option;
7217#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007218#if defined(FEAT_CRYPT)
7219 buf->b_p_cm = empty_option;
7220#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007221#ifdef FEAT_PERSISTENT_UNDO
7222 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007223 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007224#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007225 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007226 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007228 // Don't copy the options set by ex_help(), use the saved values,
7229 // when going from a help buffer to a non-help buffer.
7230 // Don't touch these at all when BCO_NOHELP is used and going from
7231 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007235#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007236 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007237 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007238 else
7239 buf->b_p_vts_array = NULL;
7240#endif
7241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 else
7243 {
7244 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007245 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 did_isk = TRUE;
7247 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007248 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007249#ifdef FEAT_VARTABS
7250 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007251 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007252 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007253 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007254 else
7255 buf->b_p_vts_array = NULL;
7256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 if (buf->b_p_bt[0] == 'h')
7259 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007261 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 }
7263 }
7264
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007265 // When the options should be copied (ignoring BCO_ALWAYS), set the
7266 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 if (should_copy)
7268 buf->b_p_initialized = TRUE;
7269 }
7270
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007271 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 if (did_isk)
7273 (void)buf_init_chartab(buf, FALSE);
7274}
7275
7276/*
7277 * Reset the 'modifiable' option and its default value.
7278 */
7279 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007280reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281{
7282 int opt_idx;
7283
7284 curbuf->b_p_ma = FALSE;
7285 p_ma = FALSE;
7286 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007287 if (opt_idx >= 0)
7288 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289}
7290
7291/*
7292 * Set the global value for 'iminsert' to the local value.
7293 */
7294 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007295set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296{
7297 p_iminsert = curbuf->b_p_iminsert;
7298}
7299
7300/*
7301 * Set the global value for 'imsearch' to the local value.
7302 */
7303 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007304set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305{
7306 p_imsearch = curbuf->b_p_imsearch;
7307}
7308
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309static int expand_option_idx = -1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007310static int expand_option_start_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7312static int expand_option_flags = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007313static int expand_option_append = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314
7315 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007316set_context_in_set_cmd(
7317 expand_T *xp,
7318 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007319 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007322 long_u flags = 0; // init for GCC
7323 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 char_u *p;
7325 char_u *s;
7326 int is_term_option = FALSE;
7327 int key;
7328
7329 expand_option_flags = opt_flags;
7330
7331 xp->xp_context = EXPAND_SETTINGS;
7332 if (*arg == NUL)
7333 {
7334 xp->xp_pattern = arg;
7335 return;
7336 }
7337 p = arg + STRLEN(arg) - 1;
7338 if (*p == ' ' && *(p - 1) != '\\')
7339 {
7340 xp->xp_pattern = p + 1;
7341 return;
7342 }
7343 while (p > arg)
7344 {
7345 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007346 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 if (*p == ' ' || *p == ',')
7348 {
7349 while (s > arg && *(s - 1) == '\\')
7350 --s;
7351 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007352 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 if (*p == ' ' && ((p - s) & 1) == 0)
7354 {
7355 ++p;
7356 break;
7357 }
7358 --p;
7359 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007360 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 {
7362 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007363 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 p += 2;
7365 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007366 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 {
7368 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007369 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 p += 3;
7371 }
7372 xp->xp_pattern = arg = p;
7373 if (*arg == '<')
7374 {
7375 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007376 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 return;
7378 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007379 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
7381 xp->xp_context = EXPAND_NOTHING;
7382 return;
7383 }
7384 nextchar = *++p;
7385 is_term_option = TRUE;
7386 expand_option_name[2] = KEY2TERMCAP0(key);
7387 expand_option_name[3] = KEY2TERMCAP1(key);
7388 }
7389 else
7390 {
7391 if (p[0] == 't' && p[1] == '_')
7392 {
7393 p += 2;
7394 if (*p != NUL)
7395 ++p;
7396 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007397 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 nextchar = *++p;
7399 is_term_option = TRUE;
7400 expand_option_name[2] = p[-2];
7401 expand_option_name[3] = p[-1];
7402 }
7403 else
7404 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007405 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7407 p++;
7408 if (*p == NUL)
7409 return;
7410 nextchar = *p;
7411 *p = NUL;
7412 opt_idx = findoption(arg);
7413 *p = nextchar;
7414 if (opt_idx == -1 || options[opt_idx].var == NULL)
7415 {
7416 xp->xp_context = EXPAND_NOTHING;
7417 return;
7418 }
7419 flags = options[opt_idx].flags;
7420 if (flags & P_BOOL)
7421 {
7422 xp->xp_context = EXPAND_NOTHING;
7423 return;
7424 }
7425 }
7426 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007427 // handle "-=" and "+="
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007428 expand_option_append = FALSE;
7429 int expand_option_subtract = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7431 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007432 if (nextchar == '-')
7433 expand_option_subtract = TRUE;
7434 if (nextchar == '+' || nextchar == '^')
7435 expand_option_append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 ++p;
7437 nextchar = '=';
7438 }
7439 if ((nextchar != '=' && nextchar != ':')
7440 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7441 {
7442 xp->xp_context = EXPAND_UNSUCCESSFUL;
7443 return;
7444 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007445
7446 // Below are for handling expanding a specific option's value after the '='
7447 // or ':'
7448
7449 if (is_term_option)
7450 expand_option_idx = -1;
7451 else
7452 expand_option_idx = opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02007454 if (!is_term_option)
7455 {
7456 if (options[opt_idx].flags & P_NO_CMD_EXPAND)
7457 {
7458 xp->xp_context=EXPAND_UNSUCCESSFUL;
7459 return;
7460 }
7461 }
7462
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 xp->xp_pattern = p + 1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007464 expand_option_start_col = (int)(p + 1 - xp->xp_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007466 // Certain options currently have special case handling to reuse the
7467 // expansion logic with other commands.
Doug Kearns6dfdff32023-08-27 18:48:51 +02007468#ifdef FEAT_SYN_HL
7469 if (options[opt_idx].var == (char_u *)&p_syn)
7470 {
7471 xp->xp_context = EXPAND_OWNSYNTAX;
7472 return;
7473 }
7474#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007475 if (options[opt_idx].var == (char_u *)&p_ft)
7476 {
7477 xp->xp_context = EXPAND_FILETYPE;
7478 return;
7479 }
Doug Kearns81642d92024-01-04 22:37:44 +01007480#ifdef FEAT_KEYMAP
7481 if (options[opt_idx].var == (char_u *)&p_keymap)
7482 {
7483 xp->xp_context = EXPAND_KEYMAP;
7484 return;
7485 }
7486#endif
Doug Kearns6dfdff32023-08-27 18:48:51 +02007487
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007488 // Now pick. If the option has a custom expander, use that. Otherwise, just
7489 // fill with the existing option value.
7490 if (expand_option_subtract)
7491 {
7492 xp->xp_context = EXPAND_SETTING_SUBTRACT;
7493 return;
7494 }
7495 else if (expand_option_idx >= 0 &&
7496 options[expand_option_idx].opt_expand_cb != NULL)
7497 {
7498 xp->xp_context = EXPAND_STRING_SETTING;
7499 }
7500 else if (*xp->xp_pattern == NUL)
7501 {
7502 xp->xp_context = EXPAND_OLD_SETTING;
7503 return;
7504 }
7505 else
7506 xp->xp_context = EXPAND_NOTHING;
7507
7508 if (is_term_option || (flags & P_NUM))
7509 return;
7510
7511 // Only string options below
7512
7513 // Options that have P_EXPAND are considered to all use file/dir expansion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 if (flags & P_EXPAND)
7515 {
7516 p = options[opt_idx].var;
7517 if (p == (char_u *)&p_bdir
7518 || p == (char_u *)&p_dir
7519 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007520 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523#ifdef FEAT_SESSION
7524 || p == (char_u *)&p_vdir
7525#endif
7526 )
7527 {
7528 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007529 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 xp->xp_backslash = XP_BS_THREE;
7531 else
7532 xp->xp_backslash = XP_BS_ONE;
7533 }
7534 else
7535 {
7536 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007537 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 if (p == (char_u *)&p_tags)
7539 xp->xp_backslash = XP_BS_THREE;
7540 else
7541 xp->xp_backslash = XP_BS_ONE;
7542 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02007543 if (flags & P_COMMA)
7544 xp->xp_backslash |= XP_BS_COMMA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 }
7546
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007547 // For an option that is a list of file names, or comma/colon-separated
7548 // values, split it by the delimiter and find the start of the current
7549 // pattern, while accounting for backslash-escaped space/commas/colons.
7550 // Triple-backslashed escaped file names (e.g. 'path') can also be
7551 // delimited by space.
7552 if ((flags & P_EXPAND) || (flags & P_COMMA) || (flags & P_COLON))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007554 for (p = arg + STRLEN(arg) - 1; p >= xp->xp_pattern; --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007556 // count number of backslashes before ' ' or ',' or ':'
7557 if (*p == ' ' || *p == ',' ||
7558 (*p == ':' && (flags & P_COLON)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007560 s = p;
7561 while (s > xp->xp_pattern && *(s - 1) == '\\')
7562 --s;
Yee Cheng Chin54844852023-10-09 18:12:31 +02007563 if ((*p == ' ' && ((xp->xp_backslash & XP_BS_THREE) && (p - s) < 3))
7564#if defined(BACKSLASH_IN_FILENAME)
7565 || (*p == ',' && (flags & P_COMMA) && (p - s) < 1)
7566#else
7567 || (*p == ',' && (flags & P_COMMA) && (p - s) < 2)
7568#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007569 || (*p == ':' && (flags & P_COLON)))
7570 {
7571 xp->xp_pattern = p + 1;
7572 break;
7573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 }
7575 }
7576 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007577
7578 // An option that is a list of single-character flags should always start
7579 // at the end as we don't complete words.
7580 if (flags & P_FLAGLIST)
7581 xp->xp_pattern = arg + STRLEN(arg);
7582
7583 // Some options can either be using file/dir expansions, or custom value
7584 // expansion depending on what the user typed. Unfortunately we have to
7585 // manually handle it here to make sure we have the correct xp_context set.
7586#ifdef FEAT_SPELL
7587 if (options[opt_idx].var == (char_u *)&p_sps)
7588 {
7589 if (STRNCMP(xp->xp_pattern, "file:", 5) == 0)
7590 {
7591 xp->xp_pattern += 5;
7592 return;
7593 }
7594 else if (options[expand_option_idx].opt_expand_cb != NULL)
7595 {
7596 xp->xp_context = EXPAND_STRING_SETTING;
7597 }
7598 }
7599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600}
7601
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007602/*
7603 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7604 *
7605 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7606 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7607 *
7608 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7609 * regular expression 'regmatch', then stores the match in matches[idx] and
7610 * returns TRUE.
7611 *
7612 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7613 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7614 *
7615 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7616 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7617 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02007618 static int
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007619match_str(
7620 char_u *str,
7621 regmatch_T *regmatch,
7622 char_u **matches,
7623 int idx,
7624 int test_only,
7625 int fuzzy,
7626 char_u *fuzzystr,
7627 fuzmatch_str_T *fuzmatch)
7628{
7629 if (!fuzzy)
7630 {
7631 if (vim_regexec(regmatch, str, (colnr_T)0))
7632 {
7633 if (!test_only)
7634 matches[idx] = vim_strsave(str);
7635 return TRUE;
7636 }
7637 }
7638 else
7639 {
7640 int score;
7641
7642 score = fuzzy_match_str(str, fuzzystr);
7643 if (score != 0)
7644 {
7645 if (!test_only)
7646 {
7647 fuzmatch[idx].idx = idx;
7648 fuzmatch[idx].str = vim_strsave(str);
7649 fuzmatch[idx].score = score;
7650 }
7651
7652 return TRUE;
7653 }
7654 }
7655
7656 return FALSE;
7657}
7658
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007660ExpandSettings(
7661 expand_T *xp,
7662 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007663 char_u *fuzzystr,
7664 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007665 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007666 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007668 int num_normal = 0; // Nr of matching non-term-code settings
7669 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 int opt_idx;
7671 int match;
7672 int count = 0;
7673 char_u *str;
7674 int loop;
7675 int is_term_opt;
7676 char_u name_buf[MAX_KEY_NAME_LEN];
7677 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007678 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007679 int fuzzy;
7680 fuzmatch_str_T *fuzmatch = NULL;
7681
Christian Brabandtcb747892022-05-08 21:10:56 +01007682 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007684 // do this loop twice:
7685 // loop == 0: count the number of matching options
7686 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 for (loop = 0; loop <= 1; ++loop)
7688 {
7689 regmatch->rm_ic = ic;
7690 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7691 {
K.Takataeeec2542021-06-02 13:28:16 +02007692 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007693 {
7694 if (match_str((char_u *)names[match], regmatch, *matches,
7695 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 {
7697 if (loop == 0)
7698 num_normal++;
7699 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007700 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 }
7704 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7705 opt_idx++)
7706 {
7707 if (options[opt_idx].var == NULL)
7708 continue;
7709 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007710 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007712 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 if (is_term_opt && num_normal > 0)
7714 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007715
7716 if (match_str(str, regmatch, *matches, count, (loop == 0),
7717 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 {
7719 if (loop == 0)
7720 {
7721 if (is_term_opt)
7722 num_term++;
7723 else
7724 num_normal++;
7725 }
7726 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007727 count++;
7728 }
7729 else if (!fuzzy && options[opt_idx].shortname != NULL
7730 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007731 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007732 {
7733 // Compare against the abbreviated option name (for regular
7734 // expression match). Fuzzy matching (previous if) already
7735 // matches against both the expanded and abbreviated names.
7736 if (loop == 0)
7737 {
7738 if (is_term_opt)
7739 num_term++;
7740 else
7741 num_normal++;
7742 }
7743 else
7744 (*matches)[count++] = vim_strsave(str);
7745 }
7746 else if (is_term_opt)
7747 {
7748 name_buf[0] = '<';
7749 name_buf[1] = 't';
7750 name_buf[2] = '_';
7751 name_buf[3] = str[2];
7752 name_buf[4] = str[3];
7753 name_buf[5] = '>';
7754 name_buf[6] = NUL;
7755
7756 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7757 fuzzy, fuzzystr, fuzmatch))
7758 {
7759 if (loop == 0)
7760 num_term++;
7761 else
7762 count++;
7763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 }
7765 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007766
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007767 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7769 {
7770 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7771 {
Keith Thompson184f71c2024-01-04 21:19:04 +01007772 if (!SAFE_isprint(str[0]) || !SAFE_isprint(str[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 continue;
7774
7775 name_buf[0] = 't';
7776 name_buf[1] = '_';
7777 name_buf[2] = str[0];
7778 name_buf[3] = str[1];
7779 name_buf[4] = NUL;
7780
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007781 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007782 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007783 {
7784 if (loop == 0)
7785 num_term++;
7786 else
7787 count++;
7788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 else
7790 {
7791 name_buf[0] = '<';
7792 name_buf[1] = 't';
7793 name_buf[2] = '_';
7794 name_buf[3] = str[0];
7795 name_buf[4] = str[1];
7796 name_buf[5] = '>';
7797 name_buf[6] = NUL;
7798
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007799 if (match_str(name_buf, regmatch, *matches, count,
7800 (loop == 0), fuzzy, fuzzystr,
7801 fuzmatch))
7802 {
7803 if (loop == 0)
7804 num_term++;
7805 else
7806 count++;
7807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809 }
7810
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007811 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007812 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7814 {
7815 name_buf[0] = '<';
7816 STRCPY(name_buf + 1, str);
7817 STRCAT(name_buf, ">");
7818
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007819 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7820 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 {
7822 if (loop == 0)
7823 num_term++;
7824 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007825 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 }
7827 }
7828 }
7829 if (loop == 0)
7830 {
7831 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007832 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007834 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 else
7836 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007837 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007839 *matches = ALLOC_MULT(char_u *, *numMatches);
7840 if (*matches == NULL)
7841 {
7842 *matches = (char_u **)"";
7843 return FAIL;
7844 }
7845 }
7846 else
7847 {
7848 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7849 if (fuzmatch == NULL)
7850 {
7851 *matches = (char_u **)"";
7852 return FAIL;
7853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 }
7855 }
7856 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007857
7858 if (fuzzy &&
7859 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7860 return FAIL;
7861
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 return OK;
7863}
7864
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007865// Escape an option value that can be used on the command-line with :set.
7866// Caller needs to free the returned string, unless NULL is returned.
7867 static char_u*
7868escape_option_str_cmdline(char_u *var)
7869{
7870 char_u *buf;
7871
7872 // A backslash is required before some characters. This is the reverse of
7873 // what happens in do_set().
7874 buf = vim_strsave_escaped(var, escape_chars);
7875 if (buf == NULL)
7876 return NULL;
7877
7878#ifdef BACKSLASH_IN_FILENAME
7879 // For MS-Windows et al. we don't double backslashes at the start and
7880 // before a file name character.
7881 // The reverse is found at stropt_copy_value().
7882 for (var = buf; *var != NUL; MB_PTR_ADV(var))
7883 if (var[0] == '\\' && var[1] == '\\'
7884 && expand_option_idx >= 0
7885 && (options[expand_option_idx].flags & P_EXPAND)
7886 && vim_isfilec(var[2])
7887 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
7888 STRMOVE(var, var + 1);
7889#endif
7890 return buf;
7891}
7892
7893/*
7894 * Expansion handler for :set= when we just want to fill in with the existing value.
7895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007897ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007899 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 char_u *buf;
7901
zeertzjqb0d45ec2023-01-25 15:04:22 +00007902 *numMatches = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007903 *matches = ALLOC_MULT(char_u *, 1);
zeertzjqb0d45ec2023-01-25 15:04:22 +00007904 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 return FAIL;
7906
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007907 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 if (expand_option_idx < 0)
7909 {
7910 var = find_termcode(expand_option_name + 2);
7911 if (var == NULL)
7912 expand_option_idx = findoption(expand_option_name);
7913 }
7914
7915 if (expand_option_idx >= 0)
7916 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007917 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 option_value2string(&options[expand_option_idx], expand_option_flags);
7919 var = NameBuff;
7920 }
7921 else if (var == NULL)
7922 var = (char_u *)"";
7923
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007924 buf = escape_option_str_cmdline(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 if (buf == NULL)
7926 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007927 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 return FAIL;
7929 }
7930
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007931 (*matches)[0] = buf;
zeertzjqb0d45ec2023-01-25 15:04:22 +00007932 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 return OK;
7934}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935
7936/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007937 * Expansion handler for :set=/:set+= when the option has a custom expansion handler.
7938 */
7939 int
7940ExpandStringSetting(
7941 expand_T *xp,
7942 regmatch_T *regmatch,
7943 int *numMatches,
7944 char_u ***matches)
7945{
7946 char_u *var = NULL; // init for GCC
7947 char_u *buf;
7948
7949 if (expand_option_idx < 0 ||
7950 options[expand_option_idx].opt_expand_cb == NULL)
7951 {
7952 // Not supposed to reach this. This function is only for options with
7953 // custom expansion callbacks.
7954 return FAIL;
7955 }
7956
7957 optexpand_T args;
7958 args.oe_varp = get_varp_scope(&options[expand_option_idx], expand_option_flags);
7959 args.oe_append = expand_option_append;
7960 args.oe_regmatch = regmatch;
7961 args.oe_xp = xp;
7962 args.oe_set_arg = xp->xp_line + expand_option_start_col;
7963 args.oe_include_orig_val =
7964 !expand_option_append &&
7965 (*args.oe_set_arg == NUL);
7966
7967 // Retrieve the existing value, but escape it as a reverse of setting it.
7968 // We technically only need to do this when oe_append or
7969 // oe_include_orig_val is true.
7970 option_value2string(&options[expand_option_idx], expand_option_flags);
7971 var = NameBuff;
7972 buf = escape_option_str_cmdline(var);
7973 if (buf == NULL)
7974 return FAIL;
7975
7976 args.oe_opt_value = buf;
7977
7978 int num_ret = options[expand_option_idx].opt_expand_cb(&args, numMatches, matches);
7979
7980 vim_free(buf);
7981 return num_ret;
7982}
7983
7984/*
7985 * Expansion handler for :set-=
7986 */
7987 int
7988ExpandSettingSubtract(
7989 expand_T *xp,
7990 regmatch_T *regmatch,
7991 int *numMatches,
7992 char_u ***matches)
7993{
7994 if (expand_option_idx < 0)
7995 // term option
7996 return ExpandOldSetting(numMatches, matches);
7997
7998 char_u *option_val = *(char_u**)get_option_varp_scope(
7999 expand_option_idx, expand_option_flags);
8000
8001 long_u option_flags = options[expand_option_idx].flags;
8002
8003 if (option_flags & P_NUM)
8004 return ExpandOldSetting(numMatches, matches);
8005 else if (option_flags & P_COMMA)
8006 {
8007 // Split the option by comma, then present each option to the user if
8008 // it matches the pattern.
8009 // This condition needs to go first, because 'whichwrap' has both
8010 // P_COMMA and P_FLAGLIST.
8011 garray_T ga;
8012
8013 char_u *item;
8014 char_u *option_copy;
8015 char_u *next_val;
8016 char_u *comma;
8017
8018 if (*option_val == NUL)
8019 return FAIL;
8020
8021 // Make a copy as we need to inject null characters destructively.
8022 option_copy = vim_strsave(option_val);
8023 if (option_copy == NULL)
8024 return FAIL;
8025 next_val = option_copy;
8026
8027 ga_init2(&ga, sizeof(char_u *), 10);
8028
8029 do
8030 {
8031 item = next_val;
8032 comma = vim_strchr(next_val, ',');
8033 while (comma != NULL && comma != next_val && *(comma - 1) == '\\')
8034 {
8035 // "\," is interpreted as a literal comma rather than option
8036 // separator when reading options in copy_option_part(). Skip
8037 // it.
8038 comma = vim_strchr(comma + 1, ',');
8039 }
8040 if (comma != NULL)
8041 {
8042 *comma = NUL; // null-terminate this value, required by later functions
8043 next_val = comma + 1;
8044 }
8045 else
8046 next_val = NULL;
8047
8048 if (*item == NUL)
8049 // empty value, don't add to list
8050 continue;
8051
8052 if (!vim_regexec(regmatch, item, (colnr_T)0))
8053 continue;
8054
8055 char_u *buf = escape_option_str_cmdline(item);
8056 if (buf == NULL)
8057 {
8058 vim_free(option_copy);
8059 ga_clear_strings(&ga);
8060 return FAIL;
8061 }
8062 if (ga_add_string(&ga, buf) != OK)
8063 {
8064 vim_free(buf);
8065 break;
8066 }
8067 } while (next_val != NULL);
8068
8069 vim_free(option_copy);
8070
8071 *matches = ga.ga_data;
8072 *numMatches = ga.ga_len;
8073 return OK;
8074 }
8075 else if (option_flags & P_FLAGLIST)
8076 {
8077 // Only present the flags that are set on the option as the other flags
8078 // are not meaningful to do set-= on.
8079
8080 if (*xp->xp_pattern != NUL)
8081 {
8082 // Don't suggest anything if cmdline is non-empty. Vim's set-=
8083 // behavior requires consecutive strings and it's usually
Viktor Szépedbf749b2023-10-16 09:53:37 +02008084 // unintuitive to users if they try to subtract multiple flags at
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008085 // once.
8086 return FAIL;
8087 }
8088
Yee Cheng Chin6d113472023-10-02 21:38:39 +02008089 size_t num_flags = STRLEN(option_val);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008090 if (num_flags == 0)
8091 return FAIL;
8092
8093 *matches = ALLOC_MULT(char_u *, num_flags + 1);
8094 if (*matches == NULL)
8095 return FAIL;
8096
8097 int count = 0;
8098 char_u *p;
8099
8100 p = vim_strsave(option_val);
8101 if (p == NULL)
8102 {
8103 VIM_CLEAR(*matches);
8104 return FAIL;
8105 }
8106 (*matches)[count++] = p;
8107
8108 if (num_flags > 1)
8109 {
8110 // If more than one flags, split the flags up and expose each
8111 // character as individual choice.
8112 for (char_u *flag = option_val; *flag != NUL; flag++)
8113 {
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02008114 p = vim_strnsave(flag, 1);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008115 if (p == NULL)
8116 break;
8117 (*matches)[count++] = p;
8118 }
8119 }
8120
8121 *numMatches = count;
8122 return OK;
8123 }
8124
8125 return ExpandOldSetting(numMatches, matches);
8126}
8127
8128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 * Get the value for the numeric or string option *opp in a nice format into
8130 * NameBuff[]. Must not be called with a hidden option!
8131 */
8132 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008133option_value2string(
8134 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008135 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136{
8137 char_u *varp;
8138
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008139 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140
8141 if (opp->flags & P_NUM)
8142 {
8143 long wc = 0;
8144
8145 if (wc_use_keyname(varp, &wc))
8146 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
8147 else if (wc != 0)
8148 STRCPY(NameBuff, transchar((int)wc));
8149 else
8150 sprintf((char *)NameBuff, "%ld", *(long *)varp);
8151 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008152 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {
8154 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008155 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 NameBuff[0] = NUL;
8157#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008158 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008159 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 STRCPY(NameBuff, "*****");
8161#endif
8162 else if (opp->flags & P_EXPAND)
8163 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008164 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 else if ((char_u **)opp->var == &p_pt)
8166 str2specialbuf(p_pt, NameBuff, MAXPATHL);
8167 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008168 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 }
8170}
8171
8172/*
8173 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
8174 * printed as a keyname.
8175 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
8176 */
8177 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008178wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179{
8180 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
8181 {
8182 *wcp = *(long *)varp;
8183 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
8184 return TRUE;
8185 }
8186 return FALSE;
8187}
8188
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 * Return TRUE if "x" is present in 'shortmess' option, or
8191 * 'shortmess' contains 'a' and "x" is present in SHM_A.
8192 */
8193 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008194shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01008196 return p_shm != NULL &&
8197 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 || (vim_strchr(p_shm, 'a') != NULL
8199 && vim_strchr((char_u *)SHM_A, x) != NULL));
8200}
8201
8202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
8204 *
8205 * Reset 'compatible' and set the values for options that didn't get set yet
8206 * to the Vim defaults.
8207 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008208 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 */
8210 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008211vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008213 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00008214 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008215 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216
8217 if (!option_was_set((char_u *)"cp"))
8218 {
8219 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02008220 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
8222 set_option_default(opt_idx, OPT_FREE, FALSE);
8223 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008224 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008226
8227 if (fname != NULL)
8228 {
8229 p = vim_getenv(envname, &dofree);
8230 if (p == NULL)
8231 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008232 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008233 p = FullName_save(fname, FALSE);
8234 if (p != NULL)
8235 {
8236 vim_setenv(envname, p);
8237 vim_free(p);
8238 }
8239 }
8240 else if (dofree)
8241 vim_free(p);
8242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243}
8244
8245/*
8246 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
8247 */
8248 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008249change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008251 int opt_idx;
8252
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 if (p_cp != on)
8254 {
8255 p_cp = on;
8256 compatible_set();
8257 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008258 opt_idx = findoption((char_u *)"cp");
8259 if (opt_idx >= 0)
8260 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261}
8262
8263/*
8264 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008265 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 */
8267 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008268option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269{
8270 int idx;
8271
8272 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008273 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 return FALSE;
8275 if (options[idx].flags & P_WAS_SET)
8276 return TRUE;
8277 return FALSE;
8278}
8279
8280/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008281 * Reset the flag indicating option "name" was set.
8282 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008283 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008284reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008285{
8286 int idx = findoption(name);
8287
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008288 if (idx < 0)
8289 return FAIL;
8290
8291 options[idx].flags &= ~P_WAS_SET;
8292 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008293}
8294
8295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 * compatible_set() - Called when 'compatible' has been set or unset.
8297 *
8298 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8299 * flag) to a Vi compatible value.
8300 * When 'compatible' is unset: Set all options that have a different default
8301 * for Vim (without the P_VI_DEF flag) to that default.
8302 */
8303 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008304compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305{
8306 int opt_idx;
8307
Bram Moolenaardac13472019-09-16 21:06:21 +02008308 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8310 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8311 set_option_default(opt_idx, OPT_FREE, p_cp);
8312 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008313 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314}
8315
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 * Check if backspacing over something is allowed.
8318 */
8319 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008320can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008321 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008323#ifdef FEAT_JOB_CHANNEL
8324 if (what == BS_START && bt_prompt(curbuf))
8325 return FALSE;
8326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 switch (*p_bs)
8328 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008329 case '3': return TRUE;
8330 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 case '1': return (what != BS_START);
8332 case '0': return FALSE;
8333 }
8334 return vim_strchr(p_bs, what) != NULL;
8335}
8336
8337/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008338 * Return the effective 'scrolloff' value for the current window, using the
8339 * global value when appropriate.
8340 */
8341 long
8342get_scrolloff_value(void)
8343{
8344 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8345}
8346
8347/*
8348 * Return the effective 'sidescrolloff' value for the current window, using the
8349 * global value when appropriate.
8350 */
8351 long
8352get_sidescrolloff_value(void)
8353{
8354 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8355}
8356
8357/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008358 * Get the local or global value of 'backupcopy'.
8359 */
8360 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008361get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008362{
8363 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8364}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008365
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008366#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008367/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008368 * Get the local or global value of 'formatlistpat'.
8369 */
8370 char_u *
8371get_flp_value(buf_T *buf)
8372{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008373 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8374 return p_flp;
8375 return buf->b_p_flp;
8376}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008377#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008378
8379/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008380 * Get the local or global value of the 'virtualedit' flags.
8381 */
8382 unsigned int
8383get_ve_flags(void)
8384{
Gary Johnson51ad8502021-08-03 18:33:08 +02008385 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8386 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008387}
8388
Bram Moolenaaree857022019-11-09 23:26:40 +01008389#if defined(FEAT_LINEBREAK) || defined(PROTO)
8390/*
8391 * Get the local or global value of 'showbreak'.
8392 */
8393 char_u *
8394get_showbreak_value(win_T *win)
8395{
8396 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8397 return p_sbr;
8398 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8399 return empty_option;
8400 return win->w_p_sbr;
8401}
8402#endif
8403
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008404#if defined(FEAT_EVAL) || defined(PROTO)
8405/*
8406 * Get window or buffer local options.
8407 */
8408 dict_T *
8409get_winbuf_options(int bufopt)
8410{
8411 dict_T *d;
8412 int opt_idx;
8413
8414 d = dict_alloc();
8415 if (d == NULL)
8416 return NULL;
8417
Bram Moolenaardac13472019-09-16 21:06:21 +02008418 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008419 {
8420 struct vimoption *opt = &options[opt_idx];
8421
8422 if ((bufopt && (opt->indir & PV_BUF))
8423 || (!bufopt && (opt->indir & PV_WIN)))
8424 {
8425 char_u *varp = get_varp(opt);
8426
8427 if (varp != NULL)
8428 {
8429 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008430 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008431 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008432 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008433 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008434 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008435 }
8436 }
8437 }
8438
8439 return d;
8440}
8441#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008442
Bram Moolenaardac13472019-09-16 21:06:21 +02008443#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008444/*
8445 * This is called when 'culopt' is changed
8446 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008447 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008448fill_culopt_flags(char_u *val, win_T *wp)
8449{
8450 char_u *p;
8451 char_u culopt_flags_new = 0;
8452
8453 if (val == NULL)
8454 p = wp->w_p_culopt;
8455 else
8456 p = val;
8457 while (*p != NUL)
8458 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008459 // Note: Keep this in sync with p_culopt_values.
Bram Moolenaar017ba072019-09-14 21:01:23 +02008460 if (STRNCMP(p, "line", 4) == 0)
8461 {
8462 p += 4;
8463 culopt_flags_new |= CULOPT_LINE;
8464 }
8465 else if (STRNCMP(p, "both", 4) == 0)
8466 {
8467 p += 4;
8468 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8469 }
8470 else if (STRNCMP(p, "number", 6) == 0)
8471 {
8472 p += 6;
8473 culopt_flags_new |= CULOPT_NBR;
8474 }
8475 else if (STRNCMP(p, "screenline", 10) == 0)
8476 {
8477 p += 10;
8478 culopt_flags_new |= CULOPT_SCRLINE;
8479 }
8480
8481 if (*p != ',' && *p != NUL)
8482 return FAIL;
8483 if (*p == ',')
8484 ++p;
8485 }
8486
8487 // Can't have both "line" and "screenline".
8488 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8489 return FAIL;
8490 wp->w_p_culopt_flags = culopt_flags_new;
8491
8492 return OK;
8493}
8494#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008495
8496/*
8497 * Get the value of 'magic' adjusted for Vim9 script.
8498 */
8499 int
8500magic_isset(void)
8501{
8502 switch (magic_overruled)
8503 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008504 case OPTION_MAGIC_ON: return TRUE;
8505 case OPTION_MAGIC_OFF: return FALSE;
8506 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008507 }
8508#ifdef FEAT_EVAL
8509 if (in_vim9script())
8510 return TRUE;
8511#endif
8512 return p_magic;
8513}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008514
8515/*
8516 * Set the callback function value for an option that accepts a function name,
8517 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8518 * Returns OK if the option is successfully set to a function, otherwise
8519 * returns FAIL.
8520 */
8521 int
8522option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8523{
8524#ifdef FEAT_EVAL
8525 typval_T *tv;
8526 callback_T cb;
8527
8528 if (optval == NULL || *optval == NUL)
8529 {
8530 free_callback(optcb);
8531 return OK;
8532 }
8533
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008534 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008535 || (STRNCMP(optval, "function(", 9) == 0)
8536 || (STRNCMP(optval, "funcref(", 8) == 0))
8537 // Lambda expression or a funcref
8538 tv = eval_expr(optval, NULL);
8539 else
8540 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008541 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008542 if (tv == NULL)
8543 return FAIL;
8544
8545 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008546 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008547 {
8548 free_tv(tv);
8549 return FAIL;
8550 }
8551
8552 free_callback(optcb);
8553 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008554 if (cb.cb_free_name)
8555 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008556 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008557
8558 // when using Vim9 style "import.funcname" it needs to be expanded to
8559 // "import#funcname".
8560 expand_autload_callback(optcb);
8561
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008562 return OK;
8563#else
8564 return FAIL;
8565#endif
8566}
Christian Brabandt757593c2023-08-22 21:44:10 +02008567
8568#if defined(FEAT_EVAL) || defined(PROTO)
8569 static void
8570didset_options_sctx(int opt_flags, char **buf)
8571{
8572 for (int i = 0; ; ++i)
8573 {
8574 if (buf[i] == NULL)
8575 break;
8576
8577 int idx = findoption((char_u *)buf[i]);
8578 if (idx >= 0)
8579 set_option_sctx_idx(idx, opt_flags, current_sctx);
8580 }
8581}
8582#endif