blob: 14447f59bae77ee921101f6f2043f5440566ec4f [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
367/*
368 * Expand environment variables and things like "~" for the defaults.
369 * If option_expand() returns non-NULL the variable is expanded. This can
370 * only happen for non-indirect options.
371 * Also set the default to the expanded value, so ":set" does not list
372 * them.
373 * Don't set the P_ALLOCED flag, because we don't want to free the
374 * default.
375 */
376 static void
377set_init_expand_env(void)
378{
379 int opt_idx;
380 char_u *p;
381
382 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
383 {
384 if ((options[opt_idx].flags & P_GETTEXT)
385 && options[opt_idx].var != NULL)
386 p = (char_u *)_(*(char **)options[opt_idx].var);
387 else
388 p = option_expand(opt_idx, NULL);
389 if (p != NULL && (p = vim_strsave(p)) != NULL)
390 {
391 *(char_u **)options[opt_idx].var = p;
392 // VIMEXP
393 // Defaults for all expanded options are currently the same for Vi
394 // and Vim. When this changes, add some code here! Also need to
395 // split P_DEF_ALLOCED in two.
396 if (options[opt_idx].flags & P_DEF_ALLOCED)
397 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
398 options[opt_idx].def_val[VI_DEFAULT] = p;
399 options[opt_idx].flags |= P_DEF_ALLOCED;
400 }
401 }
402}
403
404/*
405 * Initialize the 'LANG' environment variable to a default value.
406 */
407 static void
408set_init_lang_env(void)
409{
410#if defined(MSWIN) && defined(FEAT_GETTEXT)
411 // If $LANG isn't set, try to get a good value for it. This makes the
412 // right language be used automatically. Don't do this for English.
413 if (mch_getenv((char_u *)"LANG") == NULL)
414 {
415 char buf[20];
416 long_u n;
417
418 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
419 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
420 // only the first two.
421 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
422 (LPTSTR)buf, 20);
423 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
424 {
425 // There are a few exceptions (probably more)
426 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
427 STRCPY(buf, "zh_TW");
428 else if (STRNICMP(buf, "chs", 3) == 0
429 || STRNICMP(buf, "zhc", 3) == 0)
430 STRCPY(buf, "zh_CN");
431 else if (STRNICMP(buf, "jp", 2) == 0)
432 STRCPY(buf, "ja");
433 else
434 buf[2] = NUL; // truncate to two-letter code
435 vim_setenv((char_u *)"LANG", (char_u *)buf);
436 }
437 }
438#elif defined(MACOS_CONVERT)
439 // Moved to os_mac_conv.c to avoid dependency problems.
440 mac_lang_init();
441#endif
442}
443
444/*
445 * Initialize the 'encoding' option to a default value.
446 */
447 static void
448set_init_default_encoding(void)
449{
450 char_u *p;
451 int opt_idx;
452
Igor Todorovski497e5282024-01-12 17:59:18 +0100453# if defined(MSWIN) || defined(__MVS__)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000454 // MS-Windows has builtin support for conversion to and from Unicode, using
455 // "utf-8" for 'encoding' should work best for most users.
Igor Todorovski497e5282024-01-12 17:59:18 +0100456 // 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 +0000457 p = vim_strsave((char_u *)ENC_DFLT);
458# else
459 // enc_locale() will try to find the encoding of the current locale.
460 // This works best for properly configured systems, old and new.
461 p = enc_locale();
462# endif
463 if (p == NULL)
464 return;
465
466 // Try setting 'encoding' and check if the value is valid.
467 // If not, go back to the default encoding.
468 char_u *save_enc = p_enc;
469 p_enc = p;
470 if (STRCMP(p_enc, "gb18030") == 0)
471 {
472 // We don't support "gb18030", but "cp936" is a good substitute
473 // for practical purposes, thus use that. It's not an alias to
474 // still support conversion between gb18030 and utf-8.
475 p_enc = vim_strsave((char_u *)"cp936");
476 vim_free(p);
477 }
478 if (mb_init() == NULL)
479 {
480 opt_idx = findoption((char_u *)"encoding");
481 if (opt_idx >= 0)
482 {
483 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
484 options[opt_idx].flags |= P_DEF_ALLOCED;
485 }
486
487#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
488 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
489 {
490 // Adjust the default for 'isprint' and 'iskeyword' to match
491 // latin1. Also set the defaults for when 'nocompatible' is
492 // set.
493 set_string_option_direct((char_u *)"isp", -1,
494 ISP_LATIN1, OPT_FREE, SID_NONE);
495 set_string_option_direct((char_u *)"isk", -1,
496 ISK_LATIN1, OPT_FREE, SID_NONE);
497 opt_idx = findoption((char_u *)"isp");
498 if (opt_idx >= 0)
499 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
500 opt_idx = findoption((char_u *)"isk");
501 if (opt_idx >= 0)
502 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
503 (void)init_chartab();
504 }
505#endif
506
507#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
508 // Win32 console: When GetACP() returns a different value from
509 // GetConsoleCP() set 'termencoding'.
510 if (
511# ifdef VIMDLL
512 (!gui.in_use && !gui.starting) &&
513# endif
514 GetACP() != GetConsoleCP())
515 {
516 char buf[50];
517
518 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
519 // Use an alternative value.
520 if (GetConsoleCP() == 0)
521 sprintf(buf, "cp%ld", (long)GetACP());
522 else
523 sprintf(buf, "cp%ld", (long)GetConsoleCP());
524 p_tenc = vim_strsave((char_u *)buf);
525 if (p_tenc != NULL)
526 {
527 opt_idx = findoption((char_u *)"termencoding");
528 if (opt_idx >= 0)
529 {
530 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
531 options[opt_idx].flags |= P_DEF_ALLOCED;
532 }
533 convert_setup(&input_conv, p_tenc, p_enc);
534 convert_setup(&output_conv, p_enc, p_tenc);
535 }
536 else
537 p_tenc = empty_option;
538 }
539#endif
540#if defined(MSWIN)
541 // $HOME may have characters in active code page.
542 init_homedir();
543#endif
544 }
545 else
546 {
547 vim_free(p_enc);
548 p_enc = save_enc;
549 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000550}
551
552/*
553 * Initialize the options, first part.
554 *
555 * Called only once from main(), just after creating the first buffer.
556 * If "clean_arg" is TRUE Vim was started with --clean.
557 */
558 void
559set_init_1(int clean_arg)
560{
561#ifdef FEAT_LANGMAP
562 langmap_init();
563#endif
564
565 // Be Vi compatible by default
566 p_cp = TRUE;
567
568 // Use POSIX compatibility when $VIM_POSIX is set.
569 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
570 {
571 set_string_default("cpo", (char_u *)CPO_ALL);
572 set_string_default("shm", (char_u *)SHM_POSIX);
573 }
574
575 set_init_default_shell();
576 set_init_default_backupskip();
577 set_init_default_maxmemtot();
578 set_init_default_cdpath();
579 set_init_default_printencoding();
580#ifdef FEAT_POSTSCRIPT
581 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582#endif
583
584 /*
585 * Set all the options (except the terminal options) to their default
586 * value. Also set the global value for local options.
587 */
588 set_options_default(0);
589
matveytadbb1bf2022-02-01 17:26:12 +0000590#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000591 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000592#endif
593
Bram Moolenaar07268702018-03-01 21:57:32 +0100594#ifdef CLEAN_RUNTIMEPATH
595 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000596 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100597#endif
598
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599#ifdef FEAT_GUI
600 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100601 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602#endif
603
604 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100606 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 check_buf_options(curbuf);
608 check_win_options(curwin);
609 check_options();
610
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 didset_options();
613
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000614#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100615 // Use the current chartab for the generic chartab. This is not in
616 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000617 init_spell_chartab();
618#endif
619
K.Takatace3189d2023-02-15 19:13:43 +0000620 set_init_default_encoding();
621
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000622 // Expand environment variables and things like "~" for the defaults.
623 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100625 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100628 // Detect use of mlterm.
629 // Mlterm is a terminal emulator akin to xterm that has some special
630 // abilities (bidi namely).
631 // NOTE: mlterm's author is being asked to 'set' a variable
632 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100634 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635#endif
636
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200637 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000639 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100642 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 set_helplang_default(get_mess_lang());
644#endif
645}
646
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200647static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
648
649/*
650 * Set the "fileencodings" option to the default value for when 'encoding' is
651 * utf-8.
652 */
653 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000654set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200655{
656 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
657 OPT_FREE, 0);
658}
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660/*
661 * Set an option to its default value.
662 * This does not take care of side effects!
663 */
664 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100665set_option_default(
666 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100667 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
668 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 char_u *varp; // pointer to variable for current option
671 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000673 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
675
676 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
677 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100678 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
680 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
681 if (flags & P_STRING)
682 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200683 // 'fencs' default value depends on 'encoding'
684 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
685 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100686 // Use set_string_option_direct() for local options to handle
687 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200688 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200689 set_string_option_direct(NULL, opt_idx,
690 options[opt_idx].def_val[dvi], opt_flags, 0);
691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200693 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
694 free_string_option(*(char_u **)(varp));
695 *(char_u **)varp = options[opt_idx].def_val[dvi];
696 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 }
698 }
699 else if (flags & P_NUM)
700 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000701 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 win_comp_scroll(curwin);
703 else
704 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100705 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
706
707 if ((long *)varp == &curwin->w_p_so
708 || (long *)varp == &curwin->w_p_siso)
709 // 'scrolloff' and 'sidescrolloff' local values have a
710 // different default value than the global default.
711 *(long *)varp = -1;
712 else
713 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100714 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (both)
716 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100717 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100720 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100722 // the cast to long is required for Manx C, long_i is needed for
723 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000724 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000725#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100726 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000727 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
728 *(int *)varp = FALSE;
729#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100730 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 if (both)
732 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
733 *(int *)varp;
734 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000735
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100736 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000737 flagsp = insecure_flag(opt_idx, opt_flags);
738 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
740
741#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200742 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743#endif
744}
745
746/*
747 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200748 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 */
750 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100751set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100752 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753{
754 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000756 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
Bram Moolenaardac13472019-09-16 21:06:21 +0200758 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200759 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200760 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100761 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200762# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200763 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200764 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200765# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100766 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 set_option_default(i, opt_flags, p_cp);
768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100769 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000770 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200772 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773}
774
775/*
776 * Set the Vi-default value of a string option.
777 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100778 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100780 static void
781set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
783 char_u *p;
784 int opt_idx;
785
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100786 if (escape && vim_strchr(val, ' ') != NULL)
787 p = vim_strsave_escaped(val, (char_u *)" ");
788 else
789 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000790 if (p == NULL) // we don't want a NULL
791 return;
792
793 opt_idx = findoption((char_u *)name);
794 if (opt_idx < 0)
795 return;
796
797 if (options[opt_idx].flags & P_DEF_ALLOCED)
798 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
799 options[opt_idx].def_val[VI_DEFAULT] = p;
800 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801}
802
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100803 void
804set_string_default(char *name, char_u *val)
805{
806 set_string_default_esc(name, val, FALSE);
807}
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200810 * For an option value that contains comma separated items, find "newval" in
811 * "origval". Return NULL if not found.
812 */
813 static char_u *
814find_dup_item(char_u *origval, char_u *newval, long_u flags)
815{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200816 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200817 size_t newlen;
818 char_u *s;
819
820 if (origval == NULL)
821 return NULL;
822
823 newlen = STRLEN(newval);
824 for (s = origval; *s != NUL; ++s)
825 {
826 if ((!(flags & P_COMMA)
827 || s == origval
828 || (s[-1] == ',' && !(bs & 1)))
829 && STRNCMP(s, newval, newlen) == 0
830 && (!(flags & P_COMMA)
831 || s[newlen] == ','
832 || s[newlen] == NUL))
833 return s;
834 // Count backslashes. Only a comma with an even number of backslashes
835 // or a single backslash preceded by a comma before it is recognized as
836 // a separator.
837 if ((s > origval + 1
838 && s[-1] == '\\'
839 && s[-2] != ',')
840 || (s == origval + 1
841 && s[-1] == '\\'))
842 ++bs;
843 else
844 bs = 0;
845 }
846 return NULL;
847}
848
849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 * Set the Vi-default value of a number option.
851 * Used for 'lines' and 'columns'.
852 */
853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100854set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000856 int opt_idx;
857
858 opt_idx = findoption((char_u *)name);
859 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000860 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861}
862
Dominique Pelle748b3082022-01-08 12:41:16 +0000863#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200864/*
865 * Set all window-local and buffer-local options to the Vim default.
866 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200867 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200868 */
869 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200870set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200871{
872 win_T *save_curwin = curwin;
873 int i;
874
875 curwin = wp;
876 curbuf = curwin->w_buffer;
877 block_autocmds();
878
Bram Moolenaardac13472019-09-16 21:06:21 +0200879 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200880 {
881 struct vimoption *p = &(options[i]);
882 char_u *varp = get_varp_scope(p, OPT_LOCAL);
883
884 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200885 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200886 && !(options[i].flags & P_NODEFAULT)
887 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200888 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200889 }
890
891 unblock_autocmds();
892 curwin = save_curwin;
893 curbuf = curwin->w_buffer;
894}
Dominique Pelle748b3082022-01-08 12:41:16 +0000895#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200896
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000897#if defined(EXITFREE) || defined(PROTO)
898/*
899 * Free all options.
900 */
901 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100902free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000903{
904 int i;
905
Bram Moolenaardac13472019-09-16 21:06:21 +0200906 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000907 {
908 if (options[i].indir == PV_NONE)
909 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100910 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100911 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000912 free_string_option(*(char_u **)options[i].var);
913 if (options[i].flags & P_DEF_ALLOCED)
914 free_string_option(options[i].def_val[VI_DEFAULT]);
915 }
916 else if (options[i].var != VAR_WIN
917 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200919 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000920 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000921 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000922 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000923}
924#endif
925
926
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927/*
928 * Initialize the options, part two: After getting Rows and Columns and
929 * setting 'term'.
930 */
931 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100932set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000934 int idx;
935
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000936 // 'scroll' defaults to half the window height. The stored default is zero,
937 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000938 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000939 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000940 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 comp_col();
942
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000943 // 'window' is only for backwards compatibility with Vi.
944 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +0000945 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000946 p_window = Rows - 1;
947 set_number_default("window", Rows - 1);
948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100949 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100950#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000951 // If 'background' wasn't set by the user, try guessing the value,
952 // depending on the terminal name. Only need to check for terminals
953 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000954 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000955 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
956 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000958 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100959 // don't mark it as set, when starting the GUI it may be
960 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000961 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
963#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000964
965#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000966 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000967#endif
968#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000969 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000970#endif
971#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000972 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974}
975
976/*
977 * Initialize the options, part three: After reading the .vimrc
978 */
979 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100980set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100982#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983/*
984 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
985 * This is done after other initializations, where 'shell' might have been
986 * set, but only if they have not been set before.
987 */
988 char_u *p;
989 int idx_srr;
990 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100991# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 int idx_sp;
993 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000997 if (idx_srr < 0)
998 do_srr = FALSE;
999 else
1000 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001001# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001003 if (idx_sp < 0)
1004 do_sp = FALSE;
1005 else
1006 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001007# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001008 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (p != NULL)
1010 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001011 // Default for p_sp is "| tee", for p_srr is ">".
1012 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if ( fnamecmp(p, "csh") == 0
1014 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001015# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 || fnamecmp(p, "csh.exe") == 0
1017 || fnamecmp(p, "tcsh.exe") == 0
1018# endif
1019 )
1020 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001021# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (do_sp)
1023 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001024# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001026# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1030 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (do_srr)
1033 {
1034 p_srr = (char_u *)">&";
1035 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1036 }
1037 }
Mike Williams12795022021-06-28 20:53:58 +02001038# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001039 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1040 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001041 else if ( fnamecmp(p, "powershell") == 0
1042 || fnamecmp(p, "powershell.exe") == 0
1043 )
1044 {
1045# if defined(FEAT_QUICKFIX)
1046 if (do_sp)
1047 {
1048 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1049 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1050 }
1051# endif
1052 if (do_srr)
1053 {
1054 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1055 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1056 }
1057 }
1058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 else
Natanael Copa56318362021-05-06 18:46:35 +02001060 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if ( fnamecmp(p, "sh") == 0
1062 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001063 || fnamecmp(p, "mksh") == 0
1064 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001066 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001068 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001069 || fnamecmp(p, "ash") == 0
1070 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001071 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001072# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 || fnamecmp(p, "cmd") == 0
1074 || fnamecmp(p, "sh.exe") == 0
1075 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001076 || fnamecmp(p, "mksh.exe") == 0
1077 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001079 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 || fnamecmp(p, "bash.exe") == 0
1081 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001082 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001083 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001085 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001087# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (do_sp)
1089 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001090# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001092# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001093 if (fnamecmp(p, "pwsh") == 0)
1094 p_sp = (char_u *)">%s 2>&1";
1095 else
1096 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1099 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001100# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 if (do_srr)
1102 {
1103 p_srr = (char_u *)">%s 2>&1";
1104 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1105 }
1106 }
1107 vim_free(p);
1108 }
1109#endif
1110
Bram Moolenaar4f974752019-02-17 17:44:42 +01001111#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001113 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1114 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001116 * set, but only if they have not been set before.
1117 * Default values depend on shell (cmd.exe is default shell):
1118 *
1119 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001120 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001121 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001122 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001123 * "sh" like shells - "-c" "\""
1124 *
1125 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 */
Mike Williams12795022021-06-28 20:53:58 +02001127 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1128 {
1129 int idx_opt;
1130
1131 idx_opt = findoption((char_u *)"shcf");
1132 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1133 {
1134 p_shcf = (char_u*)"-Command";
1135 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1136 }
1137
1138 idx_opt = findoption((char_u *)"sxq");
1139 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1140 {
1141 p_sxq = (char_u*)"\"";
1142 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1143 }
1144 }
1145 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 {
1147 int idx3;
1148
1149 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001150 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
1152 p_shcf = (char_u *)"-c";
1153 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1154 }
1155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001156 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001158 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 p_sxq = (char_u *)"\"";
1161 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001164 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1165 {
1166 int idx3;
1167
1168 /*
1169 * cmd.exe on Windows will strip the first and last double quote given
1170 * on the command line, e.g. most of the time things like:
1171 * cmd /c "my path/to/echo" "my args to echo"
1172 * become:
1173 * my path/to/echo" "my args to echo
1174 * when executed.
1175 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001176 * To avoid this, set shellxquote to surround the command in
1177 * parenthesis. This appears to make most commands work, without
1178 * breaking commands that worked previously, such as
1179 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001180 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001181 idx3 = findoption((char_u *)"sxq");
1182 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1183 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001184 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001185 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1186 }
1187
Bram Moolenaara64ba222012-02-12 23:23:31 +01001188 idx3 = findoption((char_u *)"shcf");
1189 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1190 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001191 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001192 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1193 }
1194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#endif
1196
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001197 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001198 {
1199 int idx_ffs = findoption((char_u *)"ffs");
1200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001201 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001202 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1203 set_fileformat(default_fileformat(), OPT_LOCAL);
1204 }
1205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207}
1208
1209#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1210/*
1211 * When 'helplang' is still at its default value, set it to "lang".
1212 * Only the first two characters of "lang" are used.
1213 */
1214 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001215set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216{
1217 int idx;
1218
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001219 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 return;
1221 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001222 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1223 return;
1224
1225 if (options[idx].flags & P_ALLOCED)
1226 free_string_option(p_hlg);
1227 p_hlg = vim_strsave(lang);
1228 if (p_hlg == NULL)
1229 p_hlg = empty_option;
1230 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001232 // zh_CN becomes "cn", zh_TW becomes "tw"
1233 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001234 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001235 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1236 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001237 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001238 // any C like setting, such as C.UTF-8, becomes "en"
1239 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1240 {
1241 p_hlg[0] = 'e';
1242 p_hlg[1] = 'n';
1243 }
1244 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001246 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247}
1248#endif
1249
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250/*
1251 * 'title' and 'icon' only default to true if they have not been set or reset
1252 * in .vimrc and we can read the old value.
1253 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1254 * they can be reset. This reduces startup time when using X on a remote
1255 * machine.
1256 */
1257 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001258set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259{
1260 int idx1;
1261 long val;
1262
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001263 // If GUI is (going to be) used, we can always set the window title and
1264 // icon name. Saves a bit of time, because the X11 display server does
1265 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001267 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
1269#ifdef FEAT_GUI
1270 if (gui.starting || gui.in_use)
1271 val = TRUE;
1272 else
1273#endif
1274 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001275 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 p_title = val;
1277 }
1278 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001279 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001281 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001283
1284#ifdef FEAT_GUI
1285 if (gui.starting || gui.in_use)
1286 val = TRUE;
1287 else
1288#endif
1289 val = mch_can_restore_icon();
1290 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1291 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001294 void
1295ex_set(exarg_T *eap)
1296{
1297 int flags = 0;
1298
1299 if (eap->cmdidx == CMD_setlocal)
1300 flags = OPT_LOCAL;
1301 else if (eap->cmdidx == CMD_setglobal)
1302 flags = OPT_GLOBAL;
1303#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001304 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001305 ex_options(eap);
1306 else
1307#endif
1308 {
1309 if (eap->forceit)
1310 flags |= OPT_ONECOLUMN;
1311 (void)do_set(eap->arg, flags);
1312 }
1313}
1314
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001315/*
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001316 * :set boolean option prefix
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001317 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001318typedef enum {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001319 PREFIX_NO = 0, // "no" prefix
1320 PREFIX_NONE, // no prefix
1321 PREFIX_INV, // "inv" prefix
1322} set_prefix_T;
1323
1324/*
1325 * Return the prefix type for the option name in *argp.
1326 */
1327 static set_prefix_T
1328get_option_prefix(char_u **argp)
1329{
1330 int prefix = PREFIX_NONE;
1331 char_u *arg = *argp;
1332
1333 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1334 {
1335 prefix = PREFIX_NO;
1336 arg += 2;
1337 }
1338 else if (STRNCMP(arg, "inv", 3) == 0)
1339 {
1340 prefix = PREFIX_INV;
1341 arg += 3;
1342 }
1343
1344 *argp = arg;
1345 return prefix;
1346}
1347
1348/*
1349 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1350 * and the option name length in "*lenp". For a <t_xx> option, return the key
1351 * number in "*keyp".
1352 *
1353 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1354 * otherwise returns OK.
1355 */
1356 static int
1357parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1358{
1359 int key = 0;
1360 int len;
1361 int opt_idx;
1362
1363 if (*arg == '<')
1364 {
1365 opt_idx = -1;
1366 // look out for <t_>;>
1367 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1368 len = 5;
1369 else
1370 {
1371 len = 1;
1372 while (arg[len] != NUL && arg[len] != '>')
1373 ++len;
1374 }
1375 if (arg[len] != '>')
1376 return FAIL;
1377
1378 arg[len] = NUL; // put NUL after name
1379 if (arg[1] == 't' && arg[2] == '_') // could be term code
1380 opt_idx = findoption(arg + 1);
1381 arg[len++] = '>'; // restore '>'
1382 if (opt_idx == -1)
1383 key = find_key_option(arg + 1, TRUE);
1384 }
1385 else
1386 {
1387 int nextchar; // next non-white char after option name
1388
1389 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001390 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001391 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1392 len = 4;
1393 else
1394 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1395 ++len;
1396 nextchar = arg[len];
1397 arg[len] = NUL; // put NUL after name
1398 opt_idx = findoption(arg);
1399 arg[len] = nextchar; // restore nextchar
1400 if (opt_idx == -1)
1401 key = find_key_option(arg, FALSE);
1402 }
1403
1404 *keyp = key;
1405 *lenp = len;
1406 *opt_idxp = opt_idx;
1407
1408 return OK;
1409}
1410
1411/*
1412 * Get the option operator (+=, ^=, -=).
1413 */
1414 static set_op_T
1415get_opt_op(char_u *arg)
1416{
1417 set_op_T op = OP_NONE;
1418
1419 if (*arg != NUL && *(arg + 1) == '=')
1420 {
1421 if (*arg == '+')
1422 op = OP_ADDING; // "+="
1423 else if (*arg == '^')
1424 op = OP_PREPENDING; // "^="
1425 else if (*arg == '-')
1426 op = OP_REMOVING; // "-="
1427 }
1428
1429 return op;
1430}
1431
1432/*
1433 * Validate whether the value of the option in "opt_idx" can be changed.
1434 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1435 * if it can be changed.
1436 */
1437 static int
1438validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1439{
1440 // Skip all options that are not window-local (used when showing
1441 // an already loaded buffer in a window).
1442 if ((opt_flags & OPT_WINONLY)
1443 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1444 return FAIL;
1445
1446 // Skip all options that are window-local (used for :vimgrep).
1447 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1448 && options[opt_idx].var == VAR_WIN)
1449 return FAIL;
1450
1451 // Disallow changing some options from modelines.
1452 if (opt_flags & OPT_MODELINE)
1453 {
1454 if (flags & (P_SECURE | P_NO_ML))
1455 {
1456 *errmsg = e_not_allowed_in_modeline;
1457 return FAIL;
1458 }
1459 if ((flags & P_MLE) && !p_mle)
1460 {
1461 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1462 return FAIL;
1463 }
1464#ifdef FEAT_DIFF
1465 // In diff mode some options are overruled. This avoids that
1466 // 'foldmethod' becomes "marker" instead of "diff" and that
1467 // "wrap" gets set.
1468 if (curwin->w_p_diff
1469 && opt_idx >= 0 // shut up coverity warning
1470 && (
1471# ifdef FEAT_FOLDING
1472 options[opt_idx].indir == PV_FDM ||
1473# endif
1474 options[opt_idx].indir == PV_WRAP))
1475 return FAIL;
1476#endif
1477 }
1478
1479#ifdef HAVE_SANDBOX
1480 // Disallow changing some options in the sandbox
1481 if (sandbox != 0 && (flags & P_SECURE))
1482 {
1483 *errmsg = e_not_allowed_in_sandbox;
1484 return FAIL;
1485 }
1486#endif
1487
1488 return OK;
1489}
1490
Bram Moolenaar47403942022-09-21 21:12:53 +01001491/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001492 * Get the Vim/Vi default value for a string option.
1493 */
1494 static char_u *
1495stropt_get_default_val(
1496 int opt_idx,
1497 char_u *varp,
1498 int flags,
1499 int cp_val)
1500{
1501 char_u *newval;
1502
1503 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1504 ? VI_DEFAULT : VIM_DEFAULT];
1505 if ((char_u **)varp == &p_bg)
1506 {
1507 // guess the value of 'background'
1508#ifdef FEAT_GUI
1509 if (gui.in_use)
1510 newval = gui_bg_default();
1511 else
1512#endif
1513 newval = term_bg_default();
1514 }
1515 else if ((char_u **)varp == &p_fencs && enc_utf8)
1516 newval = fencs_utf8_default;
1517
1518 // expand environment variables and ~ since the default value was
1519 // already expanded, only required when an environment variable was set
1520 // later
1521 if (newval == NULL)
1522 newval = empty_option;
1523 else
1524 {
1525 char_u *s = option_expand(opt_idx, newval);
1526 if (s == NULL)
1527 s = newval;
1528 newval = vim_strsave(s);
1529 }
1530
1531 return newval;
1532}
1533
1534/*
1535 * Convert the 'backspace' option number value to a string: for adding,
1536 * prepending and removing string.
1537 */
1538 static void
1539opt_backspace_nr2str(
1540 char_u *varp,
1541 char_u **origval_p,
1542 char_u **origval_l_p,
1543 char_u **origval_g_p,
1544 char_u **oldval_p)
1545{
1546 int i = getdigits((char_u **)varp);
1547
1548 switch (i)
1549 {
1550 case 0:
1551 *(char_u **)varp = empty_option;
1552 break;
1553 case 1:
1554 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1555 break;
1556 case 2:
1557 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1558 break;
1559 case 3:
1560 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1561 break;
1562 }
1563 vim_free(*oldval_p);
1564 if (*origval_p == *oldval_p)
1565 *origval_p = *(char_u **)varp;
1566 if (*origval_l_p == *oldval_p)
1567 *origval_l_p = *(char_u **)varp;
1568 if (*origval_g_p == *oldval_p)
1569 *origval_g_p = *(char_u **)varp;
1570 *oldval_p = *(char_u **)varp;
1571}
1572
1573/*
1574 * Convert the 'whichwrap' option number value to a string, for backwards
1575 * compatibility with Vim 3.0.
1576 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1577 */
1578 static char_u *
1579opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1580{
1581 *whichwrap = NUL;
1582 int i = getdigits(argp);
1583 if (i & 1)
1584 STRCAT(whichwrap, "b,");
1585 if (i & 2)
1586 STRCAT(whichwrap, "s,");
1587 if (i & 4)
1588 STRCAT(whichwrap, "h,l,");
1589 if (i & 8)
1590 STRCAT(whichwrap, "<,>,");
1591 if (i & 16)
1592 STRCAT(whichwrap, "[,],");
1593 if (*whichwrap != NUL) // remove trailing ,
1594 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1595
1596 return whichwrap;
1597}
1598
1599/*
1600 * Copy the new string value into allocated memory for the option.
1601 * Can't use set_string_option_direct(), because we need to remove the
1602 * backslashes.
1603 */
1604 static char_u *
1605stropt_copy_value(
1606 char_u *origval,
1607 char_u **argp,
1608 set_op_T op,
1609 int flags UNUSED)
1610{
1611 char_u *arg = *argp;
1612 unsigned newlen;
1613 char_u *newval;
1614 char_u *s = NULL;
1615
1616 // get a bit too much
1617 newlen = (unsigned)STRLEN(arg) + 1;
1618 if (op != OP_NONE)
1619 newlen += (unsigned)STRLEN(origval) + 1;
1620 newval = alloc(newlen);
1621 if (newval == NULL) // out of mem, don't change
1622 return NULL;
1623 s = newval;
1624
1625 // Copy the string, skip over escaped chars.
1626 // For MS-DOS and WIN32 backslashes before normal file name characters
1627 // are not removed, and keep backslash at start, for "\\machine\path",
1628 // but do remove it for "\\\\machine\\path".
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001629 // The reverse is found in escape_option_str_cmdline().
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001630 while (*arg != NUL && !VIM_ISWHITE(*arg))
1631 {
1632 int i;
1633
1634 if (*arg == '\\' && arg[1] != NUL
1635#ifdef BACKSLASH_IN_FILENAME
1636 && !((flags & P_EXPAND)
1637 && vim_isfilec(arg[1])
1638 && !VIM_ISWHITE(arg[1])
1639 && (arg[1] != '\\'
1640 || (s == newval && arg[2] != '\\')))
1641#endif
1642 )
1643 ++arg; // remove backslash
1644 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1645 {
1646 // copy multibyte char
1647 mch_memmove(s, arg, (size_t)i);
1648 arg += i;
1649 s += i;
1650 }
1651 else
1652 *s++ = *arg++;
1653 }
1654 *s = NUL;
1655
1656 *argp = arg;
1657 return newval;
1658}
1659
1660/*
1661 * Expand environment variables and ~ in string option value 'newval'.
1662 */
1663 static char_u *
1664stropt_expand_envvar(
1665 int opt_idx,
1666 char_u *origval,
1667 char_u *newval,
1668 set_op_T op)
1669{
1670 char_u *s = option_expand(opt_idx, newval);
1671 if (s == NULL)
1672 return newval;
1673
1674 vim_free(newval);
1675 unsigned newlen = (unsigned)STRLEN(s) + 1;
1676 if (op != OP_NONE)
1677 newlen += (unsigned)STRLEN(origval) + 1;
1678
1679 newval = alloc(newlen);
1680 if (newval == NULL)
1681 return NULL;
1682
1683 STRCPY(newval, s);
1684
1685 return newval;
1686}
1687
1688/*
1689 * Concatenate the original and new values of a string option, adding a "," if
1690 * needed.
1691 */
1692 static void
1693stropt_concat_with_comma(
1694 char_u *origval,
1695 char_u *newval,
1696 set_op_T op,
1697 int flags)
1698{
1699 int len = 0;
1700
1701 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1702 if (op == OP_ADDING)
1703 {
1704 len = (int)STRLEN(origval);
1705 // strip a trailing comma, would get 2
1706 if (comma && len > 1
1707 && (flags & P_ONECOMMA) == P_ONECOMMA
1708 && origval[len - 1] == ','
1709 && origval[len - 2] != '\\')
1710 len--;
1711 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1712 mch_memmove(newval, origval, (size_t)len);
1713 }
1714 else
1715 {
1716 len = (int)STRLEN(newval);
1717 STRMOVE(newval + len + comma, origval);
1718 }
1719 if (comma)
1720 newval[len] = ',';
1721}
1722
1723/*
1724 * Remove a value from a string option. Copy string option value in "origval"
1725 * to "newval" and then remove the string "strval" of length "len".
1726 */
1727 static void
1728stropt_remove_val(
1729 char_u *origval,
1730 char_u *newval,
1731 int flags,
1732 char_u *strval,
1733 int len)
1734{
1735 // Remove newval[] from origval[]. (Note: "len" has been set above
1736 // and is used here).
1737 STRCPY(newval, origval);
1738 if (*strval)
1739 {
1740 // may need to remove a comma
1741 if (flags & P_COMMA)
1742 {
1743 if (strval == origval)
1744 {
1745 // include comma after string
1746 if (strval[len] == ',')
1747 ++len;
1748 }
1749 else
1750 {
1751 // include comma before string
1752 --strval;
1753 ++len;
1754 }
1755 }
1756 STRMOVE(newval + (strval - origval), strval + len);
1757 }
1758}
1759
1760/*
1761 * Remove flags that appear twice in the string option value 'newval'.
1762 */
1763 static void
1764stropt_remove_dupflags(char_u *newval, int flags)
1765{
1766 char_u *s = newval;
1767
1768 // Remove flags that appear twice.
1769 while (*s)
1770 {
1771 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1772 if (flags & P_ONECOMMA)
1773 {
1774 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1775 {
1776 // Remove the duplicated value and the next comma.
1777 STRMOVE(s, s + 2);
1778 continue;
1779 }
1780 }
1781 else
1782 {
1783 if ((!(flags & P_COMMA) || *s != ',')
1784 && vim_strchr(s + 1, *s) != NULL)
1785 {
1786 STRMOVE(s, s + 1);
1787 continue;
1788 }
1789 }
1790 ++s;
1791 }
1792}
1793
1794/*
1795 * Get the string value specified for a ":set" command. The following set
1796 * options are supported:
1797 * set {opt}&
1798 * set {opt}<
1799 * set {opt}={val}
1800 * set {opt}:{val}
1801 */
1802 static char_u *
1803stropt_get_newval(
1804 int nextchar,
1805 int opt_idx,
1806 char_u **argp,
1807 char_u *varp,
1808 char_u **origval_arg,
1809 char_u **origval_l_arg,
1810 char_u **origval_g_arg,
1811 char_u **oldval_arg,
1812 set_op_T *op_arg,
1813 int flags,
1814 int cp_val)
1815{
1816 char_u *arg = *argp;
1817 char_u *origval = *origval_arg;
1818 char_u *origval_l = *origval_l_arg;
1819 char_u *origval_g = *origval_g_arg;
1820 char_u *oldval = *oldval_arg;
1821 set_op_T op = *op_arg;
1822 char_u *save_arg = NULL;
1823 char_u *newval;
1824 char_u *s = NULL;
1825 char_u whichwrap[80];
1826
1827 if (nextchar == '&') // set to default val
1828 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1829 else if (nextchar == '<') // set to global val
1830 newval = vim_strsave(*(char_u **)get_varp_scope(
1831 &(options[opt_idx]), OPT_GLOBAL));
1832 else
1833 {
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001834 ++arg; // jump to after the '=' or ':'
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001835
1836 // Set 'keywordprg' to ":help" if an empty
1837 // value was passed to :set by the user.
1838 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1839 {
1840 save_arg = arg;
1841 arg = (char_u *)":help";
1842 }
1843 // Convert 'backspace' number to string
1844 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1845 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1846 &oldval);
1847 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1848 {
1849 // Convert 'whichwrap' number to string, for backwards
1850 // compatibility with Vim 3.0.
1851 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1852 save_arg = arg;
1853 arg = t;
1854 }
1855 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1856 // version 3.0
1857 else if (*arg == '>' && (varp == (char_u *)&p_dir
1858 || varp == (char_u *)&p_bdir))
1859 ++arg;
1860
1861 // Copy the new string into allocated memory.
1862 newval = stropt_copy_value(origval, &arg, op, flags);
1863 if (newval == NULL)
1864 goto done;
1865
1866 // Expand environment variables and ~.
1867 // Don't do it when adding without inserting a comma.
1868 if (op == OP_NONE || (flags & P_COMMA))
1869 {
1870 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1871 if (newval == NULL)
1872 goto done;
1873 }
1874
1875 // locate newval[] in origval[] when removing it and when adding to
1876 // avoid duplicates
1877 int len = 0;
1878 if (op == OP_REMOVING || (flags & P_NODUP))
1879 {
1880 len = (int)STRLEN(newval);
1881 s = find_dup_item(origval, newval, flags);
1882
1883 // do not add if already there
1884 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1885 {
1886 op = OP_NONE;
1887 STRCPY(newval, origval);
1888 }
1889
1890 // if no duplicate, move pointer to end of original value
1891 if (s == NULL)
1892 s = origval + (int)STRLEN(origval);
1893 }
1894
1895 // concatenate the two strings; add a ',' if needed
1896 if (op == OP_ADDING || op == OP_PREPENDING)
1897 stropt_concat_with_comma(origval, newval, op, flags);
1898 else if (op == OP_REMOVING)
1899 // Remove newval[] from origval[]. (Note: "len" has been set above
1900 // and is used here).
1901 stropt_remove_val(origval, newval, flags, s, len);
1902
1903 if (flags & P_FLAGLIST)
1904 // Remove flags that appear twice.
1905 stropt_remove_dupflags(newval, flags);
1906 }
1907
1908done:
1909 if (save_arg != NULL)
1910 arg = save_arg; // arg was temporarily changed, restore it
1911 *argp = arg;
1912 *origval_arg = origval;
1913 *origval_l_arg = origval_l;
1914 *origval_g_arg = origval_g;
1915 *oldval_arg = oldval;
1916 *op_arg = op;
1917
1918 return newval;
1919}
1920
1921/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001922 * Part of do_set() for string options.
1923 * Returns FAIL on failure, do not process further options.
1924 */
1925 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001926do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001927 int opt_idx,
1928 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001929 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001930 int nextchar,
1931 set_op_T op_arg,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001932 long_u flags,
Bram Moolenaar47403942022-09-21 21:12:53 +01001933 int cp_val,
1934 char_u *varp_arg,
1935 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +01001936 size_t errbuflen,
Bram Moolenaar47403942022-09-21 21:12:53 +01001937 int *value_checked,
1938 char **errmsg)
1939{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001940 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001941 set_op_T op = op_arg;
1942 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001943 char_u *oldval = NULL; // previous value if *varp
1944 char_u *newval;
1945 char_u *origval = NULL;
1946 char_u *origval_l = NULL;
1947 char_u *origval_g = NULL;
1948#if defined(FEAT_EVAL)
1949 char_u *saved_origval = NULL;
1950 char_u *saved_origval_l = NULL;
1951 char_u *saved_origval_g = NULL;
1952 char_u *saved_newval = NULL;
1953#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01001954
1955 // When using ":set opt=val" for a global option
1956 // with a local value the local value will be
1957 // reset, use the global value here.
1958 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1959 && ((int)options[opt_idx].indir & PV_BOTH))
1960 varp = options[opt_idx].var;
1961
1962 // The old value is kept until we are sure that the new value is valid.
1963 oldval = *(char_u **)varp;
1964
1965 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1966 {
1967 origval_l = *(char_u **)get_varp_scope(
1968 &(options[opt_idx]), OPT_LOCAL);
1969 origval_g = *(char_u **)get_varp_scope(
1970 &(options[opt_idx]), OPT_GLOBAL);
1971
1972 // A global-local string option might have an empty option as value to
1973 // indicate that the global value should be used.
1974 if (((int)options[opt_idx].indir & PV_BOTH)
1975 && origval_l == empty_option)
1976 origval_l = origval_g;
1977 }
1978
1979 // When setting the local value of a global option, the old value may be
1980 // the global value.
1981 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1982 origval = *(char_u **)get_varp(&options[opt_idx]);
1983 else
1984 origval = oldval;
1985
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001986 // Get the new value for the option
1987 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
1988 &origval_l, &origval_g, &oldval, &op, flags,
1989 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01001990
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001991 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01001992 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00001993 if (newval == NULL)
1994 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01001995
1996#if defined(FEAT_EVAL)
1997 if (!starting
1998# ifdef FEAT_CRYPT
1999 && options[opt_idx].indir != PV_KEY
2000# endif
2001 && origval != NULL && newval != NULL)
2002 {
2003 // origval may be freed by did_set_string_option(), make a copy.
2004 saved_origval = vim_strsave(origval);
2005 // newval (and varp) may become invalid if the buffer is closed by
2006 // autocommands.
2007 saved_newval = vim_strsave(newval);
2008 if (origval_l != NULL)
2009 saved_origval_l = vim_strsave(origval_l);
2010 if (origval_g != NULL)
2011 saved_origval_g = vim_strsave(origval_g);
2012 }
2013#endif
2014
2015 {
2016 long_u *p = insecure_flag(opt_idx, opt_flags);
2017 int secure_saved = secure;
2018
2019 // When an option is set in the sandbox, from a modeline or in secure
2020 // mode, then deal with side effects in secure mode. Also when the
2021 // value was set with the P_INSECURE flag and is not completely
2022 // replaced.
2023 if ((opt_flags & OPT_MODELINE)
2024#ifdef HAVE_SANDBOX
2025 || sandbox != 0
2026#endif
2027 || (op != OP_NONE && (*p & P_INSECURE)))
2028 secure = 1;
2029
2030 // Handle side effects, and set the global value for ":set" on local
2031 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2032 // be triggered that can cause havoc.
2033 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002034 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002035 errbuflen, opt_flags, op, value_checked);
Bram Moolenaar47403942022-09-21 21:12:53 +01002036
2037 secure = secure_saved;
2038 }
2039
2040#if defined(FEAT_EVAL)
2041 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002042 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002043 saved_origval_l, saved_origval_g, saved_newval);
2044 vim_free(saved_origval);
2045 vim_free(saved_origval_l);
2046 vim_free(saved_origval_g);
2047 vim_free(saved_newval);
2048#endif
2049
Bram Moolenaar6f981142022-09-22 12:48:58 +01002050 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002051 return *errmsg == NULL ? OK : FAIL;
2052}
2053
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002055 * Set a boolean option.
2056 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002057 */
2058 static char *
2059do_set_option_bool(
2060 int opt_idx,
2061 int opt_flags,
2062 set_prefix_T prefix,
2063 long_u flags,
2064 char_u *varp,
2065 int nextchar,
2066 int afterchar,
2067 int cp_val)
2068
2069{
2070 varnumber_T value;
2071
2072 if (nextchar == '=' || nextchar == ':')
2073 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002074 if (opt_idx < 0 || varp == NULL)
2075 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002076
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002077 // ":set opt!": invert
2078 // ":set opt&": reset to default value
2079 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002080 if (nextchar == '!')
2081 value = *(int *)(varp) ^ 1;
2082 else if (nextchar == '&')
2083 value = (int)(long)(long_i)options[opt_idx].def_val[
2084 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2085 else if (nextchar == '<')
2086 {
2087 // For 'autoread' -1 means to use global value.
2088 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2089 value = -1;
2090 else
2091 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2092 }
2093 else
2094 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002095 // ":set invopt": invert
2096 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002097 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2098 return e_trailing_characters;
2099 if (prefix == PREFIX_INV)
2100 value = *(int *)(varp) ^ 1;
2101 else
2102 value = prefix == PREFIX_NO ? 0 : 1;
2103 }
2104
2105 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2106}
2107
2108/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002109 * Set a numeric option.
2110 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002111 */
2112 static char *
2113do_set_option_numeric(
2114 int opt_idx,
2115 int opt_flags,
2116 char_u **argp,
2117 int nextchar,
2118 set_op_T op,
2119 long_u flags,
2120 int cp_val,
2121 char_u *varp,
2122 char *errbuf,
2123 size_t errbuflen)
2124{
2125 char_u *arg = *argp;
2126 varnumber_T value;
2127 int i;
2128 char *errmsg = NULL;
2129
Bram Moolenaar546933f2023-02-06 16:40:49 +00002130 if (opt_idx < 0 || varp == NULL)
2131 return NULL; // "cannot happen"
2132 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002133 /*
2134 * Different ways to set a number option:
2135 * & set to default value
2136 * < set to global value
2137 * <xx> accept special key codes for 'wildchar'
2138 * c accept any non-digit for 'wildchar'
2139 * [-]0-9 set number
2140 * other error
2141 */
2142 ++arg;
2143 if (nextchar == '&')
2144 value = (long)(long_i)options[opt_idx].def_val[
2145 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2146 else if (nextchar == '<')
2147 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002148 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002149 // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002150 value = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002151 else if (opt_flags == OPT_LOCAL
2152 && ((long *)varp == &curwin->w_p_siso
2153 || (long *)varp == &curwin->w_p_so))
2154 // for 'scrolloff'/'sidescrolloff' -1 means using the global value
2155 value = -1;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002156 else
2157 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2158 }
2159 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2160 && (*arg == '<'
2161 || *arg == '^'
2162 || (*arg != NUL
2163 && (!arg[1] || VIM_ISWHITE(arg[1]))
2164 && !VIM_ISDIGIT(*arg))))
2165 {
2166 value = string_to_key(arg, FALSE);
2167 if (value == 0 && (long *)varp != &p_wcm)
2168 {
2169 errmsg = e_invalid_argument;
2170 goto skip;
2171 }
2172 }
2173 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2174 {
2175 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002176 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002177 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2178 {
2179 errmsg = e_number_required_after_equal;
2180 goto skip;
2181 }
2182 }
2183 else
2184 {
2185 errmsg = e_number_required_after_equal;
2186 goto skip;
2187 }
2188
2189 if (op == OP_ADDING)
2190 value = *(long *)varp + value;
2191 else if (op == OP_PREPENDING)
2192 value = *(long *)varp * value;
2193 else if (op == OP_REMOVING)
2194 value = *(long *)varp - value;
2195
2196 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2197 opt_flags);
2198
2199skip:
2200 *argp = arg;
2201 return errmsg;
2202}
2203
2204/*
2205 * Set a key code (t_xx) option
2206 */
2207 static char *
2208do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2209{
2210 char_u *arg = *argp;
2211 char_u *p;
2212
2213 if (nextchar == '&')
2214 {
2215 if (add_termcap_entry(key_name, TRUE) == FAIL)
2216 return e_not_found_in_termcap;
2217 }
2218 else
2219 {
2220 ++arg; // jump to after the '=' or ':'
2221 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2222 if (*p == '\\' && p[1] != NUL)
2223 ++p;
2224 nextchar = *p;
2225 *p = NUL;
2226 add_termcode(key_name, arg, FALSE);
2227 *p = nextchar;
2228 }
2229 if (full_screen)
2230 ttest(FALSE);
2231 redraw_all_later(UPD_CLEAR);
2232
2233 *argp = arg;
2234 return NULL;
2235}
2236
2237/*
2238 * Set an option to a new value.
2239 */
2240 static char *
2241do_set_option_value(
2242 int opt_idx,
2243 int opt_flags,
2244 char_u **argp,
2245 set_prefix_T prefix,
2246 set_op_T op,
2247 long_u flags,
2248 char_u *varp,
2249 char_u *key_name,
2250 int nextchar,
2251 int afterchar,
2252 int cp_val,
2253 int *stopopteval,
2254 char *errbuf,
2255 size_t errbuflen)
2256{
2257 int value_checked = FALSE;
2258 char *errmsg = NULL;
2259 char_u *arg = *argp;
2260
2261 if (flags & P_BOOL)
2262 {
2263 // boolean option
2264 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2265 nextchar, afterchar, cp_val);
2266 if (errmsg != NULL)
2267 goto skip;
2268 }
2269 else
2270 {
2271 // numeric or string option
2272 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2273 || prefix != PREFIX_NONE)
2274 {
2275 errmsg = e_invalid_argument;
2276 goto skip;
2277 }
2278
2279 if (flags & P_NUM)
2280 {
2281 // numeric option
2282 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2283 op, flags, cp_val, varp,
2284 errbuf, errbuflen);
2285 if (errmsg != NULL)
2286 goto skip;
2287 }
2288 else if (opt_idx >= 0)
2289 {
2290 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002291 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002292 flags, cp_val, varp, errbuf, errbuflen,
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002293 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002294 {
2295 if (errmsg != NULL)
2296 goto skip;
2297 *stopopteval = TRUE;
2298 goto skip;
2299 }
2300 }
2301 else
2302 {
2303 // key code option
2304 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2305 if (errmsg != NULL)
2306 goto skip;
2307 }
2308 }
2309
2310 if (opt_idx >= 0)
2311 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2312
2313skip:
2314 *argp = arg;
2315 return errmsg;
2316}
2317
2318/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002319 * Set an option to a new value.
2320 * Return NULL if OK, return an untranslated error message when something is
2321 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2322 */
2323 static char *
2324do_set_option(
2325 int opt_flags,
2326 char_u **argp,
2327 char_u *arg_start,
2328 char_u **startarg,
2329 int *did_show,
2330 int *stopopteval,
2331 char *errbuf,
2332 size_t errbuflen)
2333{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002334 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002335 char_u *arg;
2336 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2337 set_op_T op;
2338 long_u flags; // flags for current option
2339 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002340 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002341 int nextchar; // next non-white char after option name
2342 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002343 char *errmsg = NULL;
2344 int key;
2345 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002346
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002347 prefix = get_option_prefix(argp);
2348 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002349
2350 // find end of name
2351 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002352 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2353 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002354
2355 // remember character after option name
2356 afterchar = arg[len];
2357
2358 if (in_vim9script())
2359 {
2360 char_u *p = skipwhite(arg + len);
2361
2362 // disallow white space before =val, +=val, -=val, ^=val
2363 if (p > arg + len && (p[0] == '='
2364 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2365 && p[1] == '=')))
2366 {
2367 errmsg = e_no_white_space_allowed_between_option_and;
2368 arg = p;
2369 *startarg = p;
2370 goto skip;
2371 }
2372 }
2373 else
2374 // skip white space, allow ":set ai ?", ":set hlsearch !"
2375 while (VIM_ISWHITE(arg[len]))
2376 ++len;
2377
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002378 op = get_opt_op(arg + len);
2379 if (op != OP_NONE)
2380 len++;
2381
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002382 nextchar = arg[len];
2383
2384 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2385 {
2386 if (in_vim9script() && arg > arg_start
2387 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2388 errmsg = e_no_white_space_allowed_between_option_and;
2389 else
2390 errmsg = e_unknown_option;
2391 goto skip;
2392 }
2393
2394 if (opt_idx >= 0)
2395 {
2396 if (options[opt_idx].var == NULL) // hidden option: skip
2397 {
2398 // Only give an error message when requesting the value of
2399 // a hidden option, ignore setting it.
2400 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2401 && (!(options[opt_idx].flags & P_BOOL)
2402 || nextchar == '?'))
2403 errmsg = e_option_not_supported;
2404 goto skip;
2405 }
2406
2407 flags = options[opt_idx].flags;
2408 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2409 }
2410 else
2411 {
2412 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002413 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002414 if (key < 0)
2415 {
2416 key_name[0] = KEY2TERMCAP0(key);
2417 key_name[1] = KEY2TERMCAP1(key);
2418 }
2419 else
2420 {
2421 key_name[0] = KS_KEY;
2422 key_name[1] = (key & 0xff);
2423 }
2424 }
2425
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002426 // Make sure the option value can be changed.
2427 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002428 goto skip;
2429
Bram Moolenaar40b48722023-02-05 17:04:50 +00002430 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002431 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2432 {
2433 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002434 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2435 {
2436 if (arg[3] == 'm') // "opt&vim": set to Vim default
2437 {
2438 cp_val = FALSE;
2439 arg += 3;
2440 }
2441 else // "opt&vi": set to Vi default
2442 {
2443 cp_val = TRUE;
2444 arg += 2;
2445 }
2446 }
2447 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2448 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2449 {
2450 errmsg = e_trailing_characters;
2451 goto skip;
2452 }
2453 }
2454
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002455 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2456 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002457 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002458 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002459 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2460 && !(flags & P_BOOL)))
2461 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002462 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002463 if (*did_show)
2464 msg_putchar('\n'); // cursor below last one
2465 else
2466 {
2467 gotocmdline(TRUE); // cursor at status line
2468 *did_show = TRUE; // remember that we did a line
2469 }
2470 if (opt_idx >= 0)
2471 {
2472 showoneopt(&options[opt_idx], opt_flags);
2473#ifdef FEAT_EVAL
2474 if (p_verbose > 0)
2475 {
2476 // Mention where the option was last set.
2477 if (varp == options[opt_idx].var)
2478 last_set_msg(options[opt_idx].script_ctx);
2479 else if ((int)options[opt_idx].indir & PV_WIN)
2480 last_set_msg(curwin->w_p_script_ctx[
2481 (int)options[opt_idx].indir & PV_MASK]);
2482 else if ((int)options[opt_idx].indir & PV_BUF)
2483 last_set_msg(curbuf->b_p_script_ctx[
2484 (int)options[opt_idx].indir & PV_MASK]);
2485 }
2486#endif
2487 }
2488 else
2489 {
2490 char_u *p;
2491
2492 p = find_termcode(key_name);
2493 if (p == NULL)
2494 {
2495 errmsg = e_key_code_not_set;
2496 goto skip;
2497 }
2498 else
2499 (void)show_one_termcode(key_name, p, TRUE);
2500 }
2501 if (nextchar != '?'
2502 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2503 errmsg = e_trailing_characters;
2504 }
2505 else
2506 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002507 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2508 flags, varp, key_name, nextchar,
2509 afterchar, cp_val, stopopteval, errbuf,
2510 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002511 }
2512
2513skip:
2514 *argp = arg;
2515 return errmsg;
2516}
2517
2518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 * Parse 'arg' for option settings.
2520 *
2521 * 'arg' may be IObuff, but only when no errors can be present and option
2522 * does not need to be expanded with option_expand().
2523 * "opt_flags":
2524 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002525 * OPT_GLOBAL for ":setglobal"
2526 * OPT_LOCAL for ":setlocal" and a modeline
2527 * OPT_MODELINE for a modeline
2528 * OPT_WINONLY to only set window-local options
2529 * OPT_NOWIN to skip setting window-local options
2530 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002532 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 */
2534 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002535do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002536 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002537 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002539 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002541 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
2543 if (*arg == NUL)
2544 {
2545 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002546 did_show = TRUE;
2547 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002550 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002552 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002553 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002555 // ":set all" show all options.
2556 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 arg += 3;
2558 if (*arg == '&')
2559 {
2560 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002561 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002563 didset_options();
2564 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002565 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
2567 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002570 did_show = TRUE;
2571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002573 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
2575 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002576 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002577 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 arg += 7;
2579 }
2580 else
2581 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002582 int stopopteval = FALSE;
2583 char *errmsg = NULL;
Christian Brabandtb39b2402023-11-29 11:34:05 +01002584 char errbuf[ERR_BUFLEN];
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002585 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002587 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2588 &did_show, &stopopteval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002589 ERR_BUFLEN);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002590 if (stopopteval)
2591 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002593 // Advance to next argument.
2594 // - skip until a blank found, taking care of backslashes
2595 // - skip blanks
2596 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 for (i = 0; i < 2 ; ++i)
2598 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002599 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 if (*arg++ == '\\' && *arg != NUL)
2601 ++arg;
2602 arg = skipwhite(arg);
2603 if (*arg != '=')
2604 break;
2605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002607 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002609 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2610 i = (int)STRLEN(IObuff) + 2;
2611 if (i + (arg - startarg) < IOSIZE)
2612 {
2613 // append the argument with the error
2614 STRCAT(IObuff, ": ");
2615 mch_memmove(IObuff + i, startarg, (arg - startarg));
2616 IObuff[i + (arg - startarg)] = NUL;
2617 }
2618 // make sure all characters are printable
2619 trans_characters(IObuff, IOSIZE);
2620
2621 ++no_wait_return; // wait_return() done later
2622 emsg((char *)IObuff); // show error highlighted
2623 --no_wait_return;
2624
2625 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
2628
2629 arg = skipwhite(arg);
2630 }
2631
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002632theend:
2633 if (silent_mode && did_show)
2634 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002635 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002636 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002637 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002638 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002639 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002640 out_flush();
2641 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002642 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002643 }
2644
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 return OK;
2646}
2647
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002648/*
2649 * Call this when an option has been given a new value through a user command.
2650 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2651 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002652 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002653did_set_option(
2654 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002655 int opt_flags, // possibly with OPT_MODELINE
2656 int new_value, // value was replaced completely
2657 int value_checked) // value was checked to be safe, no need to set the
2658 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002659{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002660 long_u *p;
2661
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002662 options[opt_idx].flags |= P_WAS_SET;
2663
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002664 // When an option is set in the sandbox, from a modeline or in secure mode
2665 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2666 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002667 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002668 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002669#ifdef HAVE_SANDBOX
2670 || sandbox != 0
2671#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002672 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002673 *p = *p | P_INSECURE;
2674 else if (new_value)
2675 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002676}
2677
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678/*
2679 * Convert a key name or string into a key value.
2680 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002681 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002683 int
2684string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
2686 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002687 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 if (*arg == '^')
2689 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002690 if (multi_byte)
2691 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return *arg;
2693}
2694
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695/*
2696 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2697 * maketitle() to create and display it.
2698 * When switching the title or icon off, call mch_restore_title() to get
2699 * the old value back.
2700 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002701 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002702did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
2704 if (starting != NO_SCREEN
2705#ifdef FEAT_GUI
2706 && !gui.starting
2707#endif
2708 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
2712/*
2713 * set_options_bin - called when 'bin' changes value.
2714 */
2715 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002716set_options_bin(
2717 int oldval,
2718 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002719 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002721 // The option values that are changed when 'bin' changes are
2722 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (newval)
2724 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
2727 if (!(opt_flags & OPT_GLOBAL))
2728 {
2729 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2730 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2731 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2732 curbuf->b_p_et_nobin = curbuf->b_p_et;
2733 }
2734 if (!(opt_flags & OPT_LOCAL))
2735 {
2736 p_tw_nobin = p_tw;
2737 p_wm_nobin = p_wm;
2738 p_ml_nobin = p_ml;
2739 p_et_nobin = p_et;
2740 }
2741 }
2742
2743 if (!(opt_flags & OPT_GLOBAL))
2744 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 curbuf->b_p_tw = 0; // no automatic line wrap
2746 curbuf->b_p_wm = 0; // no automatic line wrap
2747 curbuf->b_p_ml = 0; // no modelines
2748 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
2750 if (!(opt_flags & OPT_LOCAL))
2751 {
2752 p_tw = 0;
2753 p_wm = 0;
2754 p_ml = FALSE;
2755 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002759 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
2761 if (!(opt_flags & OPT_GLOBAL))
2762 {
2763 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2764 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2765 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2766 curbuf->b_p_et = curbuf->b_p_et_nobin;
2767 }
2768 if (!(opt_flags & OPT_LOCAL))
2769 {
2770 p_tw = p_tw_nobin;
2771 p_wm = p_wm_nobin;
2772 p_ml = p_ml_nobin;
2773 p_et = p_et_nobin;
2774 }
2775 }
Christian Brabandt757593c2023-08-22 21:44:10 +02002776#if defined(FEAT_EVAL) || defined(PROTO)
2777 // Remember where the dependent option were reset
2778 didset_options_sctx(opt_flags, p_bin_dep_opts);
2779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780}
2781
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782/*
2783 * Expand environment variables for some string options.
2784 * These string options cannot be indirect!
2785 * If "val" is NULL expand the current value of the option.
2786 * Return pointer to NameBuff, or NULL when not expanded.
2787 */
2788 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002789option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002791 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2793 return NULL;
2794
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002795 // If val is longer than MAXPATHL no meaningful expansion can be done,
2796 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (val != NULL && STRLEN(val) > MAXPATHL)
2798 return NULL;
2799
2800 if (val == NULL)
2801 val = *(char_u **)options[opt_idx].var;
2802
2803 /*
2804 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2805 * Escape spaces when expanding 'tags', they are used to separate file
2806 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002807 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
2809 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002810 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002811#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002812 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2813#endif
2814 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002815 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 return NULL;
2817
2818 return NameBuff;
2819}
2820
2821/*
2822 * After setting various option values: recompute variables that depend on
2823 * option values.
2824 */
2825 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002826didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002828 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 (void)init_chartab();
2830
Bram Moolenaardac13472019-09-16 21:06:21 +02002831 didset_string_options();
2832
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002833#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002834 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002835 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002836 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002837 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002838#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002839 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002840 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002841#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002842 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002843 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002844#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002845 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002846}
2847
2848/*
2849 * More side effects of setting options.
2850 */
2851 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002852didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002853{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002854 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002855 (void)highlight_changed();
2856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002857 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002858 check_opt_wim();
2859
Bram Moolenaareed9d462021-02-15 20:38:25 +01002860 // Parse default for 'listchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002861 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002862
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002863 // Parse default for 'fillchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002864 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002865
2866#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002867 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002868 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002869#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002870#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002871 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002872 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002873 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002874 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876}
2877
2878/*
2879 * Check for string options that are NULL (normally only termcap options).
2880 */
2881 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002882check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
2884 int opt_idx;
2885
2886 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2887 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2888 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2889}
2890
2891/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002892 * Return the option index found by a pointer into term_strings[].
2893 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002895 int
2896get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002898 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
2900 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2901 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002902 return opt_idx;
2903 return -1; // cannot happen: didn't find it!
2904}
2905
2906/*
2907 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2908 * Return the option index or -1 if not found.
2909 */
2910 int
2911set_term_option_alloced(char_u **p)
2912{
2913 int opt_idx = get_term_opt_idx(p);
2914
2915 if (opt_idx >= 0)
2916 options[opt_idx].flags |= P_ALLOCED;
2917 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918}
2919
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002920#if defined(FEAT_EVAL) || defined(PROTO)
2921/*
2922 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2923 * Return FALSE when it wasn't.
2924 * Return -1 for an unknown option.
2925 */
2926 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002927was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002928{
2929 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002930 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002931
2932 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002933 {
2934 flagp = insecure_flag(idx, opt_flags);
2935 return (*flagp & P_INSECURE) != 0;
2936 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002937 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002938 return -1;
2939}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002940
2941/*
2942 * Get a pointer to the flags used for the P_INSECURE flag of option
2943 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002944 * NOTE: Caller must make sure that "curwin" is set to the window from which
2945 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002946 */
2947 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002948insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002949{
2950 if (opt_flags & OPT_LOCAL)
2951 switch ((int)options[opt_idx].indir)
2952 {
2953#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002954 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002955#endif
2956#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002957# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002958 case PV_FDE: return &curwin->w_p_fde_flags;
2959 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002960# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002961# ifdef FEAT_BEVAL
2962 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2963# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002964 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002965 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002966# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002967 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002968# endif
2969#endif
2970 }
2971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002973 return &options[opt_idx].flags;
2974}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002975#endif
2976
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002977/*
2978 * Redraw the window title and/or tab page text later.
2979 */
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002980 void
2981redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002982{
2983 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002984 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002985}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002988 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2989 * characters or characters in "allowed".
2990 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002991 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002992valid_name(char_u *val, char *allowed)
2993{
2994 char_u *s;
2995
2996 for (s = val; *s != NUL; ++s)
2997 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2998 return FALSE;
2999 return TRUE;
3000}
3001
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003002#if defined(FEAT_EVAL) || defined(PROTO)
3003/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003004 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003005 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003006 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003007 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003008set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003009{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003010 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3011 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003012 sctx_T new_script_ctx = script_ctx;
3013
Bram Moolenaar51258742020-05-03 17:19:33 +02003014 // Modeline already has the line number set.
3015 if (!(opt_flags & OPT_MODELINE))
3016 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003018 // Remember where the option was set. For local options need to do that
3019 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003020 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003021 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003022 if (both || (opt_flags & OPT_LOCAL))
3023 {
3024 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003025 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003026 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003027 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003028 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003029 if (both)
3030 // also setting the "all buffers" value
3031 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3032 new_script_ctx;
3033 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003034 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003035}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003036
3037/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003038 * Get the script context of global option "name".
3039 *
3040 */
3041 sctx_T *
3042get_option_sctx(char *name)
3043{
3044 int idx = findoption((char_u *)name);
3045
3046 if (idx >= 0)
3047 return &options[idx].script_ctx;
3048 siemsg("no such option: %s", name);
3049 return NULL;
3050}
3051
3052/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003053 * Set the script_ctx for a termcap option.
3054 * "name" must be the two character code, e.g. "RV".
3055 * When "name" is NULL use "opt_idx".
3056 */
3057 void
3058set_term_option_sctx_idx(char *name, int opt_idx)
3059{
3060 char_u buf[5];
3061 int idx;
3062
3063 if (name == NULL)
3064 idx = opt_idx;
3065 else
3066 {
3067 buf[0] = 't';
3068 buf[1] = '_';
3069 buf[2] = name[0];
3070 buf[3] = name[1];
3071 buf[4] = 0;
3072 idx = findoption(buf);
3073 }
3074 if (idx >= 0)
3075 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3076}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003077#endif
3078
Bram Moolenaarf4140482020-02-15 23:06:45 +01003079#if defined(FEAT_EVAL)
3080/*
3081 * Apply the OptionSet autocommand.
3082 */
3083 static void
3084apply_optionset_autocmd(
3085 int opt_idx,
3086 long opt_flags,
3087 long oldval,
3088 long oldval_g,
3089 long newval,
3090 char *errmsg)
3091{
3092 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3093
3094 // Don't do this while starting up, failure or recursively.
3095 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3096 return;
3097
3098 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3099 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3100 oldval_g);
3101 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3102 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3103 (opt_flags & OPT_LOCAL) ? "local" : "global");
3104 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3105 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3106 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3107 if (opt_flags & OPT_LOCAL)
3108 {
3109 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3110 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3111 }
3112 if (opt_flags & OPT_GLOBAL)
3113 {
3114 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3115 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3116 }
3117 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3118 {
3119 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3120 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3121 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3122 }
3123 if (opt_flags & OPT_MODELINE)
3124 {
3125 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3126 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3127 }
3128 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3129 NULL, FALSE, NULL);
3130 reset_v_option_vars();
3131}
3132#endif
3133
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003134#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003135/*
3136 * Process the updated 'arabic' option value.
3137 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003138 char *
3139did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003140{
3141 char *errmsg = NULL;
3142
3143 if (curwin->w_p_arab)
3144 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003145 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003146 if (!p_tbidi)
3147 {
3148 // set rightleft mode
3149 if (!curwin->w_p_rl)
3150 {
3151 curwin->w_p_rl = TRUE;
3152 changed_window_setting();
3153 }
3154
3155 // Enable Arabic shaping (major part of what Arabic requires)
3156 if (!p_arshape)
3157 {
3158 p_arshape = TRUE;
3159 redraw_later_clear();
3160 }
3161 }
3162
3163 // Arabic requires a utf-8 encoding, inform the user if it's not
3164 // set.
3165 if (STRCMP(p_enc, "utf-8") != 0)
3166 {
3167 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3168
3169 msg_source(HL_ATTR(HLF_W));
3170 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3171#ifdef FEAT_EVAL
3172 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3173#endif
3174 }
3175
3176 // set 'delcombine'
3177 p_deco = TRUE;
3178
3179# ifdef FEAT_KEYMAP
3180 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003181 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3182 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003183# endif
3184 }
3185 else
3186 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003187 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003188 if (!p_tbidi)
3189 {
3190 // reset rightleft mode
3191 if (curwin->w_p_rl)
3192 {
3193 curwin->w_p_rl = FALSE;
3194 changed_window_setting();
3195 }
3196
3197 // 'arabicshape' isn't reset, it is a global option and
3198 // another window may still need it "on".
3199 }
3200
3201 // 'delcombine' isn't reset, it is a global option and another
3202 // window may still want it "on".
3203
3204# ifdef FEAT_KEYMAP
3205 // Revert to the default keymap
3206 curbuf->b_p_iminsert = B_IMODE_NONE;
3207 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3208# endif
3209 }
3210
3211 return errmsg;
3212}
3213#endif
3214
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003215#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3216/*
3217 * Process the updated 'autochdir' option value.
3218 */
3219 char *
3220did_set_autochdir(optset_T *args UNUSED)
3221{
3222 // Change directories when the 'acd' option is set now.
3223 DO_AUTOCHDIR;
3224 return NULL;
3225}
3226#endif
3227
3228#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3229/*
3230 * Process the updated 'ballooneval' option value.
3231 */
3232 char *
3233did_set_ballooneval(optset_T *args)
3234{
3235 if (balloonEvalForTerm)
3236 return NULL;
3237
3238 if (p_beval && !args->os_oldval.boolean)
3239 gui_mch_enable_beval_area(balloonEval);
3240 else if (!p_beval && args->os_oldval.boolean)
3241 gui_mch_disable_beval_area(balloonEval);
3242
3243 return NULL;
3244}
3245#endif
3246
3247#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3248/*
3249 * Process the updated 'balloonevalterm' option value.
3250 */
3251 char *
3252did_set_balloonevalterm(optset_T *args UNUSED)
3253{
3254 mch_bevalterm_changed();
3255 return NULL;
3256}
3257#endif
3258
3259/*
3260 * Process the updated 'binary' option value.
3261 */
3262 char *
3263did_set_binary(optset_T *args)
3264{
3265 // when 'bin' is set also set some other options
3266 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3267 redraw_titles();
3268
3269 return NULL;
3270}
3271
3272#if defined(FEAT_LINEBREAK) || defined(PROTO)
3273/*
3274 * Called when the 'breakat' option changes value.
3275 */
3276 char *
3277did_set_breakat(optset_T *args UNUSED)
3278{
3279 char_u *p;
3280 int i;
3281
3282 for (i = 0; i < 256; i++)
3283 breakat_flags[i] = FALSE;
3284
3285 if (p_breakat != NULL)
3286 for (p = p_breakat; *p; p++)
3287 breakat_flags[*p] = TRUE;
3288
3289 return NULL;
3290}
3291#endif
3292
3293/*
3294 * Process the updated 'buflisted' option value.
3295 */
3296 char *
3297did_set_buflisted(optset_T *args)
3298{
3299 // when 'buflisted' changes, trigger autocommands
3300 if (args->os_oldval.boolean != curbuf->b_p_bl)
3301 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3302 NULL, NULL, TRUE, curbuf);
3303 return NULL;
3304}
3305
3306/*
3307 * Process the new 'cmdheight' option value.
3308 */
3309 char *
3310did_set_cmdheight(optset_T *args)
3311{
3312 long old_value = args->os_oldval.number;
3313 char *errmsg = NULL;
3314
3315 // if p_ch changed value, change the command line height
3316 if (p_ch < 1)
3317 {
3318 errmsg = e_argument_must_be_positive;
3319 p_ch = 1;
3320 }
3321 if (p_ch > Rows - min_rows() + 1)
3322 p_ch = Rows - min_rows() + 1;
3323
3324 // Only compute the new window layout when startup has been
3325 // completed. Otherwise the frame sizes may be wrong.
3326 if ((p_ch != old_value
3327 || tabline_height() + topframe->fr_height != Rows - p_ch)
3328 && full_screen
3329#ifdef FEAT_GUI
3330 && !gui.starting
3331#endif
3332 )
3333 command_height();
3334
3335 return errmsg;
3336}
3337
3338/*
3339 * Process the updated 'compatible' option value.
3340 */
3341 char *
3342did_set_compatible(optset_T *args UNUSED)
3343{
3344 compatible_set();
3345 return NULL;
3346}
3347
3348#if defined(FEAT_CONCEAL) || defined(PROTO)
3349/*
3350 * Process the new 'conceallevel' option value.
3351 */
3352 char *
3353did_set_conceallevel(optset_T *args UNUSED)
3354{
3355 char *errmsg = NULL;
3356
3357 if (curwin->w_p_cole < 0)
3358 {
3359 errmsg = e_argument_must_be_positive;
3360 curwin->w_p_cole = 0;
3361 }
3362 else if (curwin->w_p_cole > 3)
3363 {
3364 errmsg = e_invalid_argument;
3365 curwin->w_p_cole = 3;
3366 }
3367
3368 return errmsg;
3369}
3370#endif
3371
3372#if defined(FEAT_DIFF) || defined(PROTO)
3373/*
3374 * Process the updated 'diff' option value.
3375 */
3376 char *
3377did_set_diff(optset_T *args UNUSED)
3378{
3379 // May add or remove the buffer from the list of diff buffers.
3380 diff_buf_adjust(curwin);
3381# ifdef FEAT_FOLDING
3382 if (foldmethodIsDiff(curwin))
3383 foldUpdateAll(curwin);
3384# endif
3385 return NULL;
3386}
3387#endif
3388
3389/*
3390 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3391 * option value.
3392 */
3393 char *
3394did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3395{
3396 // redraw the window title and tab page text
3397 redraw_titles();
3398 return NULL;
3399}
3400
3401/*
3402 * Process the updated 'equalalways' option value.
3403 */
3404 char *
3405did_set_equalalways(optset_T *args)
3406{
3407 if (p_ea && !args->os_oldval.boolean)
3408 win_equal(curwin, FALSE, 0);
3409
3410 return NULL;
3411}
3412
3413#if defined(FEAT_FOLDING) || defined(PROTO)
3414/*
3415 * Process the new 'foldcolumn' option value.
3416 */
3417 char *
3418did_set_foldcolumn(optset_T *args UNUSED)
3419{
3420 char *errmsg = NULL;
3421
3422 if (curwin->w_p_fdc < 0)
3423 {
3424 errmsg = e_argument_must_be_positive;
3425 curwin->w_p_fdc = 0;
3426 }
3427 else if (curwin->w_p_fdc > 12)
3428 {
3429 errmsg = e_invalid_argument;
3430 curwin->w_p_fdc = 12;
3431 }
3432
3433 return errmsg;
3434}
3435
3436/*
3437 * Process the new 'foldlevel' option value.
3438 */
3439 char *
3440did_set_foldlevel(optset_T *args UNUSED)
3441{
3442 if (curwin->w_p_fdl < 0)
3443 curwin->w_p_fdl = 0;
3444 newFoldLevel();
3445 return NULL;
3446}
3447
3448/*
3449 * Process the new 'foldminlines' option value.
3450 */
3451 char *
3452did_set_foldminlines(optset_T *args UNUSED)
3453{
3454 foldUpdateAll(curwin);
3455 return NULL;
3456}
3457
3458/*
3459 * Process the new 'foldnestmax' option value.
3460 */
3461 char *
3462did_set_foldnestmax(optset_T *args UNUSED)
3463{
3464 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3465 foldUpdateAll(curwin);
3466 return NULL;
3467}
3468#endif
3469
3470#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3471/*
3472 * Process the updated 'hlsearch' option value.
3473 */
3474 char *
3475did_set_hlsearch(optset_T *args UNUSED)
3476{
3477 // when 'hlsearch' is set or reset: reset no_hlsearch
3478 set_no_hlsearch(FALSE);
3479 return NULL;
3480}
3481#endif
3482
3483/*
3484 * Process the updated 'ignorecase' option value.
3485 */
3486 char *
3487did_set_ignorecase(optset_T *args UNUSED)
3488{
3489 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3490 if (p_hls)
3491 redraw_all_later(UPD_SOME_VALID);
3492 return NULL;
3493}
3494
3495#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3496/*
3497 * Process the updated 'imdisable' option value.
3498 */
3499 char *
3500did_set_imdisable(optset_T *args UNUSED)
3501{
3502 // Only de-activate it here, it will be enabled when changing mode.
3503 if (p_imdisable)
3504 im_set_active(FALSE);
3505 else if (State & MODE_INSERT)
3506 // When the option is set from an autocommand, it may need to take
3507 // effect right away.
3508 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3509 return NULL;
3510}
3511#endif
3512
3513/*
3514 * Process the new 'iminsert' option value.
3515 */
3516 char *
3517did_set_iminsert(optset_T *args UNUSED)
3518{
3519 char *errmsg = NULL;
3520
3521 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3522 {
3523 errmsg = e_invalid_argument;
3524 curbuf->b_p_iminsert = B_IMODE_NONE;
3525 }
3526 p_iminsert = curbuf->b_p_iminsert;
3527 if (termcap_active) // don't do this in the alternate screen
3528 showmode();
3529#if defined(FEAT_KEYMAP)
3530 // Show/unshow value of 'keymap' in status lines.
3531 status_redraw_curbuf();
3532#endif
3533
3534 return errmsg;
3535}
3536
3537/*
3538 * Process the new 'imsearch' option value.
3539 */
3540 char *
3541did_set_imsearch(optset_T *args UNUSED)
3542{
3543 char *errmsg = NULL;
3544
3545 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3546 {
3547 errmsg = e_invalid_argument;
3548 curbuf->b_p_imsearch = B_IMODE_NONE;
3549 }
3550 p_imsearch = curbuf->b_p_imsearch;
3551
3552 return errmsg;
3553}
3554
3555#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3556/*
3557 * Process the new 'imstyle' option value.
3558 */
3559 char *
3560did_set_imstyle(optset_T *args UNUSED)
3561{
3562 char *errmsg = NULL;
3563
3564 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3565 errmsg = e_invalid_argument;
3566
3567 return errmsg;
3568}
3569#endif
3570
3571/*
3572 * Process the updated 'insertmode' option value.
3573 */
3574 char *
3575did_set_insertmode(optset_T *args)
3576{
3577 // when 'insertmode' is set from an autocommand need to do work here
3578 if (p_im)
3579 {
3580 if ((State & MODE_INSERT) == 0)
3581 need_start_insertmode = TRUE;
3582 stop_insert_mode = FALSE;
3583 }
3584 // only reset if it was set previously
3585 else if (args->os_oldval.boolean)
3586 {
3587 need_start_insertmode = FALSE;
3588 stop_insert_mode = TRUE;
3589 if (restart_edit != 0 && mode_displayed)
3590 clear_cmdline = TRUE; // remove "(insert)"
3591 restart_edit = 0;
3592 }
3593
3594 return NULL;
3595}
3596
3597#if defined(FEAT_LANGMAP) || defined(PROTO)
3598/*
3599 * Process the updated 'langnoremap' option value.
3600 */
3601 char *
3602did_set_langnoremap(optset_T *args UNUSED)
3603{
3604 // 'langnoremap' -> !'langremap'
3605 p_lrm = !p_lnr;
3606 return NULL;
3607}
3608
3609/*
3610 * Process the updated 'langremap' option value.
3611 */
3612 char *
3613did_set_langremap(optset_T *args UNUSED)
3614{
3615 // 'langremap' -> !'langnoremap'
3616 p_lnr = !p_lrm;
3617 return NULL;
3618}
3619#endif
3620
3621/*
3622 * Process the new 'laststatus' option value.
3623 */
3624 char *
3625did_set_laststatus(optset_T *args UNUSED)
3626{
3627 last_status(FALSE); // (re)set last window status line
3628 return NULL;
3629}
3630
3631#if defined(FEAT_GUI) || defined(PROTO)
3632/*
3633 * Process the new 'linespace' option value.
3634 */
3635 char *
3636did_set_linespace(optset_T *args UNUSED)
3637{
3638 // Recompute gui.char_height and resize the Vim window to keep the
3639 // same number of lines.
3640 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3641 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3642 return NULL;
3643}
3644#endif
3645
3646/*
3647 * Process the updated 'lisp' option value.
3648 */
3649 char *
3650did_set_lisp(optset_T *args UNUSED)
3651{
3652 // When 'lisp' option changes include/exclude '-' in keyword characters.
3653 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3654 return NULL;
3655}
3656
3657/*
3658 * Process the new 'maxcombine' option value.
3659 */
3660 char *
3661did_set_maxcombine(optset_T *args UNUSED)
3662{
3663 if (p_mco > MAX_MCO)
3664 p_mco = MAX_MCO;
3665 else if (p_mco < 0)
3666 p_mco = 0;
3667 screenclear(); // will re-allocate the screen
3668 return NULL;
3669}
3670
3671/*
3672 * Process the updated 'modifiable' option value.
3673 */
3674 char *
3675did_set_modifiable(optset_T *args UNUSED)
3676{
3677 // when 'modifiable' is changed, redraw the window title
3678
3679# ifdef FEAT_TERMINAL
3680 // Cannot set 'modifiable' when in Terminal mode.
3681 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3682 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3683 {
3684 curbuf->b_p_ma = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003685 return e_cannot_make_terminal_with_running_job_modifiable;
3686 }
3687# endif
3688 redraw_titles();
3689
3690 return NULL;
3691}
3692
3693/*
3694 * Process the updated 'modified' option value.
3695 */
3696 char *
3697did_set_modified(optset_T *args)
3698{
3699 if (!args->os_newval.boolean)
3700 save_file_ff(curbuf); // Buffer is unchanged
3701 redraw_titles();
3702 modified_was_set = args->os_newval.boolean;
3703 return NULL;
3704}
3705
3706#if defined(FEAT_GUI) || defined(PROTO)
3707/*
3708 * Process the updated 'mousehide' option value.
3709 */
3710 char *
3711did_set_mousehide(optset_T *args UNUSED)
3712{
3713 if (!p_mh)
3714 gui_mch_mousehide(FALSE);
3715 return NULL;
3716}
3717#endif
3718
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003719/*
3720 * Process the updated 'number' or 'relativenumber' option value.
3721 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003722 char *
3723did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003724{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003725#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003726 if (gui.in_use
3727 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3728 && curbuf->b_signlist != NULL)
3729 {
3730 // If the 'number' or 'relativenumber' options are modified and
3731 // 'signcolumn' is set to 'number', then clear the screen for a full
3732 // refresh. Otherwise the sign icons are not displayed properly in the
3733 // number column. If the 'number' option is set and only the
3734 // 'relativenumber' option is toggled, then don't refresh the screen
3735 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003736 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003737 redraw_all_later(UPD_CLEAR);
3738 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003739#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003740 return NULL;
3741}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003742
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003743#if defined(FEAT_LINEBREAK) || defined(PROTO)
3744/*
3745 * Process the new 'numberwidth' option value.
3746 */
3747 char *
3748did_set_numberwidth(optset_T *args UNUSED)
3749{
3750 char *errmsg = NULL;
3751
3752 // 'numberwidth' must be positive
3753 if (curwin->w_p_nuw < 1)
3754 {
3755 errmsg = e_argument_must_be_positive;
3756 curwin->w_p_nuw = 1;
3757 }
3758 if (curwin->w_p_nuw > 20)
3759 {
3760 errmsg = e_invalid_argument;
3761 curwin->w_p_nuw = 20;
3762 }
3763 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3764
3765 return errmsg;
3766}
3767#endif
3768
3769/*
3770 * Process the updated 'paste' option value. Called after p_paste was set or
3771 * reset. When 'paste' is set or reset also change other options.
3772 */
3773 char *
3774did_set_paste(optset_T *args UNUSED)
3775{
3776 static int old_p_paste = FALSE;
3777 static int save_sm = 0;
3778 static int save_sta = 0;
3779 static int save_ru = 0;
3780#ifdef FEAT_RIGHTLEFT
3781 static int save_ri = 0;
3782 static int save_hkmap = 0;
3783#endif
3784 buf_T *buf;
3785
3786 if (p_paste)
3787 {
3788 // Paste switched from off to on.
3789 // Save the current values, so they can be restored later.
3790 if (!old_p_paste)
3791 {
3792 // save options for each buffer
3793 FOR_ALL_BUFFERS(buf)
3794 {
3795 buf->b_p_tw_nopaste = buf->b_p_tw;
3796 buf->b_p_wm_nopaste = buf->b_p_wm;
3797 buf->b_p_sts_nopaste = buf->b_p_sts;
3798 buf->b_p_ai_nopaste = buf->b_p_ai;
3799 buf->b_p_et_nopaste = buf->b_p_et;
3800#ifdef FEAT_VARTABS
3801 if (buf->b_p_vsts_nopaste)
3802 vim_free(buf->b_p_vsts_nopaste);
3803 buf->b_p_vsts_nopaste =
3804 buf->b_p_vsts && buf->b_p_vsts != empty_option
3805 ? vim_strsave(buf->b_p_vsts) : NULL;
3806#endif
3807 }
3808
3809 // save global options
3810 save_sm = p_sm;
3811 save_sta = p_sta;
3812 save_ru = p_ru;
3813#ifdef FEAT_RIGHTLEFT
3814 save_ri = p_ri;
3815 save_hkmap = p_hkmap;
3816#endif
3817 // save global values for local buffer options
3818 p_ai_nopaste = p_ai;
3819 p_et_nopaste = p_et;
3820 p_sts_nopaste = p_sts;
3821 p_tw_nopaste = p_tw;
3822 p_wm_nopaste = p_wm;
3823#ifdef FEAT_VARTABS
3824 if (p_vsts_nopaste)
3825 vim_free(p_vsts_nopaste);
3826 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3827 ? vim_strsave(p_vsts) : NULL;
3828#endif
3829 }
3830
3831 // Always set the option values, also when 'paste' is set when it is
3832 // already on. Set options for each buffer.
3833 FOR_ALL_BUFFERS(buf)
3834 {
3835 buf->b_p_tw = 0; // textwidth is 0
3836 buf->b_p_wm = 0; // wrapmargin is 0
3837 buf->b_p_sts = 0; // softtabstop is 0
3838 buf->b_p_ai = 0; // no auto-indent
3839 buf->b_p_et = 0; // no expandtab
3840#ifdef FEAT_VARTABS
3841 if (buf->b_p_vsts)
3842 free_string_option(buf->b_p_vsts);
3843 buf->b_p_vsts = empty_option;
3844 VIM_CLEAR(buf->b_p_vsts_array);
3845#endif
3846 }
3847
3848 // set global options
3849 p_sm = 0; // no showmatch
3850 p_sta = 0; // no smarttab
3851 if (p_ru)
3852 status_redraw_all(); // redraw to remove the ruler
3853 p_ru = 0; // no ruler
3854#ifdef FEAT_RIGHTLEFT
3855 p_ri = 0; // no reverse insert
3856 p_hkmap = 0; // no Hebrew keyboard
3857#endif
3858 // set global values for local buffer options
3859 p_tw = 0;
3860 p_wm = 0;
3861 p_sts = 0;
3862 p_ai = 0;
Christian Brabandt757593c2023-08-22 21:44:10 +02003863 p_et = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003864#ifdef FEAT_VARTABS
3865 if (p_vsts)
3866 free_string_option(p_vsts);
3867 p_vsts = empty_option;
3868#endif
3869 }
3870
3871 // Paste switched from on to off: Restore saved values.
3872 else if (old_p_paste)
3873 {
3874 // restore options for each buffer
3875 FOR_ALL_BUFFERS(buf)
3876 {
3877 buf->b_p_tw = buf->b_p_tw_nopaste;
3878 buf->b_p_wm = buf->b_p_wm_nopaste;
3879 buf->b_p_sts = buf->b_p_sts_nopaste;
3880 buf->b_p_ai = buf->b_p_ai_nopaste;
3881 buf->b_p_et = buf->b_p_et_nopaste;
3882#ifdef FEAT_VARTABS
3883 if (buf->b_p_vsts)
3884 free_string_option(buf->b_p_vsts);
3885 buf->b_p_vsts = buf->b_p_vsts_nopaste
3886 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3887 vim_free(buf->b_p_vsts_array);
3888 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3889 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3890 else
3891 buf->b_p_vsts_array = NULL;
3892#endif
3893 }
3894
3895 // restore global options
3896 p_sm = save_sm;
3897 p_sta = save_sta;
3898 if (p_ru != save_ru)
3899 status_redraw_all(); // redraw to draw the ruler
3900 p_ru = save_ru;
3901#ifdef FEAT_RIGHTLEFT
3902 p_ri = save_ri;
3903 p_hkmap = save_hkmap;
3904#endif
3905 // set global values for local buffer options
3906 p_ai = p_ai_nopaste;
3907 p_et = p_et_nopaste;
3908 p_sts = p_sts_nopaste;
3909 p_tw = p_tw_nopaste;
3910 p_wm = p_wm_nopaste;
3911#ifdef FEAT_VARTABS
3912 if (p_vsts)
3913 free_string_option(p_vsts);
3914 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
3915#endif
3916 }
3917
3918 old_p_paste = p_paste;
3919
Christian Brabandt757593c2023-08-22 21:44:10 +02003920#if defined(FEAT_EVAL) || defined(PROTO)
3921 // Remember where the dependent options were reset
3922 didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
3923#endif
3924
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003925 return NULL;
3926}
3927
3928
3929#ifdef FEAT_QUICKFIX
3930/*
3931 * Process the updated 'previewwindow' option value.
3932 */
3933 char *
Yegappan Lakshmanan3ee25962023-12-04 20:31:14 +01003934did_set_previewwindow(optset_T *args UNUSED)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003935{
3936 if (!curwin->w_p_pvw)
3937 return NULL;
3938
3939 // There can be only one window with 'previewwindow' set.
3940 win_T *win;
3941
3942 FOR_ALL_WINDOWS(win)
3943 if (win->w_p_pvw && win != curwin)
3944 {
3945 curwin->w_p_pvw = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003946 return e_preview_window_already_exists;
3947 }
3948
3949 return NULL;
3950}
3951#endif
3952
3953#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
3954/*
3955 * Process the new 'pyxversion' option value.
3956 */
3957 char *
3958did_set_pyxversion(optset_T *args UNUSED)
3959{
3960 char *errmsg = NULL;
3961
3962 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3963 errmsg = e_invalid_argument;
3964
3965 return errmsg;
3966}
3967#endif
3968
3969/*
3970 * Process the updated 'readonly' option value.
3971 */
3972 char *
3973did_set_readonly(optset_T *args)
3974{
3975 // when 'readonly' is reset globally, also reset readonlymode
3976 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
3977 readonlymode = FALSE;
3978
3979 // when 'readonly' is set may give W10 again
3980 if (curbuf->b_p_ro)
3981 curbuf->b_did_warn = FALSE;
3982
3983 redraw_titles();
3984
3985 return NULL;
3986}
3987
3988/*
3989 * Process the updated 'scrollbind' option value.
3990 */
3991 char *
3992did_set_scrollbind(optset_T *args UNUSED)
3993{
3994 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3995 // at the end of normal_cmd()
3996 if (!curwin->w_p_scb)
3997 return NULL;
3998
3999 do_check_scrollbind(FALSE);
4000 curwin->w_scbind_pos = curwin->w_topline;
4001 return NULL;
4002}
4003
4004#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
4005/*
4006 * Process the updated 'shellslash' option value.
4007 */
4008 char *
4009did_set_shellslash(optset_T *args UNUSED)
4010{
4011 if (p_ssl)
4012 {
4013 psepc = '/';
4014 psepcN = '\\';
4015 pseps[0] = '/';
4016 }
4017 else
4018 {
4019 psepc = '\\';
4020 psepcN = '/';
4021 pseps[0] = '\\';
4022 }
4023
4024 // need to adjust the file name arguments and buffer names.
4025 buflist_slash_adjust();
4026 alist_slash_adjust();
4027# ifdef FEAT_EVAL
4028 scriptnames_slash_adjust();
4029# endif
4030 return NULL;
4031}
4032#endif
4033
4034/*
4035 * Process the new 'shiftwidth' or the 'tabstop' option value.
4036 */
4037 char *
4038did_set_shiftwidth_tabstop(optset_T *args)
4039{
4040 long *pp = (long *)args->os_varp;
4041 char *errmsg = NULL;
4042
4043 if (curbuf->b_p_sw < 0)
4044 {
4045 errmsg = e_argument_must_be_positive;
4046#ifdef FEAT_VARTABS
4047 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4048 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4049 ? tabstop_first(curbuf->b_p_vts_array)
4050 : curbuf->b_p_ts;
4051#else
4052 curbuf->b_p_sw = curbuf->b_p_ts;
4053#endif
4054 }
4055
4056#ifdef FEAT_FOLDING
4057 if (foldmethodIsIndent(curwin))
4058 foldUpdateAll(curwin);
4059#endif
4060 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4061 // parse 'cinoptions'.
4062 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4063 parse_cino(curbuf);
4064
4065 return errmsg;
4066}
4067
4068/*
4069 * Process the new 'showtabline' option value.
4070 */
4071 char *
4072did_set_showtabline(optset_T *args UNUSED)
4073{
4074 shell_new_rows(); // recompute window positions and heights
4075 return NULL;
4076}
4077
4078/*
4079 * Process the updated 'smoothscroll' option value.
4080 */
4081 char *
4082did_set_smoothscroll(optset_T *args UNUSED)
4083{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004084 if (!curwin->w_p_sms)
4085 curwin->w_skipcol = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004086
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004087 return NULL;
4088}
4089
4090#if defined(FEAT_SPELL) || defined(PROTO)
4091/*
4092 * Process the updated 'spell' option value.
4093 */
4094 char *
4095did_set_spell(optset_T *args UNUSED)
4096{
4097 if (curwin->w_p_spell)
4098 return parse_spelllang(curwin);
4099
4100 return NULL;
4101}
4102#endif
4103
4104/*
4105 * Process the updated 'swapfile' option value.
4106 */
4107 char *
4108did_set_swapfile(optset_T *args UNUSED)
4109{
4110 // when 'swf' is set, create swapfile, when reset remove swapfile
4111 if (curbuf->b_p_swf && p_uc)
4112 ml_open_file(curbuf); // create the swap file
4113 else
4114 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4115 // buf->b_p_swf
4116 mf_close_file(curbuf, TRUE); // remove the swap file
4117 return NULL;
4118}
4119
4120#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004121 char *
4122did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004123{
4124# ifdef FEAT_VTP
4125 // Do not turn on 'tgc' when 24-bit colors are not supported.
4126 if (
4127# ifdef VIMDLL
4128 !gui.in_use && !gui.starting &&
4129# endif
4130 !has_vtp_working())
4131 {
4132 p_tgc = 0;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004133 return e_24_bit_colors_are_not_supported_on_this_environment;
4134 }
4135 if (is_term_win32())
4136 swap_tcap();
4137# endif
4138# ifdef FEAT_GUI
4139 if (!gui.in_use && !gui.starting)
4140# endif
4141 highlight_gui_started();
4142# ifdef FEAT_VTP
4143 // reset t_Co
4144 if (is_term_win32())
4145 {
4146 control_console_color_rgb();
4147 set_termname(T_NAME);
4148 init_highlight(TRUE, FALSE);
4149 }
4150# endif
4151# ifdef FEAT_TERMINAL
4152 term_update_colors_all();
4153 term_update_palette_all();
4154 term_update_wincolor_all();
4155# endif
4156
4157 return NULL;
4158}
4159#endif
4160
4161/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004162 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004164 char *
4165did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004167 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004169 // when 'terse' is set change 'shortmess'
4170 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004172 // insert 's' in p_shm
4173 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004174 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004175 STRCPY(IObuff, p_shm);
4176 STRCAT(IObuff, "s");
4177 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004178 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004179 // remove 's' from p_shm
4180 else if (!p_terse && p != NULL)
4181 STRMOVE(p, p + 1);
4182 return NULL;
4183}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004185/*
4186 * Process the updated 'textauto' option value.
4187 */
4188 char *
4189did_set_textauto(optset_T *args)
4190{
4191 // when 'textauto' is set or reset also change 'fileformats'
4192 set_string_option_direct((char_u *)"ffs", -1,
4193 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4194 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004195
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004196 return NULL;
4197}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004199/*
4200 * Process the updated 'textmode' option value.
4201 */
4202 char *
4203did_set_textmode(optset_T *args)
4204{
4205 // when 'textmode' is set or reset also change 'fileformat'
4206 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4207
4208 return NULL;
4209}
4210
4211/*
4212 * Process the new 'textwidth' option value.
4213 */
4214 char *
4215did_set_textwidth(optset_T *args UNUSED)
4216{
4217 char *errmsg = NULL;
4218
4219 if (curbuf->b_p_tw < 0)
4220 {
4221 errmsg = e_argument_must_be_positive;
4222 curbuf->b_p_tw = 0;
4223 }
4224#ifdef FEAT_SYN_HL
4225 {
4226 win_T *wp;
4227 tabpage_T *tp;
4228
4229 FOR_ALL_TAB_WINDOWS(tp, wp)
4230 check_colorcolumn(wp);
4231 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004232#endif
4233
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004234 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235}
4236
4237/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004238 * Process the updated 'title' or the 'icon' option value.
4239 */
4240 char *
4241did_set_title_icon(optset_T *args UNUSED)
4242{
4243 // when 'title' changed, may need to change the title; same for 'icon'
4244 did_set_title();
4245 return NULL;
4246}
4247
4248/*
4249 * Process the new 'titlelen' option value.
4250 */
4251 char *
4252did_set_titlelen(optset_T *args)
4253{
4254 long old_value = args->os_oldval.number;
4255 char *errmsg = NULL;
4256
4257 // if 'titlelen' has changed, redraw the title
4258 if (p_titlelen < 0)
4259 {
4260 errmsg = e_argument_must_be_positive;
4261 p_titlelen = 85;
4262 }
4263 if (starting != NO_SCREEN && old_value != p_titlelen)
4264 need_maketitle = TRUE;
4265
4266 return errmsg;
4267}
4268
4269#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4270/*
4271 * Process the updated 'undofile' option value.
4272 */
4273 char *
4274did_set_undofile(optset_T *args)
4275{
4276 // Only take action when the option was set.
4277 if (!curbuf->b_p_udf && !p_udf)
4278 return NULL;
4279
4280 // When reset we do not delete the undo file, the option may be set again
4281 // without making any changes in between.
4282 char_u hash[UNDO_HASH_SIZE];
4283 buf_T *save_curbuf = curbuf;
4284
4285 FOR_ALL_BUFFERS(curbuf)
4286 {
4287 // When 'undofile' is set globally: for every buffer, otherwise
4288 // only for the current buffer: Try to read in the undofile,
4289 // if one exists, the buffer wasn't changed and the buffer was
4290 // loaded
4291 if ((curbuf == save_curbuf
4292 || (args->os_flags & OPT_GLOBAL)
4293 || args->os_flags == 0)
4294 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4295 {
4296#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004297 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004298 continue;
4299#endif
4300 u_compute_hash(hash);
4301 u_read_undo(NULL, hash, curbuf->b_fname);
4302 }
4303 }
4304 curbuf = save_curbuf;
4305
4306 return NULL;
4307}
4308#endif
4309
4310/*
4311 * Process the new global 'undolevels' option value.
4312 */
4313 static void
4314update_global_undolevels(long value, long old_value)
4315{
4316 // sync undo before 'undolevels' changes
4317
4318 // use the old value, otherwise u_sync() may not work properly
4319 p_ul = old_value;
4320 u_sync(TRUE);
4321 p_ul = value;
4322}
4323
4324/*
4325 * Process the new buffer local 'undolevels' option value.
4326 */
4327 static void
4328update_buflocal_undolevels(long value, long old_value)
4329{
4330 // use the old value, otherwise u_sync() may not work properly
4331 curbuf->b_p_ul = old_value;
4332 u_sync(TRUE);
4333 curbuf->b_p_ul = value;
4334}
4335
4336/*
4337 * Process the new 'undolevels' option value.
4338 */
4339 char *
4340did_set_undolevels(optset_T *args)
4341{
4342 long *pp = (long *)args->os_varp;
4343
4344 if (pp == &p_ul) // global 'undolevels'
4345 update_global_undolevels(args->os_newval.number,
4346 args->os_oldval.number);
4347 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4348 update_buflocal_undolevels(args->os_newval.number,
4349 args->os_oldval.number);
4350
4351 return NULL;
4352}
4353
4354/*
4355 * Process the new 'updatecount' option value.
4356 */
4357 char *
4358did_set_updatecount(optset_T *args)
4359{
4360 long old_value = args->os_oldval.number;
4361 char *errmsg = NULL;
4362
4363 // when 'updatecount' changes from zero to non-zero, open swap files
4364 if (p_uc < 0)
4365 {
4366 errmsg = e_argument_must_be_positive;
4367 p_uc = 100;
4368 }
4369 if (p_uc && !old_value)
4370 ml_open_files();
4371
4372 return errmsg;
4373}
4374
4375/*
4376 * Process the updated 'weirdinvert' option value.
4377 */
4378 char *
4379did_set_weirdinvert(optset_T *args)
4380{
4381 // When 'weirdinvert' changed, set/reset 't_xs'.
4382 // Then set 'weirdinvert' according to value of 't_xs'.
4383 if (p_wiv && !args->os_oldval.boolean)
4384 T_XS = (char_u *)"y";
4385 else if (!p_wiv && args->os_oldval.boolean)
4386 T_XS = empty_option;
4387 p_wiv = (*T_XS != NUL);
4388
4389 return NULL;
4390}
4391
4392/*
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02004393 * Process the new 'wildchar' / 'wildcharm' option value.
4394 */
4395 char *
4396did_set_wildchar(optset_T *args)
4397{
4398 long c = *(long *)args->os_varp;
4399
4400 // Don't allow key values that wouldn't work as wildchar.
4401 if (c == Ctrl_C || c == '\n' || c == '\r' || c == K_KENTER)
4402 return e_invalid_argument;
4403
4404 return NULL;
4405}
4406
4407/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004408 * Process the new 'window' option value.
4409 */
4410 char *
4411did_set_window(optset_T *args UNUSED)
4412{
4413 if (p_window < 1)
4414 p_window = 1;
4415 else if (p_window >= Rows)
4416 p_window = Rows - 1;
4417 return NULL;
4418}
4419
4420/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004421 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004423 char *
4424did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004426 long *pp = (long *)args->os_varp;
4427 char *errmsg = NULL;
4428
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004429 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004431 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004432 p_wh = 1;
4433 }
4434 if (p_wmh > p_wh)
4435 {
4436 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4437 p_wh = p_wmh;
4438 }
4439 if (p_hh < 0)
4440 {
4441 errmsg = e_argument_must_be_positive;
4442 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004445 // Change window height NOW
4446 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004448 if (pp == &p_wh && curwin->w_height < p_wh)
4449 win_setheight((int)p_wh);
4450 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4451 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004454 return errmsg;
4455}
4456
4457/*
4458 * Process the new 'winminheight' option value.
4459 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004460 char *
4461did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004462{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004463 char *errmsg = NULL;
4464
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004465 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004467 errmsg = e_argument_must_be_positive;
4468 p_wmh = 0;
4469 }
4470 if (p_wmh > p_wh)
4471 {
4472 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4473 p_wmh = p_wh;
4474 }
4475 win_setminheight();
4476
4477 return errmsg;
4478}
4479
4480/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004481 * Process the new 'winminwidth' option value.
4482 */
4483 char *
4484did_set_winminwidth(optset_T *args UNUSED)
4485{
4486 char *errmsg = NULL;
4487
4488 if (p_wmw < 0)
4489 {
4490 errmsg = e_argument_must_be_positive;
4491 p_wmw = 0;
4492 }
4493 if (p_wmw > p_wiw)
4494 {
4495 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4496 p_wmw = p_wiw;
4497 }
4498 win_setminwidth();
4499
4500 return errmsg;
4501}
4502
4503/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004504 * Process the new 'winwidth' option value.
4505 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004506 char *
4507did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004508{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004509 char *errmsg = NULL;
4510
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004511 if (p_wiw < 1)
4512 {
4513 errmsg = e_argument_must_be_positive;
4514 p_wiw = 1;
4515 }
4516 if (p_wmw > p_wiw)
4517 {
4518 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4519 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004521
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004522 // Change window width NOW
4523 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4524 win_setwidth((int)p_wiw);
4525
4526 return errmsg;
4527}
4528
4529/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004530 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004531 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004532 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004533did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004534{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004535 // Set w_leftcol or w_skipcol to zero.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004536 if (curwin->w_p_wrap)
4537 curwin->w_leftcol = 0;
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004538 else
4539 curwin->w_skipcol = 0;
4540
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004541 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004542}
4543
4544/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004545 * Set the value of a boolean option, and take care of side effects.
4546 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004547 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004548 static char *
4549set_bool_option(
4550 int opt_idx, // index in options[] table
4551 char_u *varp, // pointer to the option variable
4552 int value, // new value
4553 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004554{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004555 int old_value = *(int *)varp;
4556#if defined(FEAT_EVAL)
4557 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004559 char *errmsg = NULL;
4560
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004561 // Disallow changing some options from secure mode
4562 if ((secure
4563#ifdef HAVE_SANDBOX
4564 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004565#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004566 ) && (options[opt_idx].flags & P_SECURE))
4567 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004568
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004569#if defined(FEAT_EVAL)
4570 // Save the global value before changing anything. This is needed as for
4571 // a global-only option setting the "local value" in fact sets the global
4572 // value (since there is only one value).
4573 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4574 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4575 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004577
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004578 *(int *)varp = value; // set the new value
4579#ifdef FEAT_EVAL
4580 // Remember where the option was set.
4581 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004582#endif
4583
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004585 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004588 // May set global value for local option.
4589 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4590 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004591
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004592 // Handle side effects of changing a bool option.
4593 if (options[opt_idx].opt_did_set_cb != NULL)
4594 {
4595 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004596
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004597 CLEAR_FIELD(args);
4598 args.os_varp = varp;
4599 args.os_flags = opt_flags;
4600 args.os_oldval.boolean = old_value;
4601 args.os_newval.boolean = value;
4602 args.os_errbuf = NULL;
4603 errmsg = options[opt_idx].opt_did_set_cb(&args);
zeertzjqdeba02d2023-11-02 21:01:19 +01004604 if (errmsg != NULL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004605 return errmsg;
4606 }
4607
4608 // after handling side effects, call autocommand
4609
4610 options[opt_idx].flags |= P_WAS_SET;
4611
4612#if defined(FEAT_EVAL)
4613 apply_optionset_autocmd(opt_idx, opt_flags,
4614 (long)(old_value ? TRUE : FALSE),
4615 (long)(old_global_value ? TRUE : FALSE),
4616 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004617#endif
4618
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004619 comp_col(); // in case 'ruler' or 'showcmd' changed
4620 if (curwin->w_curswant != MAXCOL
4621 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
4622 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004623
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004624 if ((opt_flags & OPT_NO_REDRAW) == 0)
4625 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004626
4627 return errmsg;
4628}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004629
4630/*
4631 * Check the bounds of numeric options.
4632 */
4633 static char *
4634check_num_option_bounds(
4635 long *pp,
4636 long old_value,
4637 long old_Rows,
4638 long old_Columns,
4639 char *errbuf,
4640 size_t errbuflen,
4641 char *errmsg)
4642{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if (Rows < min_rows() && full_screen)
4644 {
4645 if (errbuf != NULL)
4646 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004647 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004648 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 errmsg = errbuf;
4650 }
4651 Rows = min_rows();
4652 }
4653 if (Columns < MIN_COLUMNS && full_screen)
4654 {
4655 if (errbuf != NULL)
4656 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004657 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004658 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 errmsg = errbuf;
4660 }
4661 Columns = MIN_COLUMNS;
4662 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004663 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004665 // If the screen (shell) height has been changed, assume it is the
4666 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if (old_Rows != Rows || old_Columns != Columns)
4668 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004669 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 if (updating_screen)
4671 *pp = old_value;
4672 else if (full_screen
4673#ifdef FEAT_GUI
4674 && !gui.starting
4675#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004676 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 set_shellsize((int)Columns, (int)Rows, TRUE);
4678 else
4679 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004680 // Postpone the resizing; check the size and cmdline position for
4681 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 check_shellsize();
4683 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4684 cmdline_row = Rows - p_ch;
4685 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004686 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004687 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 }
4689
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 if (curbuf->b_p_ts <= 0)
4691 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004692 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 curbuf->b_p_ts = 8;
4694 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004695 else if (curbuf->b_p_ts > TABSTOP_MAX)
4696 {
4697 errmsg = e_invalid_argument;
4698 curbuf->b_p_ts = 8;
4699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 if (p_tm < 0)
4701 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004702 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 p_tm = 0;
4704 }
4705 if ((curwin->w_p_scr <= 0
4706 || (curwin->w_p_scr > curwin->w_height
4707 && curwin->w_height > 0))
4708 && full_screen)
4709 {
4710 if (pp == &(curwin->w_p_scr))
4711 {
4712 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004713 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 win_comp_scroll(curwin);
4715 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004716 // If 'scroll' became invalid because of a side effect silently adjust
4717 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 else if (curwin->w_p_scr <= 0)
4719 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004720 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 curwin->w_p_scr = curwin->w_height;
4722 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004723 if (p_hi < 0)
4724 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004725 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004726 p_hi = 0;
4727 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004728 else if (p_hi > 10000)
4729 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004730 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004731 p_hi = 10000;
4732 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004733 if (p_re < 0 || p_re > 2)
4734 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004735 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004736 p_re = 0;
4737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 if (p_report < 0)
4739 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004740 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 p_report = 1;
4742 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004743 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004745 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 p_sj = Rows / 2;
4747 else
4748 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004749 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 p_sj = 1;
4751 }
4752 }
4753 if (p_so < 0 && full_screen)
4754 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004755 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 p_so = 0;
4757 }
4758 if (p_siso < 0 && full_screen)
4759 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004760 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 p_siso = 0;
4762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 if (p_cwh < 1)
4764 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004765 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 p_cwh = 1;
4767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 if (p_ut < 0)
4769 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004770 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 p_ut = 2000;
4772 }
4773 if (p_ss < 0)
4774 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004775 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 p_ss = 0;
4777 }
4778
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004779 return errmsg;
4780}
4781
4782/*
4783 * Set the value of a number option, and take care of side effects.
4784 * Returns NULL for success, or an error message for an error.
4785 */
4786 static char *
4787set_num_option(
4788 int opt_idx, // index in options[] table
4789 char_u *varp, // pointer to the option variable
4790 long value, // new value
4791 char *errbuf, // buffer for error messages
4792 size_t errbuflen, // length of "errbuf"
4793 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4794 // OPT_MODELINE, etc.
4795{
4796 char *errmsg = NULL;
4797 long old_value = *(long *)varp;
4798#if defined(FEAT_EVAL)
4799 long old_global_value = 0; // only used when setting a local and
4800 // global option
4801#endif
4802 long old_Rows = Rows; // remember old Rows
4803 long old_Columns = Columns; // remember old Columns
4804 long *pp = (long *)varp;
4805
4806 // Disallow changing some options from secure mode.
4807 if ((secure
4808#ifdef HAVE_SANDBOX
4809 || sandbox != 0
4810#endif
4811 ) && (options[opt_idx].flags & P_SECURE))
4812 return e_not_allowed_here;
4813
4814#if defined(FEAT_EVAL)
4815 // Save the global value before changing anything. This is needed as for
4816 // a global-only option setting the "local value" in fact sets the global
4817 // value (since there is only one value).
4818 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4819 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4820 OPT_GLOBAL);
4821#endif
4822
4823 *pp = value;
4824#ifdef FEAT_EVAL
4825 // Remember where the option was set.
4826 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4827#endif
4828#ifdef FEAT_GUI
4829 need_mouse_correct = TRUE;
4830#endif
4831
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004832 // Invoke the option specific callback function to validate and apply the
4833 // new value.
4834 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004835 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004836 optset_T args;
4837
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004838 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004839 args.os_varp = varp;
4840 args.os_flags = opt_flags;
4841 args.os_oldval.number = old_value;
4842 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004843 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004844 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004845 }
4846
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004847 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004848 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4849 errbuf, errbuflen, errmsg);
4850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004851 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4853 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4854
4855 options[opt_idx].flags |= P_WAS_SET;
4856
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004857#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004858 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4859 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004860#endif
4861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004862 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004863 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004864 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004865 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004866 if ((opt_flags & OPT_NO_REDRAW) == 0)
4867 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
4869 return errmsg;
4870}
4871
4872/*
4873 * Called after an option changed: check if something needs to be redrawn.
4874 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004875 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004876check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004878 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004879 int doclear = (flags & P_RCLR) == P_RCLR;
4880 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004882 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
4885 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4886 changed_window_setting();
4887 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004888 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004889 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004890 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004891 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004892 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004894 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895}
4896
4897/*
4898 * Find index for option 'arg'.
4899 * Return -1 if not found.
4900 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004901 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004902findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903{
4904 int opt_idx;
4905 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004906 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 int is_term_opt;
4908
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004909 // For first call: Initialize the quick-access table.
4910 // It contains the index for the first option that starts with a certain
4911 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 if (quick_tab[1] == 0)
4913 {
4914 p = options[0].fullname;
4915 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4916 {
4917 if (s[0] != p[0])
4918 {
4919 if (s[0] == 't' && s[1] == '_')
4920 quick_tab[26] = opt_idx;
4921 else
4922 quick_tab[CharOrdLow(s[0])] = opt_idx;
4923 }
4924 p = s;
4925 }
4926 }
4927
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004928 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 return -1;
4931
4932 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4933 if (is_term_opt)
4934 opt_idx = quick_tab[26];
4935 else
4936 opt_idx = quick_tab[CharOrdLow(arg[0])];
zeertzjqf48558e2023-12-08 21:34:31 +01004937 for (; (s = options[opt_idx].fullname) != NULL && s[0] == arg[0]; opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004939 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 break;
4941 }
zeertzjqf48558e2023-12-08 21:34:31 +01004942 if (s != NULL && s[0] != arg[0])
4943 s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 if (s == NULL && !is_term_opt)
4945 {
4946 opt_idx = quick_tab[CharOrdLow(arg[0])];
4947 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4948 {
4949 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004950 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 break;
4952 s = NULL;
4953 }
4954 }
4955 if (s == NULL)
4956 opt_idx = -1;
4957 return opt_idx;
4958}
4959
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004960#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4961 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962/*
4963 * Get the value for an option.
4964 *
4965 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004966 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004967 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004968 * String option: gov_string, *stringval gets allocated string.
4969 * Hidden Number option: gov_hidden_number.
4970 * Hidden Toggle option: gov_hidden_bool.
4971 * Hidden String option: gov_hidden_string.
4972 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004973 *
4974 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004976 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004977get_option_value(
4978 char_u *name,
4979 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004980 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004981 int *flagsp,
4982 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983{
4984 int opt_idx;
4985 char_u *varp;
4986
4987 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004988 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004989 {
4990 int key;
4991
4992 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004993 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004994 {
4995 char_u key_name[2];
4996 char_u *p;
4997
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004998 if (flagsp != NULL)
4999 *flagsp = 0; // terminal option has no flags
5000
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005001 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01005002 if (key < 0)
5003 {
5004 key_name[0] = KEY2TERMCAP0(key);
5005 key_name[1] = KEY2TERMCAP1(key);
5006 }
5007 else
5008 {
5009 key_name[0] = KS_KEY;
5010 key_name[1] = (key & 0xff);
5011 }
5012 p = find_termcode(key_name);
5013 if (p != NULL)
5014 {
5015 if (stringval != NULL)
5016 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005017 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01005018 }
5019 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005020 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01005021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005023 varp = get_varp_scope(&(options[opt_idx]), scope);
5024
5025 if (flagsp != NULL)
5026 // Return the P_xxxx option flags.
5027 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028
5029 if (options[opt_idx].flags & P_STRING)
5030 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005031 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005032 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (stringval != NULL)
5034 {
zeertzjq51f0bc32022-05-09 13:33:39 +01005035 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005036 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5037 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005039 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005040 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005041 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005044 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 *stringval = vim_strsave(*(char_u **)(varp));
5046 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005047 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005050 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005051 return (options[opt_idx].flags & P_NUM)
5052 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 if (options[opt_idx].flags & P_NUM)
5054 *numval = *(long *)varp;
5055 else
5056 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005057 // Special case: 'modified' is b_changed, but we also want to consider
5058 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 if ((int *)varp == &curbuf->b_changed)
5060 *numval = curbufIsChanged();
5061 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005062 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005064 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065}
5066#endif
5067
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005068#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005069/*
5070 * Returns the option attributes and its value. Unlike the above function it
5071 * will return either global value or local value of the option depending on
5072 * what was requested, but it will never return global value if it was
5073 * requested to return local one and vice versa. Neither it will return
5074 * buffer-local value if it was requested to return window-local one.
5075 *
5076 * Pretends that option is absent if it is not present in the requested scope
5077 * (i.e. has no global, window-local or buffer-local value depending on
5078 * opt_type). Uses
5079 *
5080 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005081 * 0 hidden or unknown option, also option that does not have requested
5082 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005083 * see SOPT_* in vim.h for other flags
5084 *
5085 * Possible opt_type values: see SREQ_* in vim.h
5086 */
5087 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005088get_option_value_strict(
5089 char_u *name,
5090 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005091 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005092 int opt_type,
5093 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005094{
5095 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005096 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005097 struct vimoption *p;
5098 int r = 0;
5099
5100 opt_idx = findoption(name);
5101 if (opt_idx < 0)
5102 return 0;
5103
5104 p = &(options[opt_idx]);
5105
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005106 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005107 if (p->var == NULL)
5108 return 0;
5109
5110 if (p->flags & P_BOOL)
5111 r |= SOPT_BOOL;
5112 else if (p->flags & P_NUM)
5113 r |= SOPT_NUM;
5114 else if (p->flags & P_STRING)
5115 r |= SOPT_STRING;
5116
5117 if (p->indir == PV_NONE)
5118 {
5119 if (opt_type == SREQ_GLOBAL)
5120 r |= SOPT_GLOBAL;
5121 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005122 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005123 }
5124 else
5125 {
5126 if (p->indir & PV_BOTH)
5127 r |= SOPT_GLOBAL;
5128 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005129 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005130
5131 if (p->indir & PV_WIN)
5132 {
5133 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005134 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005135 else
5136 r |= SOPT_WIN;
5137 }
5138 else if (p->indir & PV_BUF)
5139 {
5140 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005141 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005142 else
5143 r |= SOPT_BUF;
5144 }
5145 }
5146
5147 if (stringval == NULL)
5148 return r;
5149
5150 if (opt_type == SREQ_GLOBAL)
5151 varp = p->var;
5152 else
5153 {
5154 if (opt_type == SREQ_BUF)
5155 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005156 // Special case: 'modified' is b_changed, but we also want to
5157 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005158 if (p->indir == PV_MOD)
5159 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005160 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005161 varp = NULL;
5162 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005163# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005164 else if (p->indir == PV_KEY)
5165 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005166 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005167 *stringval = NULL;
5168 varp = NULL;
5169 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005170# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005171 else
5172 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005173 buf_T *save_curbuf = curbuf;
5174
5175 // only getting a pointer, no need to use aucmd_prepbuf()
5176 curbuf = (buf_T *)from;
5177 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005178 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005179 curbuf = save_curbuf;
5180 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005181 }
5182 }
5183 else if (opt_type == SREQ_WIN)
5184 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005185 win_T *save_curwin = curwin;
5186
5187 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005188 curbuf = curwin->w_buffer;
5189 varp = get_varp(p);
5190 curwin = save_curwin;
5191 curbuf = curwin->w_buffer;
5192 }
5193 if (varp == p->var)
5194 return (r | SOPT_UNSET);
5195 }
5196
5197 if (varp != NULL)
5198 {
5199 if (p->flags & P_STRING)
5200 *stringval = vim_strsave(*(char_u **)(varp));
5201 else if (p->flags & P_NUM)
5202 *numval = *(long *) varp;
5203 else
5204 *numval = *(int *)varp;
5205 }
5206
5207 return r;
5208}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005209
5210/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005211 * Iterate over options. First argument is a pointer to a pointer to a
5212 * structure inside options[] array, second is option type like in the above
5213 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005214 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005215 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005216 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005217 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005218 * first argument to NULL.
5219 *
5220 * Returns full option name for current option on each call.
5221 */
5222 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005223option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005224{
5225 struct vimoption *ret = NULL;
5226 do
5227 {
5228 if (*option == NULL)
5229 *option = (void *) options;
5230 else if (((struct vimoption *) (*option))->fullname == NULL)
5231 {
5232 *option = NULL;
5233 return NULL;
5234 }
5235 else
5236 *option = (void *) (((struct vimoption *) (*option)) + 1);
5237
5238 ret = ((struct vimoption *) (*option));
5239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005240 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005241 if (ret->var == NULL)
5242 {
5243 ret = NULL;
5244 continue;
5245 }
5246
5247 switch (opt_type)
5248 {
5249 case SREQ_GLOBAL:
5250 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5251 ret = NULL;
5252 break;
5253 case SREQ_BUF:
5254 if (!(ret->indir & PV_BUF))
5255 ret = NULL;
5256 break;
5257 case SREQ_WIN:
5258 if (!(ret->indir & PV_WIN))
5259 ret = NULL;
5260 break;
5261 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005262 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005263 return NULL;
5264 }
5265 }
5266 while (ret == NULL);
5267
5268 return (char_u *)ret->fullname;
5269}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005270#endif
5271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005273 * Return the flags for the option at 'opt_idx'.
5274 */
5275 long_u
5276get_option_flags(int opt_idx)
5277{
5278 return options[opt_idx].flags;
5279}
5280
5281/*
5282 * Set a flag for the option at 'opt_idx'.
5283 */
5284 void
5285set_option_flag(int opt_idx, long_u flag)
5286{
5287 options[opt_idx].flags |= flag;
5288}
5289
5290/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005291 * Returns TRUE if the option at 'opt_idx' is a global option
5292 */
5293 int
5294is_global_option(int opt_idx)
5295{
5296 return options[opt_idx].indir == PV_NONE;
5297}
5298
5299/*
5300 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5301 * local value.
5302 */
5303 int
5304is_global_local_option(int opt_idx)
5305{
5306 return options[opt_idx].indir & PV_BOTH;
5307}
5308
5309/*
5310 * Returns TRUE if the option at 'opt_idx' is a window-local option
5311 */
5312 int
5313is_window_local_option(int opt_idx)
5314{
5315 return options[opt_idx].var == VAR_WIN;
5316}
5317
5318/*
5319 * Returns TRUE if the option at 'opt_idx' is a hidden option
5320 */
5321 int
5322is_hidden_option(int opt_idx)
5323{
5324 return options[opt_idx].var == NULL;
5325}
5326
5327#if defined(FEAT_CRYPT) || defined(PROTO)
5328/*
5329 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5330 */
5331 int
5332is_crypt_key_option(int opt_idx)
5333{
5334 return options[opt_idx].indir == PV_KEY;
5335}
5336#endif
5337
5338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 * Set the value of option "name".
5340 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005341 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005342 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005344 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005345set_option_value(
5346 char_u *name,
5347 long number,
5348 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005349 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350{
5351 int opt_idx;
5352 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005353 long_u flags;
Christian Brabandtb39b2402023-11-29 11:34:05 +01005354 static char errbuf[ERR_BUFLEN];
5355 int errbuflen = ERR_BUFLEN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
5357 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005358 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005359 {
5360 int key;
5361
5362 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005363 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005364 {
5365 char_u key_name[2];
5366
5367 if (key < 0)
5368 {
5369 key_name[0] = KEY2TERMCAP0(key);
5370 key_name[1] = KEY2TERMCAP1(key);
5371 }
5372 else
5373 {
5374 key_name[0] = KS_KEY;
5375 key_name[1] = (key & 0xff);
5376 }
5377 add_termcode(key_name, string, FALSE);
5378 if (full_screen)
5379 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005380 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005381 return NULL;
5382 }
5383
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005384 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 else
5387 {
5388 flags = options[opt_idx].flags;
5389#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005390 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005392 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005393 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005394 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005397 if (flags & P_STRING)
Christian Brabandtb39b2402023-11-29 11:34:05 +01005398 return set_string_option(opt_idx, string, opt_flags, errbuf, errbuflen);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005399
5400 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5401 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005403 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005405 int idx;
5406
5407 // Either we are given a string or we are setting option
5408 // to zero.
5409 for (idx = 0; string[idx] == '0'; ++idx)
5410 ;
5411 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005412 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005413 // There's another character after zeros or the string
5414 // is empty. In both cases, we are trying to set a
5415 // num option using a string.
5416 semsg(_(e_number_required_after_str_equal_str),
5417 name, string);
5418 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005419
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005422 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005423 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005424 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005425 errbuf, sizeof(errbuf), opt_flags);
5426 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005427 else
5428 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005430
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005432 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433}
5434
5435/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005436 * Call set_option_value() and when an error is returned report it.
5437 */
5438 void
5439set_option_value_give_err(
5440 char_u *name,
5441 long number,
5442 char_u *string,
5443 int opt_flags) // OPT_LOCAL or 0 (both)
5444{
5445 char *errmsg = set_option_value(name, number, string, opt_flags);
5446
5447 if (errmsg != NULL)
5448 emsg(_(errmsg));
5449}
5450
5451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 * Get the terminal code for a terminal option.
5453 * Returns NULL when not found.
5454 */
5455 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005456get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457{
5458 int opt_idx;
5459 char_u *varp;
5460
5461 if (tname[0] != 't' || tname[1] != '_' ||
5462 tname[2] == NUL || tname[3] == NUL)
5463 return NULL;
5464 if ((opt_idx = findoption(tname)) >= 0)
5465 {
5466 varp = get_varp(&(options[opt_idx]));
5467 if (varp != NULL)
5468 varp = *(char_u **)(varp);
5469 return varp;
5470 }
5471 return find_termcode(tname + 2);
5472}
5473
5474 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005475get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
5477 int i;
5478
5479 i = findoption((char_u *)"hl");
5480 if (i >= 0)
5481 return options[i].def_val[VI_DEFAULT];
5482 return (char_u *)NULL;
5483}
5484
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005485 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005486get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005487{
5488 int i;
5489
5490 i = findoption((char_u *)"enc");
5491 if (i >= 0)
5492 return options[i].def_val[VI_DEFAULT];
5493 return (char_u *)NULL;
5494}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005495
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005496#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005497 int
5498is_option_allocated(char *name)
5499{
5500 int idx = findoption((char_u *)name);
5501
5502 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5503}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005504#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005505
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506/*
5507 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005508 * When "has_lt" is true there is a '<' before "*arg_arg".
5509 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 */
5511 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005512find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005514 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005516 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005518 // Don't use get_special_key_code() for t_xx, we don't want it to call
5519 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5521 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005522 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005524 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005526 key = find_special_key(&arg, &modifiers,
5527 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005528 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 key = 0;
5530 }
5531 return key;
5532}
5533
5534/*
5535 * if 'all' == 0: show changed options
5536 * if 'all' == 1: show all normal options
5537 * if 'all' == 2: show all terminal options
5538 */
5539 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005540showoptions(
5541 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005542 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
5544 struct vimoption *p;
5545 int col;
5546 int isterm;
5547 char_u *varp;
5548 struct vimoption **items;
5549 int item_count;
5550 int run;
5551 int row, rows;
5552 int cols;
5553 int i;
5554 int len;
5555
5556#define INC 20
5557#define GAP 3
5558
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005559 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 if (items == NULL)
5561 return;
5562
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005563 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005565 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005567 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005569 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005571 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005573 // Do the loop two times:
5574 // 1. display the short items
5575 // 2. display the long items (only strings and numbers)
5576 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 for (run = 1; run <= 2 && !got_int; ++run)
5578 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005579 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 item_count = 0;
5581 for (p = &options[0]; p->fullname != NULL; p++)
5582 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005583 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005584 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005585 continue;
5586
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 varp = NULL;
5588 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005589 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
5591 if (p->indir != PV_NONE && !isterm)
5592 varp = get_varp_scope(p, opt_flags);
5593 }
5594 else
5595 varp = get_varp(p);
5596 if (varp != NULL
5597 && ((all == 2 && isterm)
5598 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005599 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005601 if (opt_flags & OPT_ONECOLUMN)
5602 len = Columns;
5603 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005604 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 else
5606 {
5607 option_value2string(p, opt_flags);
5608 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5609 }
5610 if ((len <= INC - GAP && run == 1) ||
5611 (len > INC - GAP && run == 2))
5612 items[item_count++] = p;
5613 }
5614 }
5615
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005616 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 if (run == 1)
5618 {
5619 cols = (Columns + GAP - 3) / INC;
5620 if (cols == 0)
5621 cols = 1;
5622 rows = (item_count + cols - 1) / cols;
5623 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005624 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 rows = item_count;
5626 for (row = 0; row < rows && !got_int; ++row)
5627 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005628 msg_putchar('\n'); // go to next line
5629 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 break;
5631 col = 0;
5632 for (i = row; i < item_count; i += rows)
5633 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005634 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 showoneopt(items[i], opt_flags);
5636 col += INC;
5637 }
5638 out_flush();
5639 ui_breakcheck();
5640 }
5641 }
5642 vim_free(items);
5643}
5644
5645/*
5646 * Return TRUE if option "p" has its default value.
5647 */
5648 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005649optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650{
5651 int dvi;
5652
5653 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005654 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005655 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005657 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005659 // the cast to long is required for Manx C, long_i is
5660 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005661 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005662 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5664}
5665
5666/*
5667 * showoneopt: show the value of one option
5668 * must not be called with a hidden option!
5669 */
5670 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005671showoneopt(
5672 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005673 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005675 char_u *varp;
5676 int save_silent = silent_mode;
5677
5678 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005679 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
5681 varp = get_varp_scope(p, opt_flags);
5682
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005683 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5685 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005686 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005688 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005690 msg_puts(" ");
5691 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 if (!(p->flags & P_BOOL))
5693 {
5694 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005695 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 option_value2string(p, opt_flags);
5697 msg_outtrans(NameBuff);
5698 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005699
5700 silent_mode = save_silent;
5701 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702}
5703
5704/*
5705 * Write modified options as ":set" commands to a file.
5706 *
5707 * There are three values for "opt_flags":
5708 * OPT_GLOBAL: Write global option values and fresh values of
5709 * buffer-local options (used for start of a session
5710 * file).
5711 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5712 * curwin (used for a vimrc file).
5713 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5714 * and local values for window-local options of
5715 * curwin. Local values are also written when at the
5716 * default value, because a modeline or autocommand
5717 * may have set them when doing ":edit file" and the
5718 * user has set them back at the default or fresh
5719 * value.
5720 * When "local_only" is TRUE, don't write fresh
5721 * values, only local values (for ":mkview").
5722 * (fresh value = value used for a new buffer or window for a local option).
5723 *
5724 * Return FAIL on error, OK otherwise.
5725 */
5726 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005727makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
5729 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005730 char_u *varp; // currently used value
5731 char_u *varp_fresh; // local value
5732 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 char *cmd;
5734 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005735 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
5737 /*
5738 * The options that don't have a default (terminal name, columns, lines)
5739 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005740 * Do the loop over "options[]" twice: once for options with the
5741 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005743 for (pri = 1; pri >= 0; --pri)
5744 {
5745 for (p = &options[0]; !istermoption(p); p++)
5746 if (!(p->flags & P_NO_MKRC)
5747 && !istermoption(p)
5748 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005750 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5752 continue;
5753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005754 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5755 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5757 continue;
5758
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005759 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005761 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 continue;
5763
Bram Moolenaard23b7142021-04-17 21:04:34 +02005764 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5765 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005766 continue;
5767
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 round = 2;
5769 if (p->indir != PV_NONE)
5770 {
5771 if (p->var == VAR_WIN)
5772 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005773 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 if (!(opt_flags & OPT_LOCAL))
5775 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005776 // When fresh value of window-local option is not at the
5777 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5779 {
5780 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005781 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 {
5783 round = 1;
5784 varp_local = varp;
5785 varp = varp_fresh;
5786 }
5787 }
5788 }
5789 }
5790
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005791 // Round 1: fresh value for window-local options.
5792 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 for ( ; round <= 2; varp = varp_local, ++round)
5794 {
5795 if (round == 1 || (opt_flags & OPT_GLOBAL))
5796 cmd = "set";
5797 else
5798 cmd = "setlocal";
5799
5800 if (p->flags & P_BOOL)
5801 {
5802 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5803 return FAIL;
5804 }
5805 else if (p->flags & P_NUM)
5806 {
5807 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5808 return FAIL;
5809 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005810 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005812 int do_endif = FALSE;
5813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005814 // Don't set 'syntax' and 'filetype' again if the value is
5815 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005816 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005817#if defined(FEAT_SYN_HL)
5818 p->indir == PV_SYN ||
5819#endif
5820 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 {
5822 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5823 *(char_u **)(varp)) < 0
5824 || put_eol(fd) < 0)
5825 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005826 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 }
5828 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005829 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005831 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 {
5833 if (put_line(fd, "endif") == FAIL)
5834 return FAIL;
5835 }
5836 }
5837 }
5838 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 return OK;
5841}
5842
5843#if defined(FEAT_FOLDING) || defined(PROTO)
5844/*
5845 * Generate set commands for the local fold options only. Used when
5846 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5847 */
5848 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005849makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005851 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005853 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 == FAIL
5855# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005856 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005858 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 == FAIL
5860 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5861 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5862 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5863 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5864 )
5865 return FAIL;
5866
5867 return OK;
5868}
5869#endif
5870
5871 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005872put_setstring(
5873 FILE *fd,
5874 char *cmd,
5875 char *name,
5876 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005877 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
5879 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005880 char_u *buf = NULL;
5881 char_u *part = NULL;
5882 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
5884 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5885 return FAIL;
5886 if (*valuep != NULL)
5887 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005888 // Output 'pastetoggle' as key names. For other
5889 // options some characters have to be escaped with
5890 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 if (valuep == &p_pt)
5892 {
5893 s = *valuep;
5894 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005895 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 return FAIL;
5897 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005898 // expand the option value, replace $HOME by ~
5899 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005901 int size = (int)STRLEN(*valuep) + 1;
5902
5903 // replace home directory in the whole option value into "buf"
5904 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005905 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005906 goto fail;
5907 home_replace(NULL, *valuep, buf, size, FALSE);
5908
5909 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005910 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005911 // can be expanded when read back.
5912 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5913 && vim_strchr(*valuep, ',') != NULL)
5914 {
5915 part = alloc(size);
5916 if (part == NULL)
5917 goto fail;
5918
5919 // write line break to clear the option, e.g. ':set rtp='
5920 if (put_eol(fd) == FAIL)
5921 goto fail;
5922
5923 p = buf;
5924 while (*p != NUL)
5925 {
5926 // for each comma separated option part, append value to
5927 // the option, :set rtp+=value
5928 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5929 goto fail;
5930 (void)copy_option_part(&p, part, size, ",");
5931 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5932 goto fail;
5933 }
5934 vim_free(buf);
5935 vim_free(part);
5936 return OK;
5937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005939 {
5940 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005942 }
5943 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 }
5945 else if (put_escstr(fd, *valuep, 2) == FAIL)
5946 return FAIL;
5947 }
5948 if (put_eol(fd) < 0)
5949 return FAIL;
5950 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005951fail:
5952 vim_free(buf);
5953 vim_free(part);
5954 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955}
5956
5957 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005958put_setnum(
5959 FILE *fd,
5960 char *cmd,
5961 char *name,
5962 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963{
5964 long wc;
5965
5966 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5967 return FAIL;
5968 if (wc_use_keyname((char_u *)valuep, &wc))
5969 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005970 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5972 return FAIL;
5973 }
5974 else if (fprintf(fd, "%ld", *valuep) < 0)
5975 return FAIL;
5976 if (put_eol(fd) < 0)
5977 return FAIL;
5978 return OK;
5979}
5980
5981 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005982put_setbool(
5983 FILE *fd,
5984 char *cmd,
5985 char *name,
5986 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005988 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005989 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5991 || put_eol(fd) < 0)
5992 return FAIL;
5993 return OK;
5994}
5995
5996/*
5997 * Clear all the terminal options.
5998 * If the option has been allocated, free the memory.
5999 * Terminal options are never hidden or indirect.
6000 */
6001 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006002clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006004 // Reset a few things before clearing the old options. This may cause
6005 // outputting a few things that the terminal doesn't understand, but the
6006 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006007 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006008 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006010 // When starting the GUI close the display opened for the clipboard.
6011 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 if (gui.starting)
6013 clear_xterm_clip();
6014#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006015 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006017 free_termoptions();
6018}
6019
6020 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006021free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006022{
6023 struct vimoption *p;
6024
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006025 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 if (istermoption(p))
6027 {
6028 if (p->flags & P_ALLOCED)
6029 free_string_option(*(char_u **)(p->var));
6030 if (p->flags & P_DEF_ALLOCED)
6031 free_string_option(p->def_val[VI_DEFAULT]);
6032 *(char_u **)(p->var) = empty_option;
6033 p->def_val[VI_DEFAULT] = empty_option;
6034 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006035#ifdef FEAT_EVAL
6036 // remember where the option was cleared
6037 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 }
6040 clear_termcodes();
6041}
6042
6043/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006044 * Free the string for one term option, if it was allocated.
6045 * Set the string to empty_option and clear allocated flag.
6046 * "var" points to the option value.
6047 */
6048 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006049free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006050{
6051 struct vimoption *p;
6052
6053 for (p = &options[0]; p->fullname != NULL; p++)
6054 if (p->var == var)
6055 {
6056 if (p->flags & P_ALLOCED)
6057 free_string_option(*(char_u **)(p->var));
6058 *(char_u **)(p->var) = empty_option;
6059 p->flags &= ~P_ALLOCED;
6060 break;
6061 }
6062}
6063
6064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 * Set the terminal option defaults to the current value.
6066 * Used after setting the terminal name.
6067 */
6068 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006069set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
6071 struct vimoption *p;
6072
6073 for (p = &options[0]; p->fullname != NULL; p++)
6074 {
6075 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6076 {
6077 if (p->flags & P_DEF_ALLOCED)
6078 {
6079 free_string_option(p->def_val[VI_DEFAULT]);
6080 p->flags &= ~P_DEF_ALLOCED;
6081 }
6082 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6083 if (p->flags & P_ALLOCED)
6084 {
6085 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006086 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 }
6088 }
6089 }
6090}
6091
6092/*
6093 * return TRUE if 'p' starts with 't_'
6094 */
6095 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006096istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097{
6098 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6099}
6100
Bram Moolenaardac13472019-09-16 21:06:21 +02006101/*
6102 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6103 */
6104 int
6105istermoption_idx(int opt_idx)
6106{
6107 return istermoption(&options[opt_idx]);
6108}
6109
Bram Moolenaar113e1072019-01-20 15:30:40 +01006110#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006112 * Unset local option value, similar to ":set opt<".
6113 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006114 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006115unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006116{
6117 struct vimoption *p;
6118 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006119 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006120
6121 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006122 if (opt_idx < 0)
6123 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006124 p = &(options[opt_idx]);
6125
6126 switch ((int)p->indir)
6127 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006128 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006129 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006130 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006131 break;
6132 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006133 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006134 break;
6135 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006136 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006137 break;
6138 case PV_AR:
6139 buf->b_p_ar = -1;
6140 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006141 case PV_BKC:
6142 clear_string_option(&buf->b_p_bkc);
6143 buf->b_bkc_flags = 0;
6144 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006145 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006146 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006147 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006148 case PV_TC:
6149 clear_string_option(&buf->b_p_tc);
6150 buf->b_tc_flags = 0;
6151 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006152 case PV_SISO:
6153 curwin->w_p_siso = -1;
6154 break;
6155 case PV_SO:
6156 curwin->w_p_so = -1;
6157 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006158# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006159 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006160 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006161 break;
6162 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006163 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006164 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006165# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006166 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006167 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006168 break;
6169 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006170 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006171 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006172# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006173 case PV_TSRFU:
6174 clear_string_option(&buf->b_p_tsrfu);
6175 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006176# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006177 case PV_FP:
6178 clear_string_option(&buf->b_p_fp);
6179 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006180# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006181 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006182 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006183 break;
6184 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006185 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006186 break;
6187 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006188 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006189 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006190# endif
6191# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006192 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006193 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006194 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006195# endif
6196# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006197 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006198 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006199 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006200# endif
6201# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006202 case PV_SBR:
6203 clear_string_option(&((win_T *)from)->w_p_sbr);
6204 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006205# endif
6206# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006207 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006208 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006209 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006210# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006211 case PV_UL:
6212 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6213 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006214 case PV_LW:
6215 clear_string_option(&buf->b_p_lw);
6216 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006217 case PV_MENC:
6218 clear_string_option(&buf->b_p_menc);
6219 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006220 case PV_LCS:
6221 clear_string_option(&((win_T *)from)->w_p_lcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006222 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006223 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006224 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006225 case PV_FCS:
6226 clear_string_option(&((win_T *)from)->w_p_fcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006227 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006228 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006229 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006230 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006231 clear_string_option(&((win_T *)from)->w_p_ve);
6232 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006233 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006234 }
6235}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006236#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006237
6238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006240 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 */
6242 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006243get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006245 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 {
6247 if (p->var == VAR_WIN)
6248 return (char_u *)GLOBAL_WO(get_varp(p));
6249 return p->var;
6250 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006251 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {
6253 switch ((int)p->indir)
6254 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006255 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006257 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6258 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6259 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006261 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6262 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6263 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006264 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006265 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006266 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006267 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6268 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006270 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6271 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006273 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6274 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006275#ifdef FEAT_COMPL_FUNC
6276 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6277#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006278#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6279 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6280#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006281#if defined(FEAT_CRYPT)
6282 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6283#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006284#ifdef FEAT_LINEBREAK
6285 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6286#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006287#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006288 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006289#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006290 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006291 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006292 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006293 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006294 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006295 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006296 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006297
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006299 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 }
6301 return get_varp(p);
6302}
6303
6304/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006305 * Get pointer to option variable at 'opt_idx', depending on local or global
6306 * scope.
6307 */
6308 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006309get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006310{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006311 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006312}
6313
6314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 * Get pointer to option variable.
6316 */
6317 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006318get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006320 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 if (p->var == NULL)
6322 return NULL;
6323
6324 switch ((int)p->indir)
6325 {
6326 case PV_NONE: return p->var;
6327
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006328 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006329 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006331 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006333 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006335 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006337 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006339 case PV_TC: return *curbuf->b_p_tc != NUL
6340 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006341 case PV_BKC: return *curbuf->b_p_bkc != NUL
6342 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006343 case PV_SISO: return curwin->w_p_siso >= 0
6344 ? (char_u *)&(curwin->w_p_siso) : p->var;
6345 case PV_SO: return curwin->w_p_so >= 0
6346 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006348 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006350 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6352#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006353 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006355 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006357#ifdef FEAT_COMPL_FUNC
6358 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6359 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6360#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006361 case PV_FP: return *curbuf->b_p_fp != NUL
6362 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006364 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006366 case PV_GP: return *curbuf->b_p_gp != NUL
6367 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6368 case PV_MP: return *curbuf->b_p_mp != NUL
6369 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006371#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6372 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6373 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6374#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006375#if defined(FEAT_CRYPT)
6376 case PV_CM: return *curbuf->b_p_cm != NUL
6377 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6378#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006379#ifdef FEAT_LINEBREAK
6380 case PV_SBR: return *curwin->w_p_sbr != NUL
6381 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6382#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006383#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006384 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006385 ? (char_u *)&(curwin->w_p_stl) : p->var;
6386#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006387 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6388 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006389 case PV_LW: return *curbuf->b_p_lw != NUL
6390 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006391 case PV_MENC: return *curbuf->b_p_menc != NUL
6392 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393#ifdef FEAT_ARABIC
6394 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6395#endif
6396 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006397 case PV_LCS: return *curwin->w_p_lcs != NUL
6398 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006399 case PV_FCS: return *curwin->w_p_fcs != NUL
6400 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006401 case PV_VE: return *curwin->w_p_ve != NUL
6402 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006403#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006404 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6405#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006406#ifdef FEAT_SYN_HL
6407 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6408 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006409 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006410 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412#ifdef FEAT_DIFF
6413 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6414#endif
6415#ifdef FEAT_FOLDING
6416 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6417 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6418 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6419 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6420 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6421 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6422 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6423# ifdef FEAT_EVAL
6424 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6425 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6426# endif
6427 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6428#endif
6429 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006430 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006431#ifdef FEAT_LINEBREAK
6432 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006435 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006436#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6438#endif
6439#ifdef FEAT_RIGHTLEFT
6440 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6441 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6442#endif
6443 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006444 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6446#ifdef FEAT_LINEBREAK
6447 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006448 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6449 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006451 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006453 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006454#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006455 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6456 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6457#endif
6458#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006459 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6460 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6461 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
6464 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6465 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6468 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6470 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6472 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6473 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006474 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477#ifdef FEAT_FOLDING
6478 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006481#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006482 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006484#ifdef FEAT_COMPL_FUNC
6485 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006486 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006487#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006488#ifdef FEAT_EVAL
6489 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6490#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006491 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006493 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006499 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6501 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6502 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6503 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6504#ifdef FEAT_FIND_ID
6505# ifdef FEAT_EVAL
6506 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6507# endif
6508#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006509#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6511 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6512#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006513#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006514 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516#ifdef FEAT_CRYPT
6517 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006520 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6522 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6523 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6524 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6525 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006527 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6534#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006535 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006536 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6537#endif
6538#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006539 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6540 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6541 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006542 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543#endif
6544 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6545 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6546 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6547 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006548#ifdef FEAT_PERSISTENT_UNDO
6549 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6552#ifdef FEAT_KEYMAP
6553 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6554#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006555#ifdef FEAT_SIGNS
6556 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6557#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006558#ifdef FEAT_VARTABS
6559 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6560 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6561#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006562 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006564 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 return (char_u *)&(curbuf->b_p_wm);
6566}
6567
6568/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006569 * Return a pointer to the variable for option at 'opt_idx'
6570 */
6571 char_u *
6572get_option_var(int opt_idx)
6573{
6574 return options[opt_idx].var;
6575}
6576
Dominique Pelle748b3082022-01-08 12:41:16 +00006577#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006578/*
6579 * Return the full name of the option at 'opt_idx'
6580 */
6581 char_u *
6582get_option_fullname(int opt_idx)
6583{
6584 return (char_u *)options[opt_idx].fullname;
6585}
Dominique Pelle748b3082022-01-08 12:41:16 +00006586#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006587
6588/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006589 * Return the did_set callback function for the option at 'opt_idx'
6590 */
6591 opt_did_set_cb_T
6592get_option_did_set_cb(int opt_idx)
6593{
6594 return options[opt_idx].opt_did_set_cb;
6595}
6596
6597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 * Get the value of 'equalprg', either the buffer-local one or the global one.
6599 */
6600 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006601get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602{
6603 if (*curbuf->b_p_ep == NUL)
6604 return p_ep;
6605 return curbuf->b_p_ep;
6606}
6607
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608/*
6609 * Copy options from one window to another.
6610 * Used when splitting a window.
6611 */
6612 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006613win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614{
6615 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6616 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006617 after_copy_winopt(wp_to);
6618}
6619
6620/*
6621 * After copying window options: update variables depending on options.
6622 */
6623 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006624after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006625{
6626#ifdef FEAT_LINEBREAK
6627 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006628#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006629#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006630 fill_culopt_flags(NULL, wp);
6631 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006632#endif
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006633 set_listchars_option(wp, wp->w_p_lcs, TRUE);
6634 set_fillchars_option(wp, wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006635}
6636
6637 static char_u *
6638copy_option_val(char_u *val)
6639{
6640 if (val == empty_option)
6641 return empty_option; // no need to allocate memory
6642 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
6645/*
6646 * Copy the options from one winopt_T to another.
6647 * Doesn't free the old option values in "to", use clear_winopt() for that.
6648 * The 'scroll' option is not copied, because it depends on the window height.
6649 * The 'previewwindow' option is reset, there can be only one preview window.
6650 */
6651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006652copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653{
6654#ifdef FEAT_ARABIC
6655 to->wo_arab = from->wo_arab;
6656#endif
6657 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006658 to->wo_lcs = copy_option_val(from->wo_lcs);
6659 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006661 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006662 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006663 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006664#ifdef FEAT_LINEBREAK
6665 to->wo_nuw = from->wo_nuw;
6666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667#ifdef FEAT_RIGHTLEFT
6668 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006669 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006671#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006672 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006673#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006674#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006675 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006678#ifdef FEAT_DIFF
6679 to->wo_wrap_save = from->wo_wrap_save;
6680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681#ifdef FEAT_LINEBREAK
6682 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006683 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006684 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006686 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006688 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006689 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006690 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006691 to->wo_crb_save = from->wo_crb_save;
Christian Brabandt4a8eb6e2023-08-13 19:43:42 +02006692 to->wo_siso = from->wo_siso;
6693 to->wo_so = from->wo_so;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006694#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006695 to->wo_spell = from->wo_spell;
6696#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006697#ifdef FEAT_SYN_HL
6698 to->wo_cuc = from->wo_cuc;
6699 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006700 to->wo_culopt = copy_option_val(from->wo_culopt);
6701 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703#ifdef FEAT_DIFF
6704 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006705 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006707#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006708 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006709 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006710#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006711#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006712 to->wo_twk = copy_option_val(from->wo_twk);
6713 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715#ifdef FEAT_FOLDING
6716 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006717 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006719 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006720 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 to->wo_fml = from->wo_fml;
6722 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006723 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006724 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006725 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006726 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 to->wo_fdn = from->wo_fdn;
6728# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006729 to->wo_fde = copy_option_val(from->wo_fde);
6730 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006732 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006734#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006735 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006736#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006737
6738#ifdef FEAT_EVAL
6739 // Copy the script context so that we know where the value was last set.
6740 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6741 sizeof(to->wo_script_ctx));
6742#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006743 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744}
6745
6746/*
6747 * Check string options in a window for a NULL value.
6748 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006749 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006750check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
6752 check_winopt(&win->w_onebuf_opt);
6753 check_winopt(&win->w_allbuf_opt);
6754}
6755
6756/*
6757 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6758 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006759 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006760check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
6762#ifdef FEAT_FOLDING
6763 check_string_option(&wop->wo_fdi);
6764 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006765 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766# ifdef FEAT_EVAL
6767 check_string_option(&wop->wo_fde);
6768 check_string_option(&wop->wo_fdt);
6769# endif
6770 check_string_option(&wop->wo_fmr);
6771#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006772#ifdef FEAT_SIGNS
6773 check_string_option(&wop->wo_scl);
6774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775#ifdef FEAT_RIGHTLEFT
6776 check_string_option(&wop->wo_rlc);
6777#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006778#ifdef FEAT_LINEBREAK
6779 check_string_option(&wop->wo_sbr);
6780#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006781#ifdef FEAT_STL_OPT
6782 check_string_option(&wop->wo_stl);
6783#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006784#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006785 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006786 check_string_option(&wop->wo_cc);
6787#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006788#ifdef FEAT_CONCEAL
6789 check_string_option(&wop->wo_cocu);
6790#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006791#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006792 check_string_option(&wop->wo_twk);
6793 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006794#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006795#ifdef FEAT_LINEBREAK
6796 check_string_option(&wop->wo_briopt);
6797#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006798 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006799 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006800 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006801 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802}
6803
6804/*
6805 * Free the allocated memory inside a winopt_T.
6806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006808clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809{
6810#ifdef FEAT_FOLDING
6811 clear_string_option(&wop->wo_fdi);
6812 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006813 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814# ifdef FEAT_EVAL
6815 clear_string_option(&wop->wo_fde);
6816 clear_string_option(&wop->wo_fdt);
6817# endif
6818 clear_string_option(&wop->wo_fmr);
6819#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006820#ifdef FEAT_SIGNS
6821 clear_string_option(&wop->wo_scl);
6822#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006823#ifdef FEAT_LINEBREAK
6824 clear_string_option(&wop->wo_briopt);
6825#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006826 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827#ifdef FEAT_RIGHTLEFT
6828 clear_string_option(&wop->wo_rlc);
6829#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006830#ifdef FEAT_LINEBREAK
6831 clear_string_option(&wop->wo_sbr);
6832#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006833#ifdef FEAT_STL_OPT
6834 clear_string_option(&wop->wo_stl);
6835#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006836#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006837 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006838 clear_string_option(&wop->wo_cc);
6839#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006840#ifdef FEAT_CONCEAL
6841 clear_string_option(&wop->wo_cocu);
6842#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006843#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006844 clear_string_option(&wop->wo_twk);
6845 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006846#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006847 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006848 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006849 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850}
6851
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006852#ifdef FEAT_EVAL
6853// Index into the options table for a buffer-local option enum.
6854static int buf_opt_idx[BV_COUNT];
6855# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6856
6857/*
6858 * Initialize buf_opt_idx[] if not done already.
6859 */
6860 static void
6861init_buf_opt_idx(void)
6862{
6863 static int did_init_buf_opt_idx = FALSE;
6864 int i;
6865
6866 if (did_init_buf_opt_idx)
6867 return;
6868 did_init_buf_opt_idx = TRUE;
6869 for (i = 0; !istermoption_idx(i); i++)
6870 if (options[i].indir & PV_BUF)
6871 buf_opt_idx[options[i].indir & PV_MASK] = i;
6872}
6873#else
6874# define COPY_OPT_SCTX(buf, bv)
6875#endif
6876
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877/*
6878 * Copy global option values to local options for one buffer.
6879 * Used when creating a new buffer and sometimes when entering a buffer.
6880 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006881 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6883 * appropriate.
6884 * BCO_NOHELP Don't copy the values to a help buffer.
6885 */
6886 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006887buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888{
6889 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006890 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 int dont_do_help;
6892 int did_isk = FALSE;
6893
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006894 // Skip this when the option defaults have not been set yet. Happens when
6895 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 if (p_cpo != NULL)
6897 {
6898 /*
6899 * Always copy when entering and 'cpo' contains 'S'.
6900 * Don't copy when already initialized.
6901 * Don't copy when 'cpo' contains 's' and not entering.
6902 * 'S' BCO_ENTER initialized 's' should_copy
6903 * yes yes X X TRUE
6904 * yes no yes X FALSE
6905 * no X yes X FALSE
6906 * X no no yes FALSE
6907 * X no no no TRUE
6908 * no yes no X TRUE
6909 */
6910 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6911 && (buf->b_p_initialized
6912 || (!(flags & BCO_ENTER)
6913 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6914 should_copy = FALSE;
6915
6916 if (should_copy || (flags & BCO_ALWAYS))
6917 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006918#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006919 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006920 init_buf_opt_idx();
6921#endif
6922 // Don't copy the options specific to a help buffer when
6923 // BCO_NOHELP is given or the options were initialized already
6924 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6926 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006927 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {
6929 save_p_isk = buf->b_p_isk;
6930 buf->b_p_isk = NULL;
6931 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006932 // Always free the allocated strings. If not already initialized,
6933 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 if (!buf->b_p_initialized)
6935 {
6936 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006937 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006940 switch (*p_ffs)
6941 {
6942 case 'm':
6943 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6944 case 'd':
6945 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6946 case 'u':
6947 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6948 default:
6949 buf->b_p_ff = vim_strsave(p_ff);
6950 }
6951 if (buf->b_p_ff != NULL)
6952 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 buf->b_p_bh = empty_option;
6954 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 }
6956 else
6957 free_buf_options(buf, FALSE);
6958
6959 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006960 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 buf->b_p_ai_nopaste = p_ai_nopaste;
6962 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006963 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006965 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 buf->b_p_tw_nopaste = p_tw_nopaste;
6967 buf->b_p_tw_nobin = p_tw_nobin;
6968 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006969 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 buf->b_p_wm_nopaste = p_wm_nopaste;
6971 buf->b_p_wm_nobin = p_wm_nobin;
6972 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006973 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006974 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006975 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006976 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006977 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006979 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006981 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006983 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 buf->b_p_ml_nobin = p_ml_nobin;
6985 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006986 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006987 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006988 buf->b_p_swf = FALSE;
6989 else
6990 {
6991 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006992 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006995 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006996#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006997 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006998 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007000#ifdef FEAT_COMPL_FUNC
7001 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007002 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007003 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00007004 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007005 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007006 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007007#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007008#ifdef FEAT_EVAL
7009 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007010 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007011 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007014 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007016#ifdef FEAT_VARTABS
7017 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007018 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007019 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007020 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007021 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007022 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007023 buf->b_p_vsts_nopaste = p_vsts_nopaste
7024 ? vim_strsave(p_vsts_nopaste) : NULL;
7025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007027 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007029 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030#ifdef FEAT_FOLDING
7031 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007032 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033#endif
7034 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007035 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007036 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007037 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007038 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7039 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007041 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007043 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007045 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007047 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007048
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007050 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007052 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007054 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007055 buf->b_p_cinsd = vim_strsave(p_cinsd);
7056 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007057 buf->b_p_lop = vim_strsave(p_lop);
7058 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007059
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007060 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007063 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007065 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007067 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007069 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007071 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007072 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007073 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007074#endif
7075#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007076 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007077 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007078 (void)compile_cap_prog(&buf->b_s);
7079 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007080 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007081 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007082 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007083 buf->b_s.b_p_spo = vim_strsave(p_spo);
7084 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007086#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007088 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007090 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007092 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007093#if defined(FEAT_EVAL)
7094 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007095 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097#ifdef FEAT_CRYPT
7098 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007099 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007102 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103#ifdef FEAT_KEYMAP
7104 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007105 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 buf->b_kmap_state |= KEYMAP_INIT;
7107#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007108#ifdef FEAT_TERMINAL
7109 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007110 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007111#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007112 // This isn't really an option, but copying the langmap and IME
7113 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007115 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007117 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007119 // options that are normally global but also have a local value
7120 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007122 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007123 buf->b_p_bkc = empty_option;
7124 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125#ifdef FEAT_QUICKFIX
7126 buf->b_p_gp = empty_option;
7127 buf->b_p_mp = empty_option;
7128 buf->b_p_efm = empty_option;
7129#endif
7130 buf->b_p_ep = empty_option;
7131 buf->b_p_kp = empty_option;
7132 buf->b_p_path = empty_option;
7133 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007134 buf->b_p_tc = empty_option;
7135 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136#ifdef FEAT_FIND_ID
7137 buf->b_p_def = empty_option;
7138 buf->b_p_inc = empty_option;
7139# ifdef FEAT_EVAL
7140 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007141 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142# endif
7143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 buf->b_p_dict = empty_option;
7145 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007146#ifdef FEAT_COMPL_FUNC
7147 buf->b_p_tsrfu = empty_option;
7148#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007149 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007150 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007151#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7152 buf->b_p_bexpr = empty_option;
7153#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007154#if defined(FEAT_CRYPT)
7155 buf->b_p_cm = empty_option;
7156#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007157#ifdef FEAT_PERSISTENT_UNDO
7158 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007159 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007160#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007161 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007162 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007164 // Don't copy the options set by ex_help(), use the saved values,
7165 // when going from a help buffer to a non-help buffer.
7166 // Don't touch these at all when BCO_NOHELP is used and going from
7167 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007171#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007172 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007173 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007174 else
7175 buf->b_p_vts_array = NULL;
7176#endif
7177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 else
7179 {
7180 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007181 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 did_isk = TRUE;
7183 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007184 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007185#ifdef FEAT_VARTABS
7186 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007187 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007188 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007189 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007190 else
7191 buf->b_p_vts_array = NULL;
7192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 if (buf->b_p_bt[0] == 'h')
7195 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007197 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 }
7199 }
7200
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007201 // When the options should be copied (ignoring BCO_ALWAYS), set the
7202 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 if (should_copy)
7204 buf->b_p_initialized = TRUE;
7205 }
7206
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007207 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 if (did_isk)
7209 (void)buf_init_chartab(buf, FALSE);
7210}
7211
7212/*
7213 * Reset the 'modifiable' option and its default value.
7214 */
7215 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007216reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217{
7218 int opt_idx;
7219
7220 curbuf->b_p_ma = FALSE;
7221 p_ma = FALSE;
7222 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007223 if (opt_idx >= 0)
7224 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225}
7226
7227/*
7228 * Set the global value for 'iminsert' to the local value.
7229 */
7230 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007231set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232{
7233 p_iminsert = curbuf->b_p_iminsert;
7234}
7235
7236/*
7237 * Set the global value for 'imsearch' to the local value.
7238 */
7239 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007240set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241{
7242 p_imsearch = curbuf->b_p_imsearch;
7243}
7244
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245static int expand_option_idx = -1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007246static int expand_option_start_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7248static int expand_option_flags = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007249static int expand_option_append = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
7251 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007252set_context_in_set_cmd(
7253 expand_T *xp,
7254 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007255 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256{
7257 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007258 long_u flags = 0; // init for GCC
7259 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 char_u *p;
7261 char_u *s;
7262 int is_term_option = FALSE;
7263 int key;
7264
7265 expand_option_flags = opt_flags;
7266
7267 xp->xp_context = EXPAND_SETTINGS;
7268 if (*arg == NUL)
7269 {
7270 xp->xp_pattern = arg;
7271 return;
7272 }
7273 p = arg + STRLEN(arg) - 1;
7274 if (*p == ' ' && *(p - 1) != '\\')
7275 {
7276 xp->xp_pattern = p + 1;
7277 return;
7278 }
7279 while (p > arg)
7280 {
7281 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007282 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 if (*p == ' ' || *p == ',')
7284 {
7285 while (s > arg && *(s - 1) == '\\')
7286 --s;
7287 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007288 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 if (*p == ' ' && ((p - s) & 1) == 0)
7290 {
7291 ++p;
7292 break;
7293 }
7294 --p;
7295 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007296 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 {
7298 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007299 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 p += 2;
7301 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007302 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 {
7304 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007305 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 p += 3;
7307 }
7308 xp->xp_pattern = arg = p;
7309 if (*arg == '<')
7310 {
7311 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007312 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 return;
7314 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007315 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 {
7317 xp->xp_context = EXPAND_NOTHING;
7318 return;
7319 }
7320 nextchar = *++p;
7321 is_term_option = TRUE;
7322 expand_option_name[2] = KEY2TERMCAP0(key);
7323 expand_option_name[3] = KEY2TERMCAP1(key);
7324 }
7325 else
7326 {
7327 if (p[0] == 't' && p[1] == '_')
7328 {
7329 p += 2;
7330 if (*p != NUL)
7331 ++p;
7332 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007333 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 nextchar = *++p;
7335 is_term_option = TRUE;
7336 expand_option_name[2] = p[-2];
7337 expand_option_name[3] = p[-1];
7338 }
7339 else
7340 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007341 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7343 p++;
7344 if (*p == NUL)
7345 return;
7346 nextchar = *p;
7347 *p = NUL;
7348 opt_idx = findoption(arg);
7349 *p = nextchar;
7350 if (opt_idx == -1 || options[opt_idx].var == NULL)
7351 {
7352 xp->xp_context = EXPAND_NOTHING;
7353 return;
7354 }
7355 flags = options[opt_idx].flags;
7356 if (flags & P_BOOL)
7357 {
7358 xp->xp_context = EXPAND_NOTHING;
7359 return;
7360 }
7361 }
7362 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007363 // handle "-=" and "+="
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007364 expand_option_append = FALSE;
7365 int expand_option_subtract = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7367 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007368 if (nextchar == '-')
7369 expand_option_subtract = TRUE;
7370 if (nextchar == '+' || nextchar == '^')
7371 expand_option_append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 ++p;
7373 nextchar = '=';
7374 }
7375 if ((nextchar != '=' && nextchar != ':')
7376 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7377 {
7378 xp->xp_context = EXPAND_UNSUCCESSFUL;
7379 return;
7380 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007381
7382 // Below are for handling expanding a specific option's value after the '='
7383 // or ':'
7384
7385 if (is_term_option)
7386 expand_option_idx = -1;
7387 else
7388 expand_option_idx = opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02007390 if (!is_term_option)
7391 {
7392 if (options[opt_idx].flags & P_NO_CMD_EXPAND)
7393 {
7394 xp->xp_context=EXPAND_UNSUCCESSFUL;
7395 return;
7396 }
7397 }
7398
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 xp->xp_pattern = p + 1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007400 expand_option_start_col = (int)(p + 1 - xp->xp_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007402 // Certain options currently have special case handling to reuse the
7403 // expansion logic with other commands.
Doug Kearns6dfdff32023-08-27 18:48:51 +02007404#ifdef FEAT_SYN_HL
7405 if (options[opt_idx].var == (char_u *)&p_syn)
7406 {
7407 xp->xp_context = EXPAND_OWNSYNTAX;
7408 return;
7409 }
7410#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007411 if (options[opt_idx].var == (char_u *)&p_ft)
7412 {
7413 xp->xp_context = EXPAND_FILETYPE;
7414 return;
7415 }
Doug Kearns81642d92024-01-04 22:37:44 +01007416#ifdef FEAT_KEYMAP
7417 if (options[opt_idx].var == (char_u *)&p_keymap)
7418 {
7419 xp->xp_context = EXPAND_KEYMAP;
7420 return;
7421 }
7422#endif
Doug Kearns6dfdff32023-08-27 18:48:51 +02007423
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007424 // Now pick. If the option has a custom expander, use that. Otherwise, just
7425 // fill with the existing option value.
7426 if (expand_option_subtract)
7427 {
7428 xp->xp_context = EXPAND_SETTING_SUBTRACT;
7429 return;
7430 }
7431 else if (expand_option_idx >= 0 &&
7432 options[expand_option_idx].opt_expand_cb != NULL)
7433 {
7434 xp->xp_context = EXPAND_STRING_SETTING;
7435 }
7436 else if (*xp->xp_pattern == NUL)
7437 {
7438 xp->xp_context = EXPAND_OLD_SETTING;
7439 return;
7440 }
7441 else
7442 xp->xp_context = EXPAND_NOTHING;
7443
7444 if (is_term_option || (flags & P_NUM))
7445 return;
7446
7447 // Only string options below
7448
7449 // Options that have P_EXPAND are considered to all use file/dir expansion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 if (flags & P_EXPAND)
7451 {
7452 p = options[opt_idx].var;
7453 if (p == (char_u *)&p_bdir
7454 || p == (char_u *)&p_dir
7455 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007456 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459#ifdef FEAT_SESSION
7460 || p == (char_u *)&p_vdir
7461#endif
7462 )
7463 {
7464 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007465 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 xp->xp_backslash = XP_BS_THREE;
7467 else
7468 xp->xp_backslash = XP_BS_ONE;
7469 }
7470 else
7471 {
7472 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007473 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 if (p == (char_u *)&p_tags)
7475 xp->xp_backslash = XP_BS_THREE;
7476 else
7477 xp->xp_backslash = XP_BS_ONE;
7478 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02007479 if (flags & P_COMMA)
7480 xp->xp_backslash |= XP_BS_COMMA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 }
7482
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007483 // For an option that is a list of file names, or comma/colon-separated
7484 // values, split it by the delimiter and find the start of the current
7485 // pattern, while accounting for backslash-escaped space/commas/colons.
7486 // Triple-backslashed escaped file names (e.g. 'path') can also be
7487 // delimited by space.
7488 if ((flags & P_EXPAND) || (flags & P_COMMA) || (flags & P_COLON))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007490 for (p = arg + STRLEN(arg) - 1; p >= xp->xp_pattern; --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007492 // count number of backslashes before ' ' or ',' or ':'
7493 if (*p == ' ' || *p == ',' ||
7494 (*p == ':' && (flags & P_COLON)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007496 s = p;
7497 while (s > xp->xp_pattern && *(s - 1) == '\\')
7498 --s;
Yee Cheng Chin54844852023-10-09 18:12:31 +02007499 if ((*p == ' ' && ((xp->xp_backslash & XP_BS_THREE) && (p - s) < 3))
7500#if defined(BACKSLASH_IN_FILENAME)
7501 || (*p == ',' && (flags & P_COMMA) && (p - s) < 1)
7502#else
7503 || (*p == ',' && (flags & P_COMMA) && (p - s) < 2)
7504#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007505 || (*p == ':' && (flags & P_COLON)))
7506 {
7507 xp->xp_pattern = p + 1;
7508 break;
7509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 }
7511 }
7512 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007513
7514 // An option that is a list of single-character flags should always start
7515 // at the end as we don't complete words.
7516 if (flags & P_FLAGLIST)
7517 xp->xp_pattern = arg + STRLEN(arg);
7518
7519 // Some options can either be using file/dir expansions, or custom value
7520 // expansion depending on what the user typed. Unfortunately we have to
7521 // manually handle it here to make sure we have the correct xp_context set.
7522#ifdef FEAT_SPELL
7523 if (options[opt_idx].var == (char_u *)&p_sps)
7524 {
7525 if (STRNCMP(xp->xp_pattern, "file:", 5) == 0)
7526 {
7527 xp->xp_pattern += 5;
7528 return;
7529 }
7530 else if (options[expand_option_idx].opt_expand_cb != NULL)
7531 {
7532 xp->xp_context = EXPAND_STRING_SETTING;
7533 }
7534 }
7535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536}
7537
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007538/*
7539 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7540 *
7541 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7542 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7543 *
7544 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7545 * regular expression 'regmatch', then stores the match in matches[idx] and
7546 * returns TRUE.
7547 *
7548 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7549 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7550 *
7551 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7552 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7553 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02007554 static int
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007555match_str(
7556 char_u *str,
7557 regmatch_T *regmatch,
7558 char_u **matches,
7559 int idx,
7560 int test_only,
7561 int fuzzy,
7562 char_u *fuzzystr,
7563 fuzmatch_str_T *fuzmatch)
7564{
7565 if (!fuzzy)
7566 {
7567 if (vim_regexec(regmatch, str, (colnr_T)0))
7568 {
7569 if (!test_only)
7570 matches[idx] = vim_strsave(str);
7571 return TRUE;
7572 }
7573 }
7574 else
7575 {
7576 int score;
7577
7578 score = fuzzy_match_str(str, fuzzystr);
7579 if (score != 0)
7580 {
7581 if (!test_only)
7582 {
7583 fuzmatch[idx].idx = idx;
7584 fuzmatch[idx].str = vim_strsave(str);
7585 fuzmatch[idx].score = score;
7586 }
7587
7588 return TRUE;
7589 }
7590 }
7591
7592 return FALSE;
7593}
7594
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007596ExpandSettings(
7597 expand_T *xp,
7598 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007599 char_u *fuzzystr,
7600 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007601 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007602 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007604 int num_normal = 0; // Nr of matching non-term-code settings
7605 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 int opt_idx;
7607 int match;
7608 int count = 0;
7609 char_u *str;
7610 int loop;
7611 int is_term_opt;
7612 char_u name_buf[MAX_KEY_NAME_LEN];
7613 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007614 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007615 int fuzzy;
7616 fuzmatch_str_T *fuzmatch = NULL;
7617
Christian Brabandtcb747892022-05-08 21:10:56 +01007618 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007620 // do this loop twice:
7621 // loop == 0: count the number of matching options
7622 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 for (loop = 0; loop <= 1; ++loop)
7624 {
7625 regmatch->rm_ic = ic;
7626 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7627 {
K.Takataeeec2542021-06-02 13:28:16 +02007628 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007629 {
7630 if (match_str((char_u *)names[match], regmatch, *matches,
7631 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 {
7633 if (loop == 0)
7634 num_normal++;
7635 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007636 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 }
7640 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7641 opt_idx++)
7642 {
7643 if (options[opt_idx].var == NULL)
7644 continue;
7645 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007646 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007648 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 if (is_term_opt && num_normal > 0)
7650 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007651
7652 if (match_str(str, regmatch, *matches, count, (loop == 0),
7653 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 {
7655 if (loop == 0)
7656 {
7657 if (is_term_opt)
7658 num_term++;
7659 else
7660 num_normal++;
7661 }
7662 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007663 count++;
7664 }
7665 else if (!fuzzy && options[opt_idx].shortname != NULL
7666 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007667 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007668 {
7669 // Compare against the abbreviated option name (for regular
7670 // expression match). Fuzzy matching (previous if) already
7671 // matches against both the expanded and abbreviated names.
7672 if (loop == 0)
7673 {
7674 if (is_term_opt)
7675 num_term++;
7676 else
7677 num_normal++;
7678 }
7679 else
7680 (*matches)[count++] = vim_strsave(str);
7681 }
7682 else if (is_term_opt)
7683 {
7684 name_buf[0] = '<';
7685 name_buf[1] = 't';
7686 name_buf[2] = '_';
7687 name_buf[3] = str[2];
7688 name_buf[4] = str[3];
7689 name_buf[5] = '>';
7690 name_buf[6] = NUL;
7691
7692 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7693 fuzzy, fuzzystr, fuzmatch))
7694 {
7695 if (loop == 0)
7696 num_term++;
7697 else
7698 count++;
7699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 }
7701 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007702
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007703 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7705 {
7706 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7707 {
Keith Thompson184f71c2024-01-04 21:19:04 +01007708 if (!SAFE_isprint(str[0]) || !SAFE_isprint(str[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 continue;
7710
7711 name_buf[0] = 't';
7712 name_buf[1] = '_';
7713 name_buf[2] = str[0];
7714 name_buf[3] = str[1];
7715 name_buf[4] = NUL;
7716
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007717 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007718 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007719 {
7720 if (loop == 0)
7721 num_term++;
7722 else
7723 count++;
7724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 else
7726 {
7727 name_buf[0] = '<';
7728 name_buf[1] = 't';
7729 name_buf[2] = '_';
7730 name_buf[3] = str[0];
7731 name_buf[4] = str[1];
7732 name_buf[5] = '>';
7733 name_buf[6] = NUL;
7734
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007735 if (match_str(name_buf, regmatch, *matches, count,
7736 (loop == 0), fuzzy, fuzzystr,
7737 fuzmatch))
7738 {
7739 if (loop == 0)
7740 num_term++;
7741 else
7742 count++;
7743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 }
7745 }
7746
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007747 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007748 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7750 {
7751 name_buf[0] = '<';
7752 STRCPY(name_buf + 1, str);
7753 STRCAT(name_buf, ">");
7754
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007755 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7756 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {
7758 if (loop == 0)
7759 num_term++;
7760 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007761 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 }
7763 }
7764 }
7765 if (loop == 0)
7766 {
7767 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007768 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007770 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 else
7772 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007773 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007775 *matches = ALLOC_MULT(char_u *, *numMatches);
7776 if (*matches == NULL)
7777 {
7778 *matches = (char_u **)"";
7779 return FAIL;
7780 }
7781 }
7782 else
7783 {
7784 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7785 if (fuzmatch == NULL)
7786 {
7787 *matches = (char_u **)"";
7788 return FAIL;
7789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 }
7791 }
7792 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007793
7794 if (fuzzy &&
7795 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7796 return FAIL;
7797
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 return OK;
7799}
7800
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007801// Escape an option value that can be used on the command-line with :set.
7802// Caller needs to free the returned string, unless NULL is returned.
7803 static char_u*
7804escape_option_str_cmdline(char_u *var)
7805{
7806 char_u *buf;
7807
7808 // A backslash is required before some characters. This is the reverse of
7809 // what happens in do_set().
7810 buf = vim_strsave_escaped(var, escape_chars);
7811 if (buf == NULL)
7812 return NULL;
7813
7814#ifdef BACKSLASH_IN_FILENAME
7815 // For MS-Windows et al. we don't double backslashes at the start and
7816 // before a file name character.
7817 // The reverse is found at stropt_copy_value().
7818 for (var = buf; *var != NUL; MB_PTR_ADV(var))
7819 if (var[0] == '\\' && var[1] == '\\'
7820 && expand_option_idx >= 0
7821 && (options[expand_option_idx].flags & P_EXPAND)
7822 && vim_isfilec(var[2])
7823 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
7824 STRMOVE(var, var + 1);
7825#endif
7826 return buf;
7827}
7828
7829/*
7830 * Expansion handler for :set= when we just want to fill in with the existing value.
7831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007833ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007835 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 char_u *buf;
7837
zeertzjqb0d45ec2023-01-25 15:04:22 +00007838 *numMatches = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007839 *matches = ALLOC_MULT(char_u *, 1);
zeertzjqb0d45ec2023-01-25 15:04:22 +00007840 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 return FAIL;
7842
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007843 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 if (expand_option_idx < 0)
7845 {
7846 var = find_termcode(expand_option_name + 2);
7847 if (var == NULL)
7848 expand_option_idx = findoption(expand_option_name);
7849 }
7850
7851 if (expand_option_idx >= 0)
7852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007853 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 option_value2string(&options[expand_option_idx], expand_option_flags);
7855 var = NameBuff;
7856 }
7857 else if (var == NULL)
7858 var = (char_u *)"";
7859
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007860 buf = escape_option_str_cmdline(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 if (buf == NULL)
7862 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007863 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 return FAIL;
7865 }
7866
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007867 (*matches)[0] = buf;
zeertzjqb0d45ec2023-01-25 15:04:22 +00007868 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 return OK;
7870}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871
7872/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007873 * Expansion handler for :set=/:set+= when the option has a custom expansion handler.
7874 */
7875 int
7876ExpandStringSetting(
7877 expand_T *xp,
7878 regmatch_T *regmatch,
7879 int *numMatches,
7880 char_u ***matches)
7881{
7882 char_u *var = NULL; // init for GCC
7883 char_u *buf;
7884
7885 if (expand_option_idx < 0 ||
7886 options[expand_option_idx].opt_expand_cb == NULL)
7887 {
7888 // Not supposed to reach this. This function is only for options with
7889 // custom expansion callbacks.
7890 return FAIL;
7891 }
7892
7893 optexpand_T args;
7894 args.oe_varp = get_varp_scope(&options[expand_option_idx], expand_option_flags);
7895 args.oe_append = expand_option_append;
7896 args.oe_regmatch = regmatch;
7897 args.oe_xp = xp;
7898 args.oe_set_arg = xp->xp_line + expand_option_start_col;
7899 args.oe_include_orig_val =
7900 !expand_option_append &&
7901 (*args.oe_set_arg == NUL);
7902
7903 // Retrieve the existing value, but escape it as a reverse of setting it.
7904 // We technically only need to do this when oe_append or
7905 // oe_include_orig_val is true.
7906 option_value2string(&options[expand_option_idx], expand_option_flags);
7907 var = NameBuff;
7908 buf = escape_option_str_cmdline(var);
7909 if (buf == NULL)
7910 return FAIL;
7911
7912 args.oe_opt_value = buf;
7913
7914 int num_ret = options[expand_option_idx].opt_expand_cb(&args, numMatches, matches);
7915
7916 vim_free(buf);
7917 return num_ret;
7918}
7919
7920/*
7921 * Expansion handler for :set-=
7922 */
7923 int
7924ExpandSettingSubtract(
7925 expand_T *xp,
7926 regmatch_T *regmatch,
7927 int *numMatches,
7928 char_u ***matches)
7929{
7930 if (expand_option_idx < 0)
7931 // term option
7932 return ExpandOldSetting(numMatches, matches);
7933
7934 char_u *option_val = *(char_u**)get_option_varp_scope(
7935 expand_option_idx, expand_option_flags);
7936
7937 long_u option_flags = options[expand_option_idx].flags;
7938
7939 if (option_flags & P_NUM)
7940 return ExpandOldSetting(numMatches, matches);
7941 else if (option_flags & P_COMMA)
7942 {
7943 // Split the option by comma, then present each option to the user if
7944 // it matches the pattern.
7945 // This condition needs to go first, because 'whichwrap' has both
7946 // P_COMMA and P_FLAGLIST.
7947 garray_T ga;
7948
7949 char_u *item;
7950 char_u *option_copy;
7951 char_u *next_val;
7952 char_u *comma;
7953
7954 if (*option_val == NUL)
7955 return FAIL;
7956
7957 // Make a copy as we need to inject null characters destructively.
7958 option_copy = vim_strsave(option_val);
7959 if (option_copy == NULL)
7960 return FAIL;
7961 next_val = option_copy;
7962
7963 ga_init2(&ga, sizeof(char_u *), 10);
7964
7965 do
7966 {
7967 item = next_val;
7968 comma = vim_strchr(next_val, ',');
7969 while (comma != NULL && comma != next_val && *(comma - 1) == '\\')
7970 {
7971 // "\," is interpreted as a literal comma rather than option
7972 // separator when reading options in copy_option_part(). Skip
7973 // it.
7974 comma = vim_strchr(comma + 1, ',');
7975 }
7976 if (comma != NULL)
7977 {
7978 *comma = NUL; // null-terminate this value, required by later functions
7979 next_val = comma + 1;
7980 }
7981 else
7982 next_val = NULL;
7983
7984 if (*item == NUL)
7985 // empty value, don't add to list
7986 continue;
7987
7988 if (!vim_regexec(regmatch, item, (colnr_T)0))
7989 continue;
7990
7991 char_u *buf = escape_option_str_cmdline(item);
7992 if (buf == NULL)
7993 {
7994 vim_free(option_copy);
7995 ga_clear_strings(&ga);
7996 return FAIL;
7997 }
7998 if (ga_add_string(&ga, buf) != OK)
7999 {
8000 vim_free(buf);
8001 break;
8002 }
8003 } while (next_val != NULL);
8004
8005 vim_free(option_copy);
8006
8007 *matches = ga.ga_data;
8008 *numMatches = ga.ga_len;
8009 return OK;
8010 }
8011 else if (option_flags & P_FLAGLIST)
8012 {
8013 // Only present the flags that are set on the option as the other flags
8014 // are not meaningful to do set-= on.
8015
8016 if (*xp->xp_pattern != NUL)
8017 {
8018 // Don't suggest anything if cmdline is non-empty. Vim's set-=
8019 // behavior requires consecutive strings and it's usually
Viktor Szépedbf749b2023-10-16 09:53:37 +02008020 // unintuitive to users if they try to subtract multiple flags at
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008021 // once.
8022 return FAIL;
8023 }
8024
Yee Cheng Chin6d113472023-10-02 21:38:39 +02008025 size_t num_flags = STRLEN(option_val);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008026 if (num_flags == 0)
8027 return FAIL;
8028
8029 *matches = ALLOC_MULT(char_u *, num_flags + 1);
8030 if (*matches == NULL)
8031 return FAIL;
8032
8033 int count = 0;
8034 char_u *p;
8035
8036 p = vim_strsave(option_val);
8037 if (p == NULL)
8038 {
8039 VIM_CLEAR(*matches);
8040 return FAIL;
8041 }
8042 (*matches)[count++] = p;
8043
8044 if (num_flags > 1)
8045 {
8046 // If more than one flags, split the flags up and expose each
8047 // character as individual choice.
8048 for (char_u *flag = option_val; *flag != NUL; flag++)
8049 {
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02008050 p = vim_strnsave(flag, 1);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008051 if (p == NULL)
8052 break;
8053 (*matches)[count++] = p;
8054 }
8055 }
8056
8057 *numMatches = count;
8058 return OK;
8059 }
8060
8061 return ExpandOldSetting(numMatches, matches);
8062}
8063
8064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 * Get the value for the numeric or string option *opp in a nice format into
8066 * NameBuff[]. Must not be called with a hidden option!
8067 */
8068 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008069option_value2string(
8070 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008071 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072{
8073 char_u *varp;
8074
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008075 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076
8077 if (opp->flags & P_NUM)
8078 {
8079 long wc = 0;
8080
8081 if (wc_use_keyname(varp, &wc))
8082 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
8083 else if (wc != 0)
8084 STRCPY(NameBuff, transchar((int)wc));
8085 else
8086 sprintf((char *)NameBuff, "%ld", *(long *)varp);
8087 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008088 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {
8090 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008091 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 NameBuff[0] = NUL;
8093#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008094 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008095 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 STRCPY(NameBuff, "*****");
8097#endif
8098 else if (opp->flags & P_EXPAND)
8099 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008100 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 else if ((char_u **)opp->var == &p_pt)
8102 str2specialbuf(p_pt, NameBuff, MAXPATHL);
8103 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008104 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 }
8106}
8107
8108/*
8109 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
8110 * printed as a keyname.
8111 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
8112 */
8113 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008114wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115{
8116 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
8117 {
8118 *wcp = *(long *)varp;
8119 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
8120 return TRUE;
8121 }
8122 return FALSE;
8123}
8124
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 * Return TRUE if "x" is present in 'shortmess' option, or
8127 * 'shortmess' contains 'a' and "x" is present in SHM_A.
8128 */
8129 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008130shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01008132 return p_shm != NULL &&
8133 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 || (vim_strchr(p_shm, 'a') != NULL
8135 && vim_strchr((char_u *)SHM_A, x) != NULL));
8136}
8137
8138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
8140 *
8141 * Reset 'compatible' and set the values for options that didn't get set yet
8142 * to the Vim defaults.
8143 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008144 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 */
8146 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008147vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008149 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00008150 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008151 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152
8153 if (!option_was_set((char_u *)"cp"))
8154 {
8155 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02008156 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
8158 set_option_default(opt_idx, OPT_FREE, FALSE);
8159 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008160 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008162
8163 if (fname != NULL)
8164 {
8165 p = vim_getenv(envname, &dofree);
8166 if (p == NULL)
8167 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008168 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008169 p = FullName_save(fname, FALSE);
8170 if (p != NULL)
8171 {
8172 vim_setenv(envname, p);
8173 vim_free(p);
8174 }
8175 }
8176 else if (dofree)
8177 vim_free(p);
8178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179}
8180
8181/*
8182 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
8183 */
8184 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008185change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008187 int opt_idx;
8188
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 if (p_cp != on)
8190 {
8191 p_cp = on;
8192 compatible_set();
8193 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008194 opt_idx = findoption((char_u *)"cp");
8195 if (opt_idx >= 0)
8196 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197}
8198
8199/*
8200 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008201 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 */
8203 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008204option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205{
8206 int idx;
8207
8208 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008209 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 return FALSE;
8211 if (options[idx].flags & P_WAS_SET)
8212 return TRUE;
8213 return FALSE;
8214}
8215
8216/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008217 * Reset the flag indicating option "name" was set.
8218 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008219 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008220reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008221{
8222 int idx = findoption(name);
8223
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008224 if (idx < 0)
8225 return FAIL;
8226
8227 options[idx].flags &= ~P_WAS_SET;
8228 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008229}
8230
8231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 * compatible_set() - Called when 'compatible' has been set or unset.
8233 *
8234 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8235 * flag) to a Vi compatible value.
8236 * When 'compatible' is unset: Set all options that have a different default
8237 * for Vim (without the P_VI_DEF flag) to that default.
8238 */
8239 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008240compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241{
8242 int opt_idx;
8243
Bram Moolenaardac13472019-09-16 21:06:21 +02008244 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8246 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8247 set_option_default(opt_idx, OPT_FREE, p_cp);
8248 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008249 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250}
8251
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 * Check if backspacing over something is allowed.
8254 */
8255 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008256can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008257 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008259#ifdef FEAT_JOB_CHANNEL
8260 if (what == BS_START && bt_prompt(curbuf))
8261 return FALSE;
8262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 switch (*p_bs)
8264 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008265 case '3': return TRUE;
8266 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 case '1': return (what != BS_START);
8268 case '0': return FALSE;
8269 }
8270 return vim_strchr(p_bs, what) != NULL;
8271}
8272
8273/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008274 * Return the effective 'scrolloff' value for the current window, using the
8275 * global value when appropriate.
8276 */
8277 long
8278get_scrolloff_value(void)
8279{
8280 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8281}
8282
8283/*
8284 * Return the effective 'sidescrolloff' value for the current window, using the
8285 * global value when appropriate.
8286 */
8287 long
8288get_sidescrolloff_value(void)
8289{
8290 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8291}
8292
8293/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008294 * Get the local or global value of 'backupcopy'.
8295 */
8296 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008297get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008298{
8299 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8300}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008301
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008302#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008303/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008304 * Get the local or global value of 'formatlistpat'.
8305 */
8306 char_u *
8307get_flp_value(buf_T *buf)
8308{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008309 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8310 return p_flp;
8311 return buf->b_p_flp;
8312}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008313#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008314
8315/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008316 * Get the local or global value of the 'virtualedit' flags.
8317 */
8318 unsigned int
8319get_ve_flags(void)
8320{
Gary Johnson51ad8502021-08-03 18:33:08 +02008321 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8322 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008323}
8324
Bram Moolenaaree857022019-11-09 23:26:40 +01008325#if defined(FEAT_LINEBREAK) || defined(PROTO)
8326/*
8327 * Get the local or global value of 'showbreak'.
8328 */
8329 char_u *
8330get_showbreak_value(win_T *win)
8331{
8332 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8333 return p_sbr;
8334 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8335 return empty_option;
8336 return win->w_p_sbr;
8337}
8338#endif
8339
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008340#if defined(FEAT_EVAL) || defined(PROTO)
8341/*
8342 * Get window or buffer local options.
8343 */
8344 dict_T *
8345get_winbuf_options(int bufopt)
8346{
8347 dict_T *d;
8348 int opt_idx;
8349
8350 d = dict_alloc();
8351 if (d == NULL)
8352 return NULL;
8353
Bram Moolenaardac13472019-09-16 21:06:21 +02008354 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008355 {
8356 struct vimoption *opt = &options[opt_idx];
8357
8358 if ((bufopt && (opt->indir & PV_BUF))
8359 || (!bufopt && (opt->indir & PV_WIN)))
8360 {
8361 char_u *varp = get_varp(opt);
8362
8363 if (varp != NULL)
8364 {
8365 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008366 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008367 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008368 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008369 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008370 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008371 }
8372 }
8373 }
8374
8375 return d;
8376}
8377#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008378
Bram Moolenaardac13472019-09-16 21:06:21 +02008379#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008380/*
8381 * This is called when 'culopt' is changed
8382 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008383 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008384fill_culopt_flags(char_u *val, win_T *wp)
8385{
8386 char_u *p;
8387 char_u culopt_flags_new = 0;
8388
8389 if (val == NULL)
8390 p = wp->w_p_culopt;
8391 else
8392 p = val;
8393 while (*p != NUL)
8394 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008395 // Note: Keep this in sync with p_culopt_values.
Bram Moolenaar017ba072019-09-14 21:01:23 +02008396 if (STRNCMP(p, "line", 4) == 0)
8397 {
8398 p += 4;
8399 culopt_flags_new |= CULOPT_LINE;
8400 }
8401 else if (STRNCMP(p, "both", 4) == 0)
8402 {
8403 p += 4;
8404 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8405 }
8406 else if (STRNCMP(p, "number", 6) == 0)
8407 {
8408 p += 6;
8409 culopt_flags_new |= CULOPT_NBR;
8410 }
8411 else if (STRNCMP(p, "screenline", 10) == 0)
8412 {
8413 p += 10;
8414 culopt_flags_new |= CULOPT_SCRLINE;
8415 }
8416
8417 if (*p != ',' && *p != NUL)
8418 return FAIL;
8419 if (*p == ',')
8420 ++p;
8421 }
8422
8423 // Can't have both "line" and "screenline".
8424 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8425 return FAIL;
8426 wp->w_p_culopt_flags = culopt_flags_new;
8427
8428 return OK;
8429}
8430#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008431
8432/*
8433 * Get the value of 'magic' adjusted for Vim9 script.
8434 */
8435 int
8436magic_isset(void)
8437{
8438 switch (magic_overruled)
8439 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008440 case OPTION_MAGIC_ON: return TRUE;
8441 case OPTION_MAGIC_OFF: return FALSE;
8442 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008443 }
8444#ifdef FEAT_EVAL
8445 if (in_vim9script())
8446 return TRUE;
8447#endif
8448 return p_magic;
8449}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008450
8451/*
8452 * Set the callback function value for an option that accepts a function name,
8453 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8454 * Returns OK if the option is successfully set to a function, otherwise
8455 * returns FAIL.
8456 */
8457 int
8458option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8459{
8460#ifdef FEAT_EVAL
8461 typval_T *tv;
8462 callback_T cb;
8463
8464 if (optval == NULL || *optval == NUL)
8465 {
8466 free_callback(optcb);
8467 return OK;
8468 }
8469
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008470 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008471 || (STRNCMP(optval, "function(", 9) == 0)
8472 || (STRNCMP(optval, "funcref(", 8) == 0))
8473 // Lambda expression or a funcref
8474 tv = eval_expr(optval, NULL);
8475 else
8476 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008477 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008478 if (tv == NULL)
8479 return FAIL;
8480
8481 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008482 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008483 {
8484 free_tv(tv);
8485 return FAIL;
8486 }
8487
8488 free_callback(optcb);
8489 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008490 if (cb.cb_free_name)
8491 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008492 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008493
8494 // when using Vim9 style "import.funcname" it needs to be expanded to
8495 // "import#funcname".
8496 expand_autload_callback(optcb);
8497
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008498 return OK;
8499#else
8500 return FAIL;
8501#endif
8502}
Christian Brabandt757593c2023-08-22 21:44:10 +02008503
8504#if defined(FEAT_EVAL) || defined(PROTO)
8505 static void
8506didset_options_sctx(int opt_flags, char **buf)
8507{
8508 for (int i = 0; ; ++i)
8509 {
8510 if (buf[i] == NULL)
8511 break;
8512
8513 int idx = findoption((char_u *)buf[i]);
8514 if (idx >= 0)
8515 set_option_sctx_idx(idx, opt_flags, current_sctx);
8516 }
8517}
8518#endif