blob: 7cac89e5cfb0a3e3e70749fe895a6752193458cc [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
453# ifdef MSWIN
454 // MS-Windows has builtin support for conversion to and from Unicode, using
455 // "utf-8" for 'encoding' should work best for most users.
456 p = vim_strsave((char_u *)ENC_DFLT);
457# else
458 // enc_locale() will try to find the encoding of the current locale.
459 // This works best for properly configured systems, old and new.
460 p = enc_locale();
461# endif
462 if (p == NULL)
463 return;
464
465 // Try setting 'encoding' and check if the value is valid.
466 // If not, go back to the default encoding.
467 char_u *save_enc = p_enc;
468 p_enc = p;
469 if (STRCMP(p_enc, "gb18030") == 0)
470 {
471 // We don't support "gb18030", but "cp936" is a good substitute
472 // for practical purposes, thus use that. It's not an alias to
473 // still support conversion between gb18030 and utf-8.
474 p_enc = vim_strsave((char_u *)"cp936");
475 vim_free(p);
476 }
477 if (mb_init() == NULL)
478 {
479 opt_idx = findoption((char_u *)"encoding");
480 if (opt_idx >= 0)
481 {
482 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
483 options[opt_idx].flags |= P_DEF_ALLOCED;
484 }
485
486#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
487 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
488 {
489 // Adjust the default for 'isprint' and 'iskeyword' to match
490 // latin1. Also set the defaults for when 'nocompatible' is
491 // set.
492 set_string_option_direct((char_u *)"isp", -1,
493 ISP_LATIN1, OPT_FREE, SID_NONE);
494 set_string_option_direct((char_u *)"isk", -1,
495 ISK_LATIN1, OPT_FREE, SID_NONE);
496 opt_idx = findoption((char_u *)"isp");
497 if (opt_idx >= 0)
498 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
499 opt_idx = findoption((char_u *)"isk");
500 if (opt_idx >= 0)
501 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
502 (void)init_chartab();
503 }
504#endif
505
506#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
507 // Win32 console: When GetACP() returns a different value from
508 // GetConsoleCP() set 'termencoding'.
509 if (
510# ifdef VIMDLL
511 (!gui.in_use && !gui.starting) &&
512# endif
513 GetACP() != GetConsoleCP())
514 {
515 char buf[50];
516
517 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
518 // Use an alternative value.
519 if (GetConsoleCP() == 0)
520 sprintf(buf, "cp%ld", (long)GetACP());
521 else
522 sprintf(buf, "cp%ld", (long)GetConsoleCP());
523 p_tenc = vim_strsave((char_u *)buf);
524 if (p_tenc != NULL)
525 {
526 opt_idx = findoption((char_u *)"termencoding");
527 if (opt_idx >= 0)
528 {
529 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
530 options[opt_idx].flags |= P_DEF_ALLOCED;
531 }
532 convert_setup(&input_conv, p_tenc, p_enc);
533 convert_setup(&output_conv, p_enc, p_tenc);
534 }
535 else
536 p_tenc = empty_option;
537 }
538#endif
539#if defined(MSWIN)
540 // $HOME may have characters in active code page.
541 init_homedir();
542#endif
543 }
544 else
545 {
546 vim_free(p_enc);
547 p_enc = save_enc;
548 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000549}
550
551/*
552 * Initialize the options, first part.
553 *
554 * Called only once from main(), just after creating the first buffer.
555 * If "clean_arg" is TRUE Vim was started with --clean.
556 */
557 void
558set_init_1(int clean_arg)
559{
560#ifdef FEAT_LANGMAP
561 langmap_init();
562#endif
563
564 // Be Vi compatible by default
565 p_cp = TRUE;
566
567 // Use POSIX compatibility when $VIM_POSIX is set.
568 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
569 {
570 set_string_default("cpo", (char_u *)CPO_ALL);
571 set_string_default("shm", (char_u *)SHM_POSIX);
572 }
573
574 set_init_default_shell();
575 set_init_default_backupskip();
576 set_init_default_maxmemtot();
577 set_init_default_cdpath();
578 set_init_default_printencoding();
579#ifdef FEAT_POSTSCRIPT
580 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581#endif
582
583 /*
584 * Set all the options (except the terminal options) to their default
585 * value. Also set the global value for local options.
586 */
587 set_options_default(0);
588
matveytadbb1bf2022-02-01 17:26:12 +0000589#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000590 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000591#endif
592
Bram Moolenaar07268702018-03-01 21:57:32 +0100593#ifdef CLEAN_RUNTIMEPATH
594 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000595 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100596#endif
597
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598#ifdef FEAT_GUI
599 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100600 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601#endif
602
603 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100604 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100605 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 check_buf_options(curbuf);
607 check_win_options(curwin);
608 check_options();
609
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100610 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 didset_options();
612
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000613#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100614 // Use the current chartab for the generic chartab. This is not in
615 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000616 init_spell_chartab();
617#endif
618
K.Takatace3189d2023-02-15 19:13:43 +0000619 set_init_default_encoding();
620
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000621 // Expand environment variables and things like "~" for the defaults.
622 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100624 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100627 // Detect use of mlterm.
628 // Mlterm is a terminal emulator akin to xterm that has some special
629 // abilities (bidi namely).
630 // NOTE: mlterm's author is being asked to 'set' a variable
631 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100633 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634#endif
635
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200636 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000638 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100641 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 set_helplang_default(get_mess_lang());
643#endif
644}
645
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200646static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
647
648/*
649 * Set the "fileencodings" option to the default value for when 'encoding' is
650 * utf-8.
651 */
652 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000653set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200654{
655 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
656 OPT_FREE, 0);
657}
658
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659/*
660 * Set an option to its default value.
661 * This does not take care of side effects!
662 */
663 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100664set_option_default(
665 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100666 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
667 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100669 char_u *varp; // pointer to variable for current option
670 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000672 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
674
675 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
676 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100677 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
679 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
680 if (flags & P_STRING)
681 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200682 // 'fencs' default value depends on 'encoding'
683 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
684 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100685 // Use set_string_option_direct() for local options to handle
686 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200687 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200688 set_string_option_direct(NULL, opt_idx,
689 options[opt_idx].def_val[dvi], opt_flags, 0);
690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200692 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
693 free_string_option(*(char_u **)(varp));
694 *(char_u **)varp = options[opt_idx].def_val[dvi];
695 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
697 }
698 else if (flags & P_NUM)
699 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000700 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 win_comp_scroll(curwin);
702 else
703 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100704 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
705
706 if ((long *)varp == &curwin->w_p_so
707 || (long *)varp == &curwin->w_p_siso)
708 // 'scrolloff' and 'sidescrolloff' local values have a
709 // different default value than the global default.
710 *(long *)varp = -1;
711 else
712 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100713 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (both)
715 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100716 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
718 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100719 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100721 // the cast to long is required for Manx C, long_i is needed for
722 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000723 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000724#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100725 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000726 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
727 *(int *)varp = FALSE;
728#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100729 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 if (both)
731 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
732 *(int *)varp;
733 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000734
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100735 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000736 flagsp = insecure_flag(opt_idx, opt_flags);
737 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 }
739
740#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200741 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742#endif
743}
744
745/*
746 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200747 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 */
749 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100750set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100751 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
753 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000755 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
Bram Moolenaardac13472019-09-16 21:06:21 +0200757 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200758 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200759 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100760 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200761# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200762 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200763 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200764# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100765 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 set_option_default(i, opt_flags, p_cp);
767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100768 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000769 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200771 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772}
773
774/*
775 * Set the Vi-default value of a string option.
776 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100777 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100779 static void
780set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781{
782 char_u *p;
783 int opt_idx;
784
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100785 if (escape && vim_strchr(val, ' ') != NULL)
786 p = vim_strsave_escaped(val, (char_u *)" ");
787 else
788 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000789 if (p == NULL) // we don't want a NULL
790 return;
791
792 opt_idx = findoption((char_u *)name);
793 if (opt_idx < 0)
794 return;
795
796 if (options[opt_idx].flags & P_DEF_ALLOCED)
797 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
798 options[opt_idx].def_val[VI_DEFAULT] = p;
799 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100802 void
803set_string_default(char *name, char_u *val)
804{
805 set_string_default_esc(name, val, FALSE);
806}
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200809 * For an option value that contains comma separated items, find "newval" in
810 * "origval". Return NULL if not found.
811 */
812 static char_u *
813find_dup_item(char_u *origval, char_u *newval, long_u flags)
814{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200815 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200816 size_t newlen;
817 char_u *s;
818
819 if (origval == NULL)
820 return NULL;
821
822 newlen = STRLEN(newval);
823 for (s = origval; *s != NUL; ++s)
824 {
825 if ((!(flags & P_COMMA)
826 || s == origval
827 || (s[-1] == ',' && !(bs & 1)))
828 && STRNCMP(s, newval, newlen) == 0
829 && (!(flags & P_COMMA)
830 || s[newlen] == ','
831 || s[newlen] == NUL))
832 return s;
833 // Count backslashes. Only a comma with an even number of backslashes
834 // or a single backslash preceded by a comma before it is recognized as
835 // a separator.
836 if ((s > origval + 1
837 && s[-1] == '\\'
838 && s[-2] != ',')
839 || (s == origval + 1
840 && s[-1] == '\\'))
841 ++bs;
842 else
843 bs = 0;
844 }
845 return NULL;
846}
847
848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 * Set the Vi-default value of a number option.
850 * Used for 'lines' and 'columns'.
851 */
852 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100853set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000855 int opt_idx;
856
857 opt_idx = findoption((char_u *)name);
858 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000859 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860}
861
Dominique Pelle748b3082022-01-08 12:41:16 +0000862#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200863/*
864 * Set all window-local and buffer-local options to the Vim default.
865 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200866 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200867 */
868 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200869set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200870{
871 win_T *save_curwin = curwin;
872 int i;
873
874 curwin = wp;
875 curbuf = curwin->w_buffer;
876 block_autocmds();
877
Bram Moolenaardac13472019-09-16 21:06:21 +0200878 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200879 {
880 struct vimoption *p = &(options[i]);
881 char_u *varp = get_varp_scope(p, OPT_LOCAL);
882
883 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200884 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200885 && !(options[i].flags & P_NODEFAULT)
886 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200887 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200888 }
889
890 unblock_autocmds();
891 curwin = save_curwin;
892 curbuf = curwin->w_buffer;
893}
Dominique Pelle748b3082022-01-08 12:41:16 +0000894#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200895
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000896#if defined(EXITFREE) || defined(PROTO)
897/*
898 * Free all options.
899 */
900 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100901free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000902{
903 int i;
904
Bram Moolenaardac13472019-09-16 21:06:21 +0200905 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000906 {
907 if (options[i].indir == PV_NONE)
908 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100909 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100910 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000911 free_string_option(*(char_u **)options[i].var);
912 if (options[i].flags & P_DEF_ALLOCED)
913 free_string_option(options[i].def_val[VI_DEFAULT]);
914 }
915 else if (options[i].var != VAR_WIN
916 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100917 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200918 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000919 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000920 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000921 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000922}
923#endif
924
925
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926/*
927 * Initialize the options, part two: After getting Rows and Columns and
928 * setting 'term'.
929 */
930 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100931set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000933 int idx;
934
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000935 // 'scroll' defaults to half the window height. The stored default is zero,
936 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000937 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000938 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000939 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 comp_col();
941
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000942 // 'window' is only for backwards compatibility with Vi.
943 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +0000944 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000945 p_window = Rows - 1;
946 set_number_default("window", Rows - 1);
947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100948 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100949#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000950 // If 'background' wasn't set by the user, try guessing the value,
951 // depending on the terminal name. Only need to check for terminals
952 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000953 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000954 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
955 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000957 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100958 // don't mark it as set, when starting the GUI it may be
959 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000960 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
962#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000963
964#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000965 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000966#endif
967#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000968 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000969#endif
970#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000971 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Initialize the options, part three: After reading the .vimrc
977 */
978 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100979set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100981#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
984 * This is done after other initializations, where 'shell' might have been
985 * set, but only if they have not been set before.
986 */
987 char_u *p;
988 int idx_srr;
989 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100990# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 int idx_sp;
992 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000996 if (idx_srr < 0)
997 do_srr = FALSE;
998 else
999 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001000# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001002 if (idx_sp < 0)
1003 do_sp = FALSE;
1004 else
1005 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001006# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001007 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (p != NULL)
1009 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001010 // Default for p_sp is "| tee", for p_srr is ">".
1011 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 if ( fnamecmp(p, "csh") == 0
1013 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001014# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 || fnamecmp(p, "csh.exe") == 0
1016 || fnamecmp(p, "tcsh.exe") == 0
1017# endif
1018 )
1019 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001020# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 if (do_sp)
1022 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001023# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001025# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1029 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (do_srr)
1032 {
1033 p_srr = (char_u *)">&";
1034 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1035 }
1036 }
Mike Williams12795022021-06-28 20:53:58 +02001037# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001038 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1039 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001040 else if ( fnamecmp(p, "powershell") == 0
1041 || fnamecmp(p, "powershell.exe") == 0
1042 )
1043 {
1044# if defined(FEAT_QUICKFIX)
1045 if (do_sp)
1046 {
1047 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1048 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1049 }
1050# endif
1051 if (do_srr)
1052 {
1053 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1054 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1055 }
1056 }
1057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 else
Natanael Copa56318362021-05-06 18:46:35 +02001059 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if ( fnamecmp(p, "sh") == 0
1061 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001062 || fnamecmp(p, "mksh") == 0
1063 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001065 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001067 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001068 || fnamecmp(p, "ash") == 0
1069 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001070 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001071# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 || fnamecmp(p, "cmd") == 0
1073 || fnamecmp(p, "sh.exe") == 0
1074 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001075 || fnamecmp(p, "mksh.exe") == 0
1076 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001078 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 || fnamecmp(p, "bash.exe") == 0
1080 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001081 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001082 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001084 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001086# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (do_sp)
1088 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001089# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001091# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001092 if (fnamecmp(p, "pwsh") == 0)
1093 p_sp = (char_u *)">%s 2>&1";
1094 else
1095 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1098 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if (do_srr)
1101 {
1102 p_srr = (char_u *)">%s 2>&1";
1103 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1104 }
1105 }
1106 vim_free(p);
1107 }
1108#endif
1109
Bram Moolenaar4f974752019-02-17 17:44:42 +01001110#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001112 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1113 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001115 * set, but only if they have not been set before.
1116 * Default values depend on shell (cmd.exe is default shell):
1117 *
1118 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001119 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001120 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001121 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001122 * "sh" like shells - "-c" "\""
1123 *
1124 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 */
Mike Williams12795022021-06-28 20:53:58 +02001126 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1127 {
1128 int idx_opt;
1129
1130 idx_opt = findoption((char_u *)"shcf");
1131 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1132 {
1133 p_shcf = (char_u*)"-Command";
1134 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1135 }
1136
1137 idx_opt = findoption((char_u *)"sxq");
1138 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1139 {
1140 p_sxq = (char_u*)"\"";
1141 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1142 }
1143 }
1144 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
1146 int idx3;
1147
1148 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001149 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {
1151 p_shcf = (char_u *)"-c";
1152 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1153 }
1154
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001155 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001157 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {
1159 p_sxq = (char_u *)"\"";
1160 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001163 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1164 {
1165 int idx3;
1166
1167 /*
1168 * cmd.exe on Windows will strip the first and last double quote given
1169 * on the command line, e.g. most of the time things like:
1170 * cmd /c "my path/to/echo" "my args to echo"
1171 * become:
1172 * my path/to/echo" "my args to echo
1173 * when executed.
1174 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001175 * To avoid this, set shellxquote to surround the command in
1176 * parenthesis. This appears to make most commands work, without
1177 * breaking commands that worked previously, such as
1178 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001179 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001180 idx3 = findoption((char_u *)"sxq");
1181 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1182 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001183 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001184 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1185 }
1186
Bram Moolenaara64ba222012-02-12 23:23:31 +01001187 idx3 = findoption((char_u *)"shcf");
1188 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1189 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001190 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001191 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1192 }
1193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#endif
1195
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001196 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001197 {
1198 int idx_ffs = findoption((char_u *)"ffs");
1199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001200 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001201 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1202 set_fileformat(default_fileformat(), OPT_LOCAL);
1203 }
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206}
1207
1208#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1209/*
1210 * When 'helplang' is still at its default value, set it to "lang".
1211 * Only the first two characters of "lang" are used.
1212 */
1213 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001214set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215{
1216 int idx;
1217
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001218 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 return;
1220 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001221 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1222 return;
1223
1224 if (options[idx].flags & P_ALLOCED)
1225 free_string_option(p_hlg);
1226 p_hlg = vim_strsave(lang);
1227 if (p_hlg == NULL)
1228 p_hlg = empty_option;
1229 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001231 // zh_CN becomes "cn", zh_TW becomes "tw"
1232 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001233 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001234 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1235 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001236 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001237 // any C like setting, such as C.UTF-8, becomes "en"
1238 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1239 {
1240 p_hlg[0] = 'e';
1241 p_hlg[1] = 'n';
1242 }
1243 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001245 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246}
1247#endif
1248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249/*
1250 * 'title' and 'icon' only default to true if they have not been set or reset
1251 * in .vimrc and we can read the old value.
1252 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1253 * they can be reset. This reduces startup time when using X on a remote
1254 * machine.
1255 */
1256 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001257set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258{
1259 int idx1;
1260 long val;
1261
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001262 // If GUI is (going to be) used, we can always set the window title and
1263 // icon name. Saves a bit of time, because the X11 display server does
1264 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001266 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {
1268#ifdef FEAT_GUI
1269 if (gui.starting || gui.in_use)
1270 val = TRUE;
1271 else
1272#endif
1273 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001274 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 p_title = val;
1276 }
1277 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001278 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001280 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001282
1283#ifdef FEAT_GUI
1284 if (gui.starting || gui.in_use)
1285 val = TRUE;
1286 else
1287#endif
1288 val = mch_can_restore_icon();
1289 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1290 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001293 void
1294ex_set(exarg_T *eap)
1295{
1296 int flags = 0;
1297
1298 if (eap->cmdidx == CMD_setlocal)
1299 flags = OPT_LOCAL;
1300 else if (eap->cmdidx == CMD_setglobal)
1301 flags = OPT_GLOBAL;
1302#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001303 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001304 ex_options(eap);
1305 else
1306#endif
1307 {
1308 if (eap->forceit)
1309 flags |= OPT_ONECOLUMN;
1310 (void)do_set(eap->arg, flags);
1311 }
1312}
1313
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001314/*
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001315 * :set boolean option prefix
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001316 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001317typedef enum {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001318 PREFIX_NO = 0, // "no" prefix
1319 PREFIX_NONE, // no prefix
1320 PREFIX_INV, // "inv" prefix
1321} set_prefix_T;
1322
1323/*
1324 * Return the prefix type for the option name in *argp.
1325 */
1326 static set_prefix_T
1327get_option_prefix(char_u **argp)
1328{
1329 int prefix = PREFIX_NONE;
1330 char_u *arg = *argp;
1331
1332 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1333 {
1334 prefix = PREFIX_NO;
1335 arg += 2;
1336 }
1337 else if (STRNCMP(arg, "inv", 3) == 0)
1338 {
1339 prefix = PREFIX_INV;
1340 arg += 3;
1341 }
1342
1343 *argp = arg;
1344 return prefix;
1345}
1346
1347/*
1348 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1349 * and the option name length in "*lenp". For a <t_xx> option, return the key
1350 * number in "*keyp".
1351 *
1352 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1353 * otherwise returns OK.
1354 */
1355 static int
1356parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1357{
1358 int key = 0;
1359 int len;
1360 int opt_idx;
1361
1362 if (*arg == '<')
1363 {
1364 opt_idx = -1;
1365 // look out for <t_>;>
1366 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1367 len = 5;
1368 else
1369 {
1370 len = 1;
1371 while (arg[len] != NUL && arg[len] != '>')
1372 ++len;
1373 }
1374 if (arg[len] != '>')
1375 return FAIL;
1376
1377 arg[len] = NUL; // put NUL after name
1378 if (arg[1] == 't' && arg[2] == '_') // could be term code
1379 opt_idx = findoption(arg + 1);
1380 arg[len++] = '>'; // restore '>'
1381 if (opt_idx == -1)
1382 key = find_key_option(arg + 1, TRUE);
1383 }
1384 else
1385 {
1386 int nextchar; // next non-white char after option name
1387
1388 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001389 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001390 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1391 len = 4;
1392 else
1393 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1394 ++len;
1395 nextchar = arg[len];
1396 arg[len] = NUL; // put NUL after name
1397 opt_idx = findoption(arg);
1398 arg[len] = nextchar; // restore nextchar
1399 if (opt_idx == -1)
1400 key = find_key_option(arg, FALSE);
1401 }
1402
1403 *keyp = key;
1404 *lenp = len;
1405 *opt_idxp = opt_idx;
1406
1407 return OK;
1408}
1409
1410/*
1411 * Get the option operator (+=, ^=, -=).
1412 */
1413 static set_op_T
1414get_opt_op(char_u *arg)
1415{
1416 set_op_T op = OP_NONE;
1417
1418 if (*arg != NUL && *(arg + 1) == '=')
1419 {
1420 if (*arg == '+')
1421 op = OP_ADDING; // "+="
1422 else if (*arg == '^')
1423 op = OP_PREPENDING; // "^="
1424 else if (*arg == '-')
1425 op = OP_REMOVING; // "-="
1426 }
1427
1428 return op;
1429}
1430
1431/*
1432 * Validate whether the value of the option in "opt_idx" can be changed.
1433 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1434 * if it can be changed.
1435 */
1436 static int
1437validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1438{
1439 // Skip all options that are not window-local (used when showing
1440 // an already loaded buffer in a window).
1441 if ((opt_flags & OPT_WINONLY)
1442 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1443 return FAIL;
1444
1445 // Skip all options that are window-local (used for :vimgrep).
1446 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1447 && options[opt_idx].var == VAR_WIN)
1448 return FAIL;
1449
1450 // Disallow changing some options from modelines.
1451 if (opt_flags & OPT_MODELINE)
1452 {
1453 if (flags & (P_SECURE | P_NO_ML))
1454 {
1455 *errmsg = e_not_allowed_in_modeline;
1456 return FAIL;
1457 }
1458 if ((flags & P_MLE) && !p_mle)
1459 {
1460 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1461 return FAIL;
1462 }
1463#ifdef FEAT_DIFF
1464 // In diff mode some options are overruled. This avoids that
1465 // 'foldmethod' becomes "marker" instead of "diff" and that
1466 // "wrap" gets set.
1467 if (curwin->w_p_diff
1468 && opt_idx >= 0 // shut up coverity warning
1469 && (
1470# ifdef FEAT_FOLDING
1471 options[opt_idx].indir == PV_FDM ||
1472# endif
1473 options[opt_idx].indir == PV_WRAP))
1474 return FAIL;
1475#endif
1476 }
1477
1478#ifdef HAVE_SANDBOX
1479 // Disallow changing some options in the sandbox
1480 if (sandbox != 0 && (flags & P_SECURE))
1481 {
1482 *errmsg = e_not_allowed_in_sandbox;
1483 return FAIL;
1484 }
1485#endif
1486
1487 return OK;
1488}
1489
Bram Moolenaar47403942022-09-21 21:12:53 +01001490/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001491 * Get the Vim/Vi default value for a string option.
1492 */
1493 static char_u *
1494stropt_get_default_val(
1495 int opt_idx,
1496 char_u *varp,
1497 int flags,
1498 int cp_val)
1499{
1500 char_u *newval;
1501
1502 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1503 ? VI_DEFAULT : VIM_DEFAULT];
1504 if ((char_u **)varp == &p_bg)
1505 {
1506 // guess the value of 'background'
1507#ifdef FEAT_GUI
1508 if (gui.in_use)
1509 newval = gui_bg_default();
1510 else
1511#endif
1512 newval = term_bg_default();
1513 }
1514 else if ((char_u **)varp == &p_fencs && enc_utf8)
1515 newval = fencs_utf8_default;
1516
1517 // expand environment variables and ~ since the default value was
1518 // already expanded, only required when an environment variable was set
1519 // later
1520 if (newval == NULL)
1521 newval = empty_option;
1522 else
1523 {
1524 char_u *s = option_expand(opt_idx, newval);
1525 if (s == NULL)
1526 s = newval;
1527 newval = vim_strsave(s);
1528 }
1529
1530 return newval;
1531}
1532
1533/*
1534 * Convert the 'backspace' option number value to a string: for adding,
1535 * prepending and removing string.
1536 */
1537 static void
1538opt_backspace_nr2str(
1539 char_u *varp,
1540 char_u **origval_p,
1541 char_u **origval_l_p,
1542 char_u **origval_g_p,
1543 char_u **oldval_p)
1544{
1545 int i = getdigits((char_u **)varp);
1546
1547 switch (i)
1548 {
1549 case 0:
1550 *(char_u **)varp = empty_option;
1551 break;
1552 case 1:
1553 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1554 break;
1555 case 2:
1556 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1557 break;
1558 case 3:
1559 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1560 break;
1561 }
1562 vim_free(*oldval_p);
1563 if (*origval_p == *oldval_p)
1564 *origval_p = *(char_u **)varp;
1565 if (*origval_l_p == *oldval_p)
1566 *origval_l_p = *(char_u **)varp;
1567 if (*origval_g_p == *oldval_p)
1568 *origval_g_p = *(char_u **)varp;
1569 *oldval_p = *(char_u **)varp;
1570}
1571
1572/*
1573 * Convert the 'whichwrap' option number value to a string, for backwards
1574 * compatibility with Vim 3.0.
1575 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1576 */
1577 static char_u *
1578opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1579{
1580 *whichwrap = NUL;
1581 int i = getdigits(argp);
1582 if (i & 1)
1583 STRCAT(whichwrap, "b,");
1584 if (i & 2)
1585 STRCAT(whichwrap, "s,");
1586 if (i & 4)
1587 STRCAT(whichwrap, "h,l,");
1588 if (i & 8)
1589 STRCAT(whichwrap, "<,>,");
1590 if (i & 16)
1591 STRCAT(whichwrap, "[,],");
1592 if (*whichwrap != NUL) // remove trailing ,
1593 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1594
1595 return whichwrap;
1596}
1597
1598/*
1599 * Copy the new string value into allocated memory for the option.
1600 * Can't use set_string_option_direct(), because we need to remove the
1601 * backslashes.
1602 */
1603 static char_u *
1604stropt_copy_value(
1605 char_u *origval,
1606 char_u **argp,
1607 set_op_T op,
1608 int flags UNUSED)
1609{
1610 char_u *arg = *argp;
1611 unsigned newlen;
1612 char_u *newval;
1613 char_u *s = NULL;
1614
1615 // get a bit too much
1616 newlen = (unsigned)STRLEN(arg) + 1;
1617 if (op != OP_NONE)
1618 newlen += (unsigned)STRLEN(origval) + 1;
1619 newval = alloc(newlen);
1620 if (newval == NULL) // out of mem, don't change
1621 return NULL;
1622 s = newval;
1623
1624 // Copy the string, skip over escaped chars.
1625 // For MS-DOS and WIN32 backslashes before normal file name characters
1626 // are not removed, and keep backslash at start, for "\\machine\path",
1627 // but do remove it for "\\\\machine\\path".
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001628 // The reverse is found in escape_option_str_cmdline().
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001629 while (*arg != NUL && !VIM_ISWHITE(*arg))
1630 {
1631 int i;
1632
1633 if (*arg == '\\' && arg[1] != NUL
1634#ifdef BACKSLASH_IN_FILENAME
1635 && !((flags & P_EXPAND)
1636 && vim_isfilec(arg[1])
1637 && !VIM_ISWHITE(arg[1])
1638 && (arg[1] != '\\'
1639 || (s == newval && arg[2] != '\\')))
1640#endif
1641 )
1642 ++arg; // remove backslash
1643 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1644 {
1645 // copy multibyte char
1646 mch_memmove(s, arg, (size_t)i);
1647 arg += i;
1648 s += i;
1649 }
1650 else
1651 *s++ = *arg++;
1652 }
1653 *s = NUL;
1654
1655 *argp = arg;
1656 return newval;
1657}
1658
1659/*
1660 * Expand environment variables and ~ in string option value 'newval'.
1661 */
1662 static char_u *
1663stropt_expand_envvar(
1664 int opt_idx,
1665 char_u *origval,
1666 char_u *newval,
1667 set_op_T op)
1668{
1669 char_u *s = option_expand(opt_idx, newval);
1670 if (s == NULL)
1671 return newval;
1672
1673 vim_free(newval);
1674 unsigned newlen = (unsigned)STRLEN(s) + 1;
1675 if (op != OP_NONE)
1676 newlen += (unsigned)STRLEN(origval) + 1;
1677
1678 newval = alloc(newlen);
1679 if (newval == NULL)
1680 return NULL;
1681
1682 STRCPY(newval, s);
1683
1684 return newval;
1685}
1686
1687/*
1688 * Concatenate the original and new values of a string option, adding a "," if
1689 * needed.
1690 */
1691 static void
1692stropt_concat_with_comma(
1693 char_u *origval,
1694 char_u *newval,
1695 set_op_T op,
1696 int flags)
1697{
1698 int len = 0;
1699
1700 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1701 if (op == OP_ADDING)
1702 {
1703 len = (int)STRLEN(origval);
1704 // strip a trailing comma, would get 2
1705 if (comma && len > 1
1706 && (flags & P_ONECOMMA) == P_ONECOMMA
1707 && origval[len - 1] == ','
1708 && origval[len - 2] != '\\')
1709 len--;
1710 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1711 mch_memmove(newval, origval, (size_t)len);
1712 }
1713 else
1714 {
1715 len = (int)STRLEN(newval);
1716 STRMOVE(newval + len + comma, origval);
1717 }
1718 if (comma)
1719 newval[len] = ',';
1720}
1721
1722/*
1723 * Remove a value from a string option. Copy string option value in "origval"
1724 * to "newval" and then remove the string "strval" of length "len".
1725 */
1726 static void
1727stropt_remove_val(
1728 char_u *origval,
1729 char_u *newval,
1730 int flags,
1731 char_u *strval,
1732 int len)
1733{
1734 // Remove newval[] from origval[]. (Note: "len" has been set above
1735 // and is used here).
1736 STRCPY(newval, origval);
1737 if (*strval)
1738 {
1739 // may need to remove a comma
1740 if (flags & P_COMMA)
1741 {
1742 if (strval == origval)
1743 {
1744 // include comma after string
1745 if (strval[len] == ',')
1746 ++len;
1747 }
1748 else
1749 {
1750 // include comma before string
1751 --strval;
1752 ++len;
1753 }
1754 }
1755 STRMOVE(newval + (strval - origval), strval + len);
1756 }
1757}
1758
1759/*
1760 * Remove flags that appear twice in the string option value 'newval'.
1761 */
1762 static void
1763stropt_remove_dupflags(char_u *newval, int flags)
1764{
1765 char_u *s = newval;
1766
1767 // Remove flags that appear twice.
1768 while (*s)
1769 {
1770 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1771 if (flags & P_ONECOMMA)
1772 {
1773 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1774 {
1775 // Remove the duplicated value and the next comma.
1776 STRMOVE(s, s + 2);
1777 continue;
1778 }
1779 }
1780 else
1781 {
1782 if ((!(flags & P_COMMA) || *s != ',')
1783 && vim_strchr(s + 1, *s) != NULL)
1784 {
1785 STRMOVE(s, s + 1);
1786 continue;
1787 }
1788 }
1789 ++s;
1790 }
1791}
1792
1793/*
1794 * Get the string value specified for a ":set" command. The following set
1795 * options are supported:
1796 * set {opt}&
1797 * set {opt}<
1798 * set {opt}={val}
1799 * set {opt}:{val}
1800 */
1801 static char_u *
1802stropt_get_newval(
1803 int nextchar,
1804 int opt_idx,
1805 char_u **argp,
1806 char_u *varp,
1807 char_u **origval_arg,
1808 char_u **origval_l_arg,
1809 char_u **origval_g_arg,
1810 char_u **oldval_arg,
1811 set_op_T *op_arg,
1812 int flags,
1813 int cp_val)
1814{
1815 char_u *arg = *argp;
1816 char_u *origval = *origval_arg;
1817 char_u *origval_l = *origval_l_arg;
1818 char_u *origval_g = *origval_g_arg;
1819 char_u *oldval = *oldval_arg;
1820 set_op_T op = *op_arg;
1821 char_u *save_arg = NULL;
1822 char_u *newval;
1823 char_u *s = NULL;
1824 char_u whichwrap[80];
1825
1826 if (nextchar == '&') // set to default val
1827 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1828 else if (nextchar == '<') // set to global val
1829 newval = vim_strsave(*(char_u **)get_varp_scope(
1830 &(options[opt_idx]), OPT_GLOBAL));
1831 else
1832 {
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001833 ++arg; // jump to after the '=' or ':'
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001834
1835 // Set 'keywordprg' to ":help" if an empty
1836 // value was passed to :set by the user.
1837 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1838 {
1839 save_arg = arg;
1840 arg = (char_u *)":help";
1841 }
1842 // Convert 'backspace' number to string
1843 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1844 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1845 &oldval);
1846 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1847 {
1848 // Convert 'whichwrap' number to string, for backwards
1849 // compatibility with Vim 3.0.
1850 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1851 save_arg = arg;
1852 arg = t;
1853 }
1854 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1855 // version 3.0
1856 else if (*arg == '>' && (varp == (char_u *)&p_dir
1857 || varp == (char_u *)&p_bdir))
1858 ++arg;
1859
1860 // Copy the new string into allocated memory.
1861 newval = stropt_copy_value(origval, &arg, op, flags);
1862 if (newval == NULL)
1863 goto done;
1864
1865 // Expand environment variables and ~.
1866 // Don't do it when adding without inserting a comma.
1867 if (op == OP_NONE || (flags & P_COMMA))
1868 {
1869 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1870 if (newval == NULL)
1871 goto done;
1872 }
1873
1874 // locate newval[] in origval[] when removing it and when adding to
1875 // avoid duplicates
1876 int len = 0;
1877 if (op == OP_REMOVING || (flags & P_NODUP))
1878 {
1879 len = (int)STRLEN(newval);
1880 s = find_dup_item(origval, newval, flags);
1881
1882 // do not add if already there
1883 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1884 {
1885 op = OP_NONE;
1886 STRCPY(newval, origval);
1887 }
1888
1889 // if no duplicate, move pointer to end of original value
1890 if (s == NULL)
1891 s = origval + (int)STRLEN(origval);
1892 }
1893
1894 // concatenate the two strings; add a ',' if needed
1895 if (op == OP_ADDING || op == OP_PREPENDING)
1896 stropt_concat_with_comma(origval, newval, op, flags);
1897 else if (op == OP_REMOVING)
1898 // Remove newval[] from origval[]. (Note: "len" has been set above
1899 // and is used here).
1900 stropt_remove_val(origval, newval, flags, s, len);
1901
1902 if (flags & P_FLAGLIST)
1903 // Remove flags that appear twice.
1904 stropt_remove_dupflags(newval, flags);
1905 }
1906
1907done:
1908 if (save_arg != NULL)
1909 arg = save_arg; // arg was temporarily changed, restore it
1910 *argp = arg;
1911 *origval_arg = origval;
1912 *origval_l_arg = origval_l;
1913 *origval_g_arg = origval_g;
1914 *oldval_arg = oldval;
1915 *op_arg = op;
1916
1917 return newval;
1918}
1919
1920/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001921 * Part of do_set() for string options.
1922 * Returns FAIL on failure, do not process further options.
1923 */
1924 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001925do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001926 int opt_idx,
1927 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001928 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001929 int nextchar,
1930 set_op_T op_arg,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001931 long_u flags,
Bram Moolenaar47403942022-09-21 21:12:53 +01001932 int cp_val,
1933 char_u *varp_arg,
1934 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +01001935 size_t errbuflen,
Bram Moolenaar47403942022-09-21 21:12:53 +01001936 int *value_checked,
1937 char **errmsg)
1938{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001939 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001940 set_op_T op = op_arg;
1941 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001942 char_u *oldval = NULL; // previous value if *varp
1943 char_u *newval;
1944 char_u *origval = NULL;
1945 char_u *origval_l = NULL;
1946 char_u *origval_g = NULL;
1947#if defined(FEAT_EVAL)
1948 char_u *saved_origval = NULL;
1949 char_u *saved_origval_l = NULL;
1950 char_u *saved_origval_g = NULL;
1951 char_u *saved_newval = NULL;
1952#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01001953
1954 // When using ":set opt=val" for a global option
1955 // with a local value the local value will be
1956 // reset, use the global value here.
1957 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1958 && ((int)options[opt_idx].indir & PV_BOTH))
1959 varp = options[opt_idx].var;
1960
1961 // The old value is kept until we are sure that the new value is valid.
1962 oldval = *(char_u **)varp;
1963
1964 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1965 {
1966 origval_l = *(char_u **)get_varp_scope(
1967 &(options[opt_idx]), OPT_LOCAL);
1968 origval_g = *(char_u **)get_varp_scope(
1969 &(options[opt_idx]), OPT_GLOBAL);
1970
1971 // A global-local string option might have an empty option as value to
1972 // indicate that the global value should be used.
1973 if (((int)options[opt_idx].indir & PV_BOTH)
1974 && origval_l == empty_option)
1975 origval_l = origval_g;
1976 }
1977
1978 // When setting the local value of a global option, the old value may be
1979 // the global value.
1980 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1981 origval = *(char_u **)get_varp(&options[opt_idx]);
1982 else
1983 origval = oldval;
1984
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001985 // Get the new value for the option
1986 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
1987 &origval_l, &origval_g, &oldval, &op, flags,
1988 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01001989
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001990 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01001991 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00001992 if (newval == NULL)
1993 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01001994
1995#if defined(FEAT_EVAL)
1996 if (!starting
1997# ifdef FEAT_CRYPT
1998 && options[opt_idx].indir != PV_KEY
1999# endif
2000 && origval != NULL && newval != NULL)
2001 {
2002 // origval may be freed by did_set_string_option(), make a copy.
2003 saved_origval = vim_strsave(origval);
2004 // newval (and varp) may become invalid if the buffer is closed by
2005 // autocommands.
2006 saved_newval = vim_strsave(newval);
2007 if (origval_l != NULL)
2008 saved_origval_l = vim_strsave(origval_l);
2009 if (origval_g != NULL)
2010 saved_origval_g = vim_strsave(origval_g);
2011 }
2012#endif
2013
2014 {
2015 long_u *p = insecure_flag(opt_idx, opt_flags);
2016 int secure_saved = secure;
2017
2018 // When an option is set in the sandbox, from a modeline or in secure
2019 // mode, then deal with side effects in secure mode. Also when the
2020 // value was set with the P_INSECURE flag and is not completely
2021 // replaced.
2022 if ((opt_flags & OPT_MODELINE)
2023#ifdef HAVE_SANDBOX
2024 || sandbox != 0
2025#endif
2026 || (op != OP_NONE && (*p & P_INSECURE)))
2027 secure = 1;
2028
2029 // Handle side effects, and set the global value for ":set" on local
2030 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2031 // be triggered that can cause havoc.
2032 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002033 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002034 errbuflen, opt_flags, op, value_checked);
Bram Moolenaar47403942022-09-21 21:12:53 +01002035
2036 secure = secure_saved;
2037 }
2038
2039#if defined(FEAT_EVAL)
2040 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002041 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002042 saved_origval_l, saved_origval_g, saved_newval);
2043 vim_free(saved_origval);
2044 vim_free(saved_origval_l);
2045 vim_free(saved_origval_g);
2046 vim_free(saved_newval);
2047#endif
2048
Bram Moolenaar6f981142022-09-22 12:48:58 +01002049 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002050 return *errmsg == NULL ? OK : FAIL;
2051}
2052
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002054 * Set a boolean option.
2055 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002056 */
2057 static char *
2058do_set_option_bool(
2059 int opt_idx,
2060 int opt_flags,
2061 set_prefix_T prefix,
2062 long_u flags,
2063 char_u *varp,
2064 int nextchar,
2065 int afterchar,
2066 int cp_val)
2067
2068{
2069 varnumber_T value;
2070
2071 if (nextchar == '=' || nextchar == ':')
2072 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002073 if (opt_idx < 0 || varp == NULL)
2074 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002075
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002076 // ":set opt!": invert
2077 // ":set opt&": reset to default value
2078 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002079 if (nextchar == '!')
2080 value = *(int *)(varp) ^ 1;
2081 else if (nextchar == '&')
2082 value = (int)(long)(long_i)options[opt_idx].def_val[
2083 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2084 else if (nextchar == '<')
2085 {
2086 // For 'autoread' -1 means to use global value.
2087 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2088 value = -1;
2089 else
2090 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2091 }
2092 else
2093 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002094 // ":set invopt": invert
2095 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002096 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2097 return e_trailing_characters;
2098 if (prefix == PREFIX_INV)
2099 value = *(int *)(varp) ^ 1;
2100 else
2101 value = prefix == PREFIX_NO ? 0 : 1;
2102 }
2103
2104 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2105}
2106
2107/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002108 * Set a numeric option.
2109 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002110 */
2111 static char *
2112do_set_option_numeric(
2113 int opt_idx,
2114 int opt_flags,
2115 char_u **argp,
2116 int nextchar,
2117 set_op_T op,
2118 long_u flags,
2119 int cp_val,
2120 char_u *varp,
2121 char *errbuf,
2122 size_t errbuflen)
2123{
2124 char_u *arg = *argp;
2125 varnumber_T value;
2126 int i;
2127 char *errmsg = NULL;
2128
Bram Moolenaar546933f2023-02-06 16:40:49 +00002129 if (opt_idx < 0 || varp == NULL)
2130 return NULL; // "cannot happen"
2131 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002132 /*
2133 * Different ways to set a number option:
2134 * & set to default value
2135 * < set to global value
2136 * <xx> accept special key codes for 'wildchar'
2137 * c accept any non-digit for 'wildchar'
2138 * [-]0-9 set number
2139 * other error
2140 */
2141 ++arg;
2142 if (nextchar == '&')
2143 value = (long)(long_i)options[opt_idx].def_val[
2144 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2145 else if (nextchar == '<')
2146 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002147 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002148 // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002149 value = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002150 else if (opt_flags == OPT_LOCAL
2151 && ((long *)varp == &curwin->w_p_siso
2152 || (long *)varp == &curwin->w_p_so))
2153 // for 'scrolloff'/'sidescrolloff' -1 means using the global value
2154 value = -1;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002155 else
2156 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2157 }
2158 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2159 && (*arg == '<'
2160 || *arg == '^'
2161 || (*arg != NUL
2162 && (!arg[1] || VIM_ISWHITE(arg[1]))
2163 && !VIM_ISDIGIT(*arg))))
2164 {
2165 value = string_to_key(arg, FALSE);
2166 if (value == 0 && (long *)varp != &p_wcm)
2167 {
2168 errmsg = e_invalid_argument;
2169 goto skip;
2170 }
2171 }
2172 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2173 {
2174 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002175 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002176 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2177 {
2178 errmsg = e_number_required_after_equal;
2179 goto skip;
2180 }
2181 }
2182 else
2183 {
2184 errmsg = e_number_required_after_equal;
2185 goto skip;
2186 }
2187
2188 if (op == OP_ADDING)
2189 value = *(long *)varp + value;
2190 else if (op == OP_PREPENDING)
2191 value = *(long *)varp * value;
2192 else if (op == OP_REMOVING)
2193 value = *(long *)varp - value;
2194
2195 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2196 opt_flags);
2197
2198skip:
2199 *argp = arg;
2200 return errmsg;
2201}
2202
2203/*
2204 * Set a key code (t_xx) option
2205 */
2206 static char *
2207do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2208{
2209 char_u *arg = *argp;
2210 char_u *p;
2211
2212 if (nextchar == '&')
2213 {
2214 if (add_termcap_entry(key_name, TRUE) == FAIL)
2215 return e_not_found_in_termcap;
2216 }
2217 else
2218 {
2219 ++arg; // jump to after the '=' or ':'
2220 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2221 if (*p == '\\' && p[1] != NUL)
2222 ++p;
2223 nextchar = *p;
2224 *p = NUL;
2225 add_termcode(key_name, arg, FALSE);
2226 *p = nextchar;
2227 }
2228 if (full_screen)
2229 ttest(FALSE);
2230 redraw_all_later(UPD_CLEAR);
2231
2232 *argp = arg;
2233 return NULL;
2234}
2235
2236/*
2237 * Set an option to a new value.
2238 */
2239 static char *
2240do_set_option_value(
2241 int opt_idx,
2242 int opt_flags,
2243 char_u **argp,
2244 set_prefix_T prefix,
2245 set_op_T op,
2246 long_u flags,
2247 char_u *varp,
2248 char_u *key_name,
2249 int nextchar,
2250 int afterchar,
2251 int cp_val,
2252 int *stopopteval,
2253 char *errbuf,
2254 size_t errbuflen)
2255{
2256 int value_checked = FALSE;
2257 char *errmsg = NULL;
2258 char_u *arg = *argp;
2259
2260 if (flags & P_BOOL)
2261 {
2262 // boolean option
2263 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2264 nextchar, afterchar, cp_val);
2265 if (errmsg != NULL)
2266 goto skip;
2267 }
2268 else
2269 {
2270 // numeric or string option
2271 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2272 || prefix != PREFIX_NONE)
2273 {
2274 errmsg = e_invalid_argument;
2275 goto skip;
2276 }
2277
2278 if (flags & P_NUM)
2279 {
2280 // numeric option
2281 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2282 op, flags, cp_val, varp,
2283 errbuf, errbuflen);
2284 if (errmsg != NULL)
2285 goto skip;
2286 }
2287 else if (opt_idx >= 0)
2288 {
2289 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002290 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002291 flags, cp_val, varp, errbuf, errbuflen,
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002292 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002293 {
2294 if (errmsg != NULL)
2295 goto skip;
2296 *stopopteval = TRUE;
2297 goto skip;
2298 }
2299 }
2300 else
2301 {
2302 // key code option
2303 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2304 if (errmsg != NULL)
2305 goto skip;
2306 }
2307 }
2308
2309 if (opt_idx >= 0)
2310 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2311
2312skip:
2313 *argp = arg;
2314 return errmsg;
2315}
2316
2317/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002318 * Set an option to a new value.
2319 * Return NULL if OK, return an untranslated error message when something is
2320 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2321 */
2322 static char *
2323do_set_option(
2324 int opt_flags,
2325 char_u **argp,
2326 char_u *arg_start,
2327 char_u **startarg,
2328 int *did_show,
2329 int *stopopteval,
2330 char *errbuf,
2331 size_t errbuflen)
2332{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002333 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002334 char_u *arg;
2335 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2336 set_op_T op;
2337 long_u flags; // flags for current option
2338 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002339 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002340 int nextchar; // next non-white char after option name
2341 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002342 char *errmsg = NULL;
2343 int key;
2344 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002345
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002346 prefix = get_option_prefix(argp);
2347 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002348
2349 // find end of name
2350 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002351 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2352 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002353
2354 // remember character after option name
2355 afterchar = arg[len];
2356
2357 if (in_vim9script())
2358 {
2359 char_u *p = skipwhite(arg + len);
2360
2361 // disallow white space before =val, +=val, -=val, ^=val
2362 if (p > arg + len && (p[0] == '='
2363 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2364 && p[1] == '=')))
2365 {
2366 errmsg = e_no_white_space_allowed_between_option_and;
2367 arg = p;
2368 *startarg = p;
2369 goto skip;
2370 }
2371 }
2372 else
2373 // skip white space, allow ":set ai ?", ":set hlsearch !"
2374 while (VIM_ISWHITE(arg[len]))
2375 ++len;
2376
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002377 op = get_opt_op(arg + len);
2378 if (op != OP_NONE)
2379 len++;
2380
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002381 nextchar = arg[len];
2382
2383 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2384 {
2385 if (in_vim9script() && arg > arg_start
2386 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2387 errmsg = e_no_white_space_allowed_between_option_and;
2388 else
2389 errmsg = e_unknown_option;
2390 goto skip;
2391 }
2392
2393 if (opt_idx >= 0)
2394 {
2395 if (options[opt_idx].var == NULL) // hidden option: skip
2396 {
2397 // Only give an error message when requesting the value of
2398 // a hidden option, ignore setting it.
2399 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2400 && (!(options[opt_idx].flags & P_BOOL)
2401 || nextchar == '?'))
2402 errmsg = e_option_not_supported;
2403 goto skip;
2404 }
2405
2406 flags = options[opt_idx].flags;
2407 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2408 }
2409 else
2410 {
2411 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002412 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002413 if (key < 0)
2414 {
2415 key_name[0] = KEY2TERMCAP0(key);
2416 key_name[1] = KEY2TERMCAP1(key);
2417 }
2418 else
2419 {
2420 key_name[0] = KS_KEY;
2421 key_name[1] = (key & 0xff);
2422 }
2423 }
2424
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002425 // Make sure the option value can be changed.
2426 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002427 goto skip;
2428
Bram Moolenaar40b48722023-02-05 17:04:50 +00002429 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002430 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2431 {
2432 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002433 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2434 {
2435 if (arg[3] == 'm') // "opt&vim": set to Vim default
2436 {
2437 cp_val = FALSE;
2438 arg += 3;
2439 }
2440 else // "opt&vi": set to Vi default
2441 {
2442 cp_val = TRUE;
2443 arg += 2;
2444 }
2445 }
2446 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2447 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2448 {
2449 errmsg = e_trailing_characters;
2450 goto skip;
2451 }
2452 }
2453
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002454 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2455 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002456 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002457 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002458 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2459 && !(flags & P_BOOL)))
2460 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002461 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002462 if (*did_show)
2463 msg_putchar('\n'); // cursor below last one
2464 else
2465 {
2466 gotocmdline(TRUE); // cursor at status line
2467 *did_show = TRUE; // remember that we did a line
2468 }
2469 if (opt_idx >= 0)
2470 {
2471 showoneopt(&options[opt_idx], opt_flags);
2472#ifdef FEAT_EVAL
2473 if (p_verbose > 0)
2474 {
2475 // Mention where the option was last set.
2476 if (varp == options[opt_idx].var)
2477 last_set_msg(options[opt_idx].script_ctx);
2478 else if ((int)options[opt_idx].indir & PV_WIN)
2479 last_set_msg(curwin->w_p_script_ctx[
2480 (int)options[opt_idx].indir & PV_MASK]);
2481 else if ((int)options[opt_idx].indir & PV_BUF)
2482 last_set_msg(curbuf->b_p_script_ctx[
2483 (int)options[opt_idx].indir & PV_MASK]);
2484 }
2485#endif
2486 }
2487 else
2488 {
2489 char_u *p;
2490
2491 p = find_termcode(key_name);
2492 if (p == NULL)
2493 {
2494 errmsg = e_key_code_not_set;
2495 goto skip;
2496 }
2497 else
2498 (void)show_one_termcode(key_name, p, TRUE);
2499 }
2500 if (nextchar != '?'
2501 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2502 errmsg = e_trailing_characters;
2503 }
2504 else
2505 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002506 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2507 flags, varp, key_name, nextchar,
2508 afterchar, cp_val, stopopteval, errbuf,
2509 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002510 }
2511
2512skip:
2513 *argp = arg;
2514 return errmsg;
2515}
2516
2517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 * Parse 'arg' for option settings.
2519 *
2520 * 'arg' may be IObuff, but only when no errors can be present and option
2521 * does not need to be expanded with option_expand().
2522 * "opt_flags":
2523 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002524 * OPT_GLOBAL for ":setglobal"
2525 * OPT_LOCAL for ":setlocal" and a modeline
2526 * OPT_MODELINE for a modeline
2527 * OPT_WINONLY to only set window-local options
2528 * OPT_NOWIN to skip setting window-local options
2529 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002531 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 */
2533 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002534do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002535 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002536 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002538 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002540 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
2542 if (*arg == NUL)
2543 {
2544 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002545 did_show = TRUE;
2546 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 }
2548
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002549 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002551 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002552 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002554 // ":set all" show all options.
2555 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 arg += 3;
2557 if (*arg == '&')
2558 {
2559 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002560 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002562 didset_options();
2563 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002564 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 }
2566 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002569 did_show = TRUE;
2570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002572 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
2574 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002575 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002576 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 arg += 7;
2578 }
2579 else
2580 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002581 int stopopteval = FALSE;
2582 char *errmsg = NULL;
Christian Brabandtb39b2402023-11-29 11:34:05 +01002583 char errbuf[ERR_BUFLEN];
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002584 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002586 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2587 &did_show, &stopopteval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002588 ERR_BUFLEN);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002589 if (stopopteval)
2590 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002592 // Advance to next argument.
2593 // - skip until a blank found, taking care of backslashes
2594 // - skip blanks
2595 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 for (i = 0; i < 2 ; ++i)
2597 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002598 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 if (*arg++ == '\\' && *arg != NUL)
2600 ++arg;
2601 arg = skipwhite(arg);
2602 if (*arg != '=')
2603 break;
2604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002606 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002608 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2609 i = (int)STRLEN(IObuff) + 2;
2610 if (i + (arg - startarg) < IOSIZE)
2611 {
2612 // append the argument with the error
2613 STRCAT(IObuff, ": ");
2614 mch_memmove(IObuff + i, startarg, (arg - startarg));
2615 IObuff[i + (arg - startarg)] = NUL;
2616 }
2617 // make sure all characters are printable
2618 trans_characters(IObuff, IOSIZE);
2619
2620 ++no_wait_return; // wait_return() done later
2621 emsg((char *)IObuff); // show error highlighted
2622 --no_wait_return;
2623
2624 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
2627
2628 arg = skipwhite(arg);
2629 }
2630
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002631theend:
2632 if (silent_mode && did_show)
2633 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002634 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002635 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002636 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002637 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002638 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002639 out_flush();
2640 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002641 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002642 }
2643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 return OK;
2645}
2646
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002647/*
2648 * Call this when an option has been given a new value through a user command.
2649 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2650 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002652did_set_option(
2653 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002654 int opt_flags, // possibly with OPT_MODELINE
2655 int new_value, // value was replaced completely
2656 int value_checked) // value was checked to be safe, no need to set the
2657 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002658{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002659 long_u *p;
2660
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002661 options[opt_idx].flags |= P_WAS_SET;
2662
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002663 // When an option is set in the sandbox, from a modeline or in secure mode
2664 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2665 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002666 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002667 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002668#ifdef HAVE_SANDBOX
2669 || sandbox != 0
2670#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002671 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002672 *p = *p | P_INSECURE;
2673 else if (new_value)
2674 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002675}
2676
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677/*
2678 * Convert a key name or string into a key value.
2679 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002680 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002682 int
2683string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
2685 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002686 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (*arg == '^')
2688 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002689 if (multi_byte)
2690 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 return *arg;
2692}
2693
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694/*
2695 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2696 * maketitle() to create and display it.
2697 * When switching the title or icon off, call mch_restore_title() to get
2698 * the old value back.
2699 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002700 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002701did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
2703 if (starting != NO_SCREEN
2704#ifdef FEAT_GUI
2705 && !gui.starting
2706#endif
2707 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711/*
2712 * set_options_bin - called when 'bin' changes value.
2713 */
2714 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002715set_options_bin(
2716 int oldval,
2717 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002718 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002720 // The option values that are changed when 'bin' changes are
2721 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 if (newval)
2723 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002724 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
2726 if (!(opt_flags & OPT_GLOBAL))
2727 {
2728 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2729 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2730 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2731 curbuf->b_p_et_nobin = curbuf->b_p_et;
2732 }
2733 if (!(opt_flags & OPT_LOCAL))
2734 {
2735 p_tw_nobin = p_tw;
2736 p_wm_nobin = p_wm;
2737 p_ml_nobin = p_ml;
2738 p_et_nobin = p_et;
2739 }
2740 }
2741
2742 if (!(opt_flags & OPT_GLOBAL))
2743 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002744 curbuf->b_p_tw = 0; // no automatic line wrap
2745 curbuf->b_p_wm = 0; // no automatic line wrap
2746 curbuf->b_p_ml = 0; // no modelines
2747 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 }
2749 if (!(opt_flags & OPT_LOCAL))
2750 {
2751 p_tw = 0;
2752 p_wm = 0;
2753 p_ml = FALSE;
2754 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002755 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
2757 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002758 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
2760 if (!(opt_flags & OPT_GLOBAL))
2761 {
2762 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2763 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2764 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2765 curbuf->b_p_et = curbuf->b_p_et_nobin;
2766 }
2767 if (!(opt_flags & OPT_LOCAL))
2768 {
2769 p_tw = p_tw_nobin;
2770 p_wm = p_wm_nobin;
2771 p_ml = p_ml_nobin;
2772 p_et = p_et_nobin;
2773 }
2774 }
Christian Brabandt757593c2023-08-22 21:44:10 +02002775#if defined(FEAT_EVAL) || defined(PROTO)
2776 // Remember where the dependent option were reset
2777 didset_options_sctx(opt_flags, p_bin_dep_opts);
2778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779}
2780
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781/*
2782 * Expand environment variables for some string options.
2783 * These string options cannot be indirect!
2784 * If "val" is NULL expand the current value of the option.
2785 * Return pointer to NameBuff, or NULL when not expanded.
2786 */
2787 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002788option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002790 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2792 return NULL;
2793
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002794 // If val is longer than MAXPATHL no meaningful expansion can be done,
2795 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (val != NULL && STRLEN(val) > MAXPATHL)
2797 return NULL;
2798
2799 if (val == NULL)
2800 val = *(char_u **)options[opt_idx].var;
2801
2802 /*
2803 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2804 * Escape spaces when expanding 'tags', they are used to separate file
2805 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002806 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 */
2808 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002809 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002810#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002811 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2812#endif
2813 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002814 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 return NULL;
2816
2817 return NameBuff;
2818}
2819
2820/*
2821 * After setting various option values: recompute variables that depend on
2822 * option values.
2823 */
2824 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002825didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002827 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 (void)init_chartab();
2829
Bram Moolenaardac13472019-09-16 21:06:21 +02002830 didset_string_options();
2831
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002832#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002833 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002834 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002835 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002836 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002837#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002838 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002839 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002840#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002841 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002842 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002843#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002844 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002845}
2846
2847/*
2848 * More side effects of setting options.
2849 */
2850 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002851didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002852{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002854 (void)highlight_changed();
2855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002856 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002857 check_opt_wim();
2858
Bram Moolenaareed9d462021-02-15 20:38:25 +01002859 // Parse default for 'listchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002860 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002862 // Parse default for 'fillchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002863 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002864
2865#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002866 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002867 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002868#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002869#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002870 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002871 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002872 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002873 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875}
2876
2877/*
2878 * Check for string options that are NULL (normally only termcap options).
2879 */
2880 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002881check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
2883 int opt_idx;
2884
2885 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2886 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2887 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2888}
2889
2890/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002891 * Return the option index found by a pointer into term_strings[].
2892 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002894 int
2895get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002897 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898
2899 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2900 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002901 return opt_idx;
2902 return -1; // cannot happen: didn't find it!
2903}
2904
2905/*
2906 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2907 * Return the option index or -1 if not found.
2908 */
2909 int
2910set_term_option_alloced(char_u **p)
2911{
2912 int opt_idx = get_term_opt_idx(p);
2913
2914 if (opt_idx >= 0)
2915 options[opt_idx].flags |= P_ALLOCED;
2916 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917}
2918
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002919#if defined(FEAT_EVAL) || defined(PROTO)
2920/*
2921 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2922 * Return FALSE when it wasn't.
2923 * Return -1 for an unknown option.
2924 */
2925 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002926was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002927{
2928 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002929 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002930
2931 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002932 {
2933 flagp = insecure_flag(idx, opt_flags);
2934 return (*flagp & P_INSECURE) != 0;
2935 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002936 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002937 return -1;
2938}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002939
2940/*
2941 * Get a pointer to the flags used for the P_INSECURE flag of option
2942 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002943 * NOTE: Caller must make sure that "curwin" is set to the window from which
2944 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002945 */
2946 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002947insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002948{
2949 if (opt_flags & OPT_LOCAL)
2950 switch ((int)options[opt_idx].indir)
2951 {
2952#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002953 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002954#endif
2955#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002956# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002957 case PV_FDE: return &curwin->w_p_fde_flags;
2958 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002959# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002960# ifdef FEAT_BEVAL
2961 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2962# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002963 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002964 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002965# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002966 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002967# endif
2968#endif
2969 }
2970
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002971 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002972 return &options[opt_idx].flags;
2973}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002974#endif
2975
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002976/*
2977 * Redraw the window title and/or tab page text later.
2978 */
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002979 void
2980redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002981{
2982 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002983 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002984}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002985
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002987 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2988 * characters or characters in "allowed".
2989 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002990 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002991valid_name(char_u *val, char *allowed)
2992{
2993 char_u *s;
2994
2995 for (s = val; *s != NUL; ++s)
2996 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2997 return FALSE;
2998 return TRUE;
2999}
3000
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003001#if defined(FEAT_EVAL) || defined(PROTO)
3002/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003003 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003004 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003005 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003006 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003007set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003008{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003009 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3010 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003011 sctx_T new_script_ctx = script_ctx;
3012
Bram Moolenaar51258742020-05-03 17:19:33 +02003013 // Modeline already has the line number set.
3014 if (!(opt_flags & OPT_MODELINE))
3015 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003017 // Remember where the option was set. For local options need to do that
3018 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003019 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003020 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003021 if (both || (opt_flags & OPT_LOCAL))
3022 {
3023 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003024 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003025 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003026 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003027 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003028 if (both)
3029 // also setting the "all buffers" value
3030 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3031 new_script_ctx;
3032 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003033 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003034}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003035
3036/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003037 * Get the script context of global option "name".
3038 *
3039 */
3040 sctx_T *
3041get_option_sctx(char *name)
3042{
3043 int idx = findoption((char_u *)name);
3044
3045 if (idx >= 0)
3046 return &options[idx].script_ctx;
3047 siemsg("no such option: %s", name);
3048 return NULL;
3049}
3050
3051/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003052 * Set the script_ctx for a termcap option.
3053 * "name" must be the two character code, e.g. "RV".
3054 * When "name" is NULL use "opt_idx".
3055 */
3056 void
3057set_term_option_sctx_idx(char *name, int opt_idx)
3058{
3059 char_u buf[5];
3060 int idx;
3061
3062 if (name == NULL)
3063 idx = opt_idx;
3064 else
3065 {
3066 buf[0] = 't';
3067 buf[1] = '_';
3068 buf[2] = name[0];
3069 buf[3] = name[1];
3070 buf[4] = 0;
3071 idx = findoption(buf);
3072 }
3073 if (idx >= 0)
3074 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3075}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003076#endif
3077
Bram Moolenaarf4140482020-02-15 23:06:45 +01003078#if defined(FEAT_EVAL)
3079/*
3080 * Apply the OptionSet autocommand.
3081 */
3082 static void
3083apply_optionset_autocmd(
3084 int opt_idx,
3085 long opt_flags,
3086 long oldval,
3087 long oldval_g,
3088 long newval,
3089 char *errmsg)
3090{
3091 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3092
3093 // Don't do this while starting up, failure or recursively.
3094 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3095 return;
3096
3097 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3098 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3099 oldval_g);
3100 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3101 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3102 (opt_flags & OPT_LOCAL) ? "local" : "global");
3103 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3104 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3105 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3106 if (opt_flags & OPT_LOCAL)
3107 {
3108 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3109 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3110 }
3111 if (opt_flags & OPT_GLOBAL)
3112 {
3113 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3114 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3115 }
3116 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3117 {
3118 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3119 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3120 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3121 }
3122 if (opt_flags & OPT_MODELINE)
3123 {
3124 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3125 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3126 }
3127 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3128 NULL, FALSE, NULL);
3129 reset_v_option_vars();
3130}
3131#endif
3132
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003133#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003134/*
3135 * Process the updated 'arabic' option value.
3136 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003137 char *
3138did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003139{
3140 char *errmsg = NULL;
3141
3142 if (curwin->w_p_arab)
3143 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003144 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003145 if (!p_tbidi)
3146 {
3147 // set rightleft mode
3148 if (!curwin->w_p_rl)
3149 {
3150 curwin->w_p_rl = TRUE;
3151 changed_window_setting();
3152 }
3153
3154 // Enable Arabic shaping (major part of what Arabic requires)
3155 if (!p_arshape)
3156 {
3157 p_arshape = TRUE;
3158 redraw_later_clear();
3159 }
3160 }
3161
3162 // Arabic requires a utf-8 encoding, inform the user if it's not
3163 // set.
3164 if (STRCMP(p_enc, "utf-8") != 0)
3165 {
3166 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3167
3168 msg_source(HL_ATTR(HLF_W));
3169 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3170#ifdef FEAT_EVAL
3171 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3172#endif
3173 }
3174
3175 // set 'delcombine'
3176 p_deco = TRUE;
3177
3178# ifdef FEAT_KEYMAP
3179 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003180 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3181 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003182# endif
3183 }
3184 else
3185 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003186 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003187 if (!p_tbidi)
3188 {
3189 // reset rightleft mode
3190 if (curwin->w_p_rl)
3191 {
3192 curwin->w_p_rl = FALSE;
3193 changed_window_setting();
3194 }
3195
3196 // 'arabicshape' isn't reset, it is a global option and
3197 // another window may still need it "on".
3198 }
3199
3200 // 'delcombine' isn't reset, it is a global option and another
3201 // window may still want it "on".
3202
3203# ifdef FEAT_KEYMAP
3204 // Revert to the default keymap
3205 curbuf->b_p_iminsert = B_IMODE_NONE;
3206 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3207# endif
3208 }
3209
3210 return errmsg;
3211}
3212#endif
3213
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003214#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3215/*
3216 * Process the updated 'autochdir' option value.
3217 */
3218 char *
3219did_set_autochdir(optset_T *args UNUSED)
3220{
3221 // Change directories when the 'acd' option is set now.
3222 DO_AUTOCHDIR;
3223 return NULL;
3224}
3225#endif
3226
3227#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3228/*
3229 * Process the updated 'ballooneval' option value.
3230 */
3231 char *
3232did_set_ballooneval(optset_T *args)
3233{
3234 if (balloonEvalForTerm)
3235 return NULL;
3236
3237 if (p_beval && !args->os_oldval.boolean)
3238 gui_mch_enable_beval_area(balloonEval);
3239 else if (!p_beval && args->os_oldval.boolean)
3240 gui_mch_disable_beval_area(balloonEval);
3241
3242 return NULL;
3243}
3244#endif
3245
3246#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3247/*
3248 * Process the updated 'balloonevalterm' option value.
3249 */
3250 char *
3251did_set_balloonevalterm(optset_T *args UNUSED)
3252{
3253 mch_bevalterm_changed();
3254 return NULL;
3255}
3256#endif
3257
3258/*
3259 * Process the updated 'binary' option value.
3260 */
3261 char *
3262did_set_binary(optset_T *args)
3263{
3264 // when 'bin' is set also set some other options
3265 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3266 redraw_titles();
3267
3268 return NULL;
3269}
3270
3271#if defined(FEAT_LINEBREAK) || defined(PROTO)
3272/*
3273 * Called when the 'breakat' option changes value.
3274 */
3275 char *
3276did_set_breakat(optset_T *args UNUSED)
3277{
3278 char_u *p;
3279 int i;
3280
3281 for (i = 0; i < 256; i++)
3282 breakat_flags[i] = FALSE;
3283
3284 if (p_breakat != NULL)
3285 for (p = p_breakat; *p; p++)
3286 breakat_flags[*p] = TRUE;
3287
3288 return NULL;
3289}
3290#endif
3291
3292/*
3293 * Process the updated 'buflisted' option value.
3294 */
3295 char *
3296did_set_buflisted(optset_T *args)
3297{
3298 // when 'buflisted' changes, trigger autocommands
3299 if (args->os_oldval.boolean != curbuf->b_p_bl)
3300 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3301 NULL, NULL, TRUE, curbuf);
3302 return NULL;
3303}
3304
3305/*
3306 * Process the new 'cmdheight' option value.
3307 */
3308 char *
3309did_set_cmdheight(optset_T *args)
3310{
3311 long old_value = args->os_oldval.number;
3312 char *errmsg = NULL;
3313
3314 // if p_ch changed value, change the command line height
3315 if (p_ch < 1)
3316 {
3317 errmsg = e_argument_must_be_positive;
3318 p_ch = 1;
3319 }
3320 if (p_ch > Rows - min_rows() + 1)
3321 p_ch = Rows - min_rows() + 1;
3322
3323 // Only compute the new window layout when startup has been
3324 // completed. Otherwise the frame sizes may be wrong.
3325 if ((p_ch != old_value
3326 || tabline_height() + topframe->fr_height != Rows - p_ch)
3327 && full_screen
3328#ifdef FEAT_GUI
3329 && !gui.starting
3330#endif
3331 )
3332 command_height();
3333
3334 return errmsg;
3335}
3336
3337/*
3338 * Process the updated 'compatible' option value.
3339 */
3340 char *
3341did_set_compatible(optset_T *args UNUSED)
3342{
3343 compatible_set();
3344 return NULL;
3345}
3346
3347#if defined(FEAT_CONCEAL) || defined(PROTO)
3348/*
3349 * Process the new 'conceallevel' option value.
3350 */
3351 char *
3352did_set_conceallevel(optset_T *args UNUSED)
3353{
3354 char *errmsg = NULL;
3355
3356 if (curwin->w_p_cole < 0)
3357 {
3358 errmsg = e_argument_must_be_positive;
3359 curwin->w_p_cole = 0;
3360 }
3361 else if (curwin->w_p_cole > 3)
3362 {
3363 errmsg = e_invalid_argument;
3364 curwin->w_p_cole = 3;
3365 }
3366
3367 return errmsg;
3368}
3369#endif
3370
3371#if defined(FEAT_DIFF) || defined(PROTO)
3372/*
3373 * Process the updated 'diff' option value.
3374 */
3375 char *
3376did_set_diff(optset_T *args UNUSED)
3377{
3378 // May add or remove the buffer from the list of diff buffers.
3379 diff_buf_adjust(curwin);
3380# ifdef FEAT_FOLDING
3381 if (foldmethodIsDiff(curwin))
3382 foldUpdateAll(curwin);
3383# endif
3384 return NULL;
3385}
3386#endif
3387
3388/*
3389 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3390 * option value.
3391 */
3392 char *
3393did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3394{
3395 // redraw the window title and tab page text
3396 redraw_titles();
3397 return NULL;
3398}
3399
3400/*
3401 * Process the updated 'equalalways' option value.
3402 */
3403 char *
3404did_set_equalalways(optset_T *args)
3405{
3406 if (p_ea && !args->os_oldval.boolean)
3407 win_equal(curwin, FALSE, 0);
3408
3409 return NULL;
3410}
3411
3412#if defined(FEAT_FOLDING) || defined(PROTO)
3413/*
3414 * Process the new 'foldcolumn' option value.
3415 */
3416 char *
3417did_set_foldcolumn(optset_T *args UNUSED)
3418{
3419 char *errmsg = NULL;
3420
3421 if (curwin->w_p_fdc < 0)
3422 {
3423 errmsg = e_argument_must_be_positive;
3424 curwin->w_p_fdc = 0;
3425 }
3426 else if (curwin->w_p_fdc > 12)
3427 {
3428 errmsg = e_invalid_argument;
3429 curwin->w_p_fdc = 12;
3430 }
3431
3432 return errmsg;
3433}
3434
3435/*
3436 * Process the new 'foldlevel' option value.
3437 */
3438 char *
3439did_set_foldlevel(optset_T *args UNUSED)
3440{
3441 if (curwin->w_p_fdl < 0)
3442 curwin->w_p_fdl = 0;
3443 newFoldLevel();
3444 return NULL;
3445}
3446
3447/*
3448 * Process the new 'foldminlines' option value.
3449 */
3450 char *
3451did_set_foldminlines(optset_T *args UNUSED)
3452{
3453 foldUpdateAll(curwin);
3454 return NULL;
3455}
3456
3457/*
3458 * Process the new 'foldnestmax' option value.
3459 */
3460 char *
3461did_set_foldnestmax(optset_T *args UNUSED)
3462{
3463 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3464 foldUpdateAll(curwin);
3465 return NULL;
3466}
3467#endif
3468
3469#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3470/*
3471 * Process the updated 'hlsearch' option value.
3472 */
3473 char *
3474did_set_hlsearch(optset_T *args UNUSED)
3475{
3476 // when 'hlsearch' is set or reset: reset no_hlsearch
3477 set_no_hlsearch(FALSE);
3478 return NULL;
3479}
3480#endif
3481
3482/*
3483 * Process the updated 'ignorecase' option value.
3484 */
3485 char *
3486did_set_ignorecase(optset_T *args UNUSED)
3487{
3488 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3489 if (p_hls)
3490 redraw_all_later(UPD_SOME_VALID);
3491 return NULL;
3492}
3493
3494#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3495/*
3496 * Process the updated 'imdisable' option value.
3497 */
3498 char *
3499did_set_imdisable(optset_T *args UNUSED)
3500{
3501 // Only de-activate it here, it will be enabled when changing mode.
3502 if (p_imdisable)
3503 im_set_active(FALSE);
3504 else if (State & MODE_INSERT)
3505 // When the option is set from an autocommand, it may need to take
3506 // effect right away.
3507 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3508 return NULL;
3509}
3510#endif
3511
3512/*
3513 * Process the new 'iminsert' option value.
3514 */
3515 char *
3516did_set_iminsert(optset_T *args UNUSED)
3517{
3518 char *errmsg = NULL;
3519
3520 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3521 {
3522 errmsg = e_invalid_argument;
3523 curbuf->b_p_iminsert = B_IMODE_NONE;
3524 }
3525 p_iminsert = curbuf->b_p_iminsert;
3526 if (termcap_active) // don't do this in the alternate screen
3527 showmode();
3528#if defined(FEAT_KEYMAP)
3529 // Show/unshow value of 'keymap' in status lines.
3530 status_redraw_curbuf();
3531#endif
3532
3533 return errmsg;
3534}
3535
3536/*
3537 * Process the new 'imsearch' option value.
3538 */
3539 char *
3540did_set_imsearch(optset_T *args UNUSED)
3541{
3542 char *errmsg = NULL;
3543
3544 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3545 {
3546 errmsg = e_invalid_argument;
3547 curbuf->b_p_imsearch = B_IMODE_NONE;
3548 }
3549 p_imsearch = curbuf->b_p_imsearch;
3550
3551 return errmsg;
3552}
3553
3554#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3555/*
3556 * Process the new 'imstyle' option value.
3557 */
3558 char *
3559did_set_imstyle(optset_T *args UNUSED)
3560{
3561 char *errmsg = NULL;
3562
3563 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3564 errmsg = e_invalid_argument;
3565
3566 return errmsg;
3567}
3568#endif
3569
3570/*
3571 * Process the updated 'insertmode' option value.
3572 */
3573 char *
3574did_set_insertmode(optset_T *args)
3575{
3576 // when 'insertmode' is set from an autocommand need to do work here
3577 if (p_im)
3578 {
3579 if ((State & MODE_INSERT) == 0)
3580 need_start_insertmode = TRUE;
3581 stop_insert_mode = FALSE;
3582 }
3583 // only reset if it was set previously
3584 else if (args->os_oldval.boolean)
3585 {
3586 need_start_insertmode = FALSE;
3587 stop_insert_mode = TRUE;
3588 if (restart_edit != 0 && mode_displayed)
3589 clear_cmdline = TRUE; // remove "(insert)"
3590 restart_edit = 0;
3591 }
3592
3593 return NULL;
3594}
3595
3596#if defined(FEAT_LANGMAP) || defined(PROTO)
3597/*
3598 * Process the updated 'langnoremap' option value.
3599 */
3600 char *
3601did_set_langnoremap(optset_T *args UNUSED)
3602{
3603 // 'langnoremap' -> !'langremap'
3604 p_lrm = !p_lnr;
3605 return NULL;
3606}
3607
3608/*
3609 * Process the updated 'langremap' option value.
3610 */
3611 char *
3612did_set_langremap(optset_T *args UNUSED)
3613{
3614 // 'langremap' -> !'langnoremap'
3615 p_lnr = !p_lrm;
3616 return NULL;
3617}
3618#endif
3619
3620/*
3621 * Process the new 'laststatus' option value.
3622 */
3623 char *
3624did_set_laststatus(optset_T *args UNUSED)
3625{
3626 last_status(FALSE); // (re)set last window status line
3627 return NULL;
3628}
3629
3630#if defined(FEAT_GUI) || defined(PROTO)
3631/*
3632 * Process the new 'linespace' option value.
3633 */
3634 char *
3635did_set_linespace(optset_T *args UNUSED)
3636{
3637 // Recompute gui.char_height and resize the Vim window to keep the
3638 // same number of lines.
3639 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3640 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3641 return NULL;
3642}
3643#endif
3644
3645/*
3646 * Process the updated 'lisp' option value.
3647 */
3648 char *
3649did_set_lisp(optset_T *args UNUSED)
3650{
3651 // When 'lisp' option changes include/exclude '-' in keyword characters.
3652 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3653 return NULL;
3654}
3655
3656/*
3657 * Process the new 'maxcombine' option value.
3658 */
3659 char *
3660did_set_maxcombine(optset_T *args UNUSED)
3661{
3662 if (p_mco > MAX_MCO)
3663 p_mco = MAX_MCO;
3664 else if (p_mco < 0)
3665 p_mco = 0;
3666 screenclear(); // will re-allocate the screen
3667 return NULL;
3668}
3669
3670/*
3671 * Process the updated 'modifiable' option value.
3672 */
3673 char *
3674did_set_modifiable(optset_T *args UNUSED)
3675{
3676 // when 'modifiable' is changed, redraw the window title
3677
3678# ifdef FEAT_TERMINAL
3679 // Cannot set 'modifiable' when in Terminal mode.
3680 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3681 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3682 {
3683 curbuf->b_p_ma = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003684 return e_cannot_make_terminal_with_running_job_modifiable;
3685 }
3686# endif
3687 redraw_titles();
3688
3689 return NULL;
3690}
3691
3692/*
3693 * Process the updated 'modified' option value.
3694 */
3695 char *
3696did_set_modified(optset_T *args)
3697{
3698 if (!args->os_newval.boolean)
3699 save_file_ff(curbuf); // Buffer is unchanged
3700 redraw_titles();
3701 modified_was_set = args->os_newval.boolean;
3702 return NULL;
3703}
3704
3705#if defined(FEAT_GUI) || defined(PROTO)
3706/*
3707 * Process the updated 'mousehide' option value.
3708 */
3709 char *
3710did_set_mousehide(optset_T *args UNUSED)
3711{
3712 if (!p_mh)
3713 gui_mch_mousehide(FALSE);
3714 return NULL;
3715}
3716#endif
3717
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003718/*
3719 * Process the updated 'number' or 'relativenumber' option value.
3720 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003721 char *
3722did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003723{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003724#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003725 if (gui.in_use
3726 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3727 && curbuf->b_signlist != NULL)
3728 {
3729 // If the 'number' or 'relativenumber' options are modified and
3730 // 'signcolumn' is set to 'number', then clear the screen for a full
3731 // refresh. Otherwise the sign icons are not displayed properly in the
3732 // number column. If the 'number' option is set and only the
3733 // 'relativenumber' option is toggled, then don't refresh the screen
3734 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003735 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003736 redraw_all_later(UPD_CLEAR);
3737 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003738#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003739 return NULL;
3740}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003741
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003742#if defined(FEAT_LINEBREAK) || defined(PROTO)
3743/*
3744 * Process the new 'numberwidth' option value.
3745 */
3746 char *
3747did_set_numberwidth(optset_T *args UNUSED)
3748{
3749 char *errmsg = NULL;
3750
3751 // 'numberwidth' must be positive
3752 if (curwin->w_p_nuw < 1)
3753 {
3754 errmsg = e_argument_must_be_positive;
3755 curwin->w_p_nuw = 1;
3756 }
3757 if (curwin->w_p_nuw > 20)
3758 {
3759 errmsg = e_invalid_argument;
3760 curwin->w_p_nuw = 20;
3761 }
3762 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3763
3764 return errmsg;
3765}
3766#endif
3767
3768/*
3769 * Process the updated 'paste' option value. Called after p_paste was set or
3770 * reset. When 'paste' is set or reset also change other options.
3771 */
3772 char *
3773did_set_paste(optset_T *args UNUSED)
3774{
3775 static int old_p_paste = FALSE;
3776 static int save_sm = 0;
3777 static int save_sta = 0;
3778 static int save_ru = 0;
3779#ifdef FEAT_RIGHTLEFT
3780 static int save_ri = 0;
3781 static int save_hkmap = 0;
3782#endif
3783 buf_T *buf;
3784
3785 if (p_paste)
3786 {
3787 // Paste switched from off to on.
3788 // Save the current values, so they can be restored later.
3789 if (!old_p_paste)
3790 {
3791 // save options for each buffer
3792 FOR_ALL_BUFFERS(buf)
3793 {
3794 buf->b_p_tw_nopaste = buf->b_p_tw;
3795 buf->b_p_wm_nopaste = buf->b_p_wm;
3796 buf->b_p_sts_nopaste = buf->b_p_sts;
3797 buf->b_p_ai_nopaste = buf->b_p_ai;
3798 buf->b_p_et_nopaste = buf->b_p_et;
3799#ifdef FEAT_VARTABS
3800 if (buf->b_p_vsts_nopaste)
3801 vim_free(buf->b_p_vsts_nopaste);
3802 buf->b_p_vsts_nopaste =
3803 buf->b_p_vsts && buf->b_p_vsts != empty_option
3804 ? vim_strsave(buf->b_p_vsts) : NULL;
3805#endif
3806 }
3807
3808 // save global options
3809 save_sm = p_sm;
3810 save_sta = p_sta;
3811 save_ru = p_ru;
3812#ifdef FEAT_RIGHTLEFT
3813 save_ri = p_ri;
3814 save_hkmap = p_hkmap;
3815#endif
3816 // save global values for local buffer options
3817 p_ai_nopaste = p_ai;
3818 p_et_nopaste = p_et;
3819 p_sts_nopaste = p_sts;
3820 p_tw_nopaste = p_tw;
3821 p_wm_nopaste = p_wm;
3822#ifdef FEAT_VARTABS
3823 if (p_vsts_nopaste)
3824 vim_free(p_vsts_nopaste);
3825 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3826 ? vim_strsave(p_vsts) : NULL;
3827#endif
3828 }
3829
3830 // Always set the option values, also when 'paste' is set when it is
3831 // already on. Set options for each buffer.
3832 FOR_ALL_BUFFERS(buf)
3833 {
3834 buf->b_p_tw = 0; // textwidth is 0
3835 buf->b_p_wm = 0; // wrapmargin is 0
3836 buf->b_p_sts = 0; // softtabstop is 0
3837 buf->b_p_ai = 0; // no auto-indent
3838 buf->b_p_et = 0; // no expandtab
3839#ifdef FEAT_VARTABS
3840 if (buf->b_p_vsts)
3841 free_string_option(buf->b_p_vsts);
3842 buf->b_p_vsts = empty_option;
3843 VIM_CLEAR(buf->b_p_vsts_array);
3844#endif
3845 }
3846
3847 // set global options
3848 p_sm = 0; // no showmatch
3849 p_sta = 0; // no smarttab
3850 if (p_ru)
3851 status_redraw_all(); // redraw to remove the ruler
3852 p_ru = 0; // no ruler
3853#ifdef FEAT_RIGHTLEFT
3854 p_ri = 0; // no reverse insert
3855 p_hkmap = 0; // no Hebrew keyboard
3856#endif
3857 // set global values for local buffer options
3858 p_tw = 0;
3859 p_wm = 0;
3860 p_sts = 0;
3861 p_ai = 0;
Christian Brabandt757593c2023-08-22 21:44:10 +02003862 p_et = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003863#ifdef FEAT_VARTABS
3864 if (p_vsts)
3865 free_string_option(p_vsts);
3866 p_vsts = empty_option;
3867#endif
3868 }
3869
3870 // Paste switched from on to off: Restore saved values.
3871 else if (old_p_paste)
3872 {
3873 // restore options for each buffer
3874 FOR_ALL_BUFFERS(buf)
3875 {
3876 buf->b_p_tw = buf->b_p_tw_nopaste;
3877 buf->b_p_wm = buf->b_p_wm_nopaste;
3878 buf->b_p_sts = buf->b_p_sts_nopaste;
3879 buf->b_p_ai = buf->b_p_ai_nopaste;
3880 buf->b_p_et = buf->b_p_et_nopaste;
3881#ifdef FEAT_VARTABS
3882 if (buf->b_p_vsts)
3883 free_string_option(buf->b_p_vsts);
3884 buf->b_p_vsts = buf->b_p_vsts_nopaste
3885 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3886 vim_free(buf->b_p_vsts_array);
3887 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3888 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3889 else
3890 buf->b_p_vsts_array = NULL;
3891#endif
3892 }
3893
3894 // restore global options
3895 p_sm = save_sm;
3896 p_sta = save_sta;
3897 if (p_ru != save_ru)
3898 status_redraw_all(); // redraw to draw the ruler
3899 p_ru = save_ru;
3900#ifdef FEAT_RIGHTLEFT
3901 p_ri = save_ri;
3902 p_hkmap = save_hkmap;
3903#endif
3904 // set global values for local buffer options
3905 p_ai = p_ai_nopaste;
3906 p_et = p_et_nopaste;
3907 p_sts = p_sts_nopaste;
3908 p_tw = p_tw_nopaste;
3909 p_wm = p_wm_nopaste;
3910#ifdef FEAT_VARTABS
3911 if (p_vsts)
3912 free_string_option(p_vsts);
3913 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
3914#endif
3915 }
3916
3917 old_p_paste = p_paste;
3918
Christian Brabandt757593c2023-08-22 21:44:10 +02003919#if defined(FEAT_EVAL) || defined(PROTO)
3920 // Remember where the dependent options were reset
3921 didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
3922#endif
3923
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003924 return NULL;
3925}
3926
3927
3928#ifdef FEAT_QUICKFIX
3929/*
3930 * Process the updated 'previewwindow' option value.
3931 */
3932 char *
Yegappan Lakshmanan3ee25962023-12-04 20:31:14 +01003933did_set_previewwindow(optset_T *args UNUSED)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003934{
3935 if (!curwin->w_p_pvw)
3936 return NULL;
3937
3938 // There can be only one window with 'previewwindow' set.
3939 win_T *win;
3940
3941 FOR_ALL_WINDOWS(win)
3942 if (win->w_p_pvw && win != curwin)
3943 {
3944 curwin->w_p_pvw = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003945 return e_preview_window_already_exists;
3946 }
3947
3948 return NULL;
3949}
3950#endif
3951
3952#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
3953/*
3954 * Process the new 'pyxversion' option value.
3955 */
3956 char *
3957did_set_pyxversion(optset_T *args UNUSED)
3958{
3959 char *errmsg = NULL;
3960
3961 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3962 errmsg = e_invalid_argument;
3963
3964 return errmsg;
3965}
3966#endif
3967
3968/*
3969 * Process the updated 'readonly' option value.
3970 */
3971 char *
3972did_set_readonly(optset_T *args)
3973{
3974 // when 'readonly' is reset globally, also reset readonlymode
3975 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
3976 readonlymode = FALSE;
3977
3978 // when 'readonly' is set may give W10 again
3979 if (curbuf->b_p_ro)
3980 curbuf->b_did_warn = FALSE;
3981
3982 redraw_titles();
3983
3984 return NULL;
3985}
3986
3987/*
3988 * Process the updated 'scrollbind' option value.
3989 */
3990 char *
3991did_set_scrollbind(optset_T *args UNUSED)
3992{
3993 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3994 // at the end of normal_cmd()
3995 if (!curwin->w_p_scb)
3996 return NULL;
3997
3998 do_check_scrollbind(FALSE);
3999 curwin->w_scbind_pos = curwin->w_topline;
4000 return NULL;
4001}
4002
4003#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
4004/*
4005 * Process the updated 'shellslash' option value.
4006 */
4007 char *
4008did_set_shellslash(optset_T *args UNUSED)
4009{
4010 if (p_ssl)
4011 {
4012 psepc = '/';
4013 psepcN = '\\';
4014 pseps[0] = '/';
4015 }
4016 else
4017 {
4018 psepc = '\\';
4019 psepcN = '/';
4020 pseps[0] = '\\';
4021 }
4022
4023 // need to adjust the file name arguments and buffer names.
4024 buflist_slash_adjust();
4025 alist_slash_adjust();
4026# ifdef FEAT_EVAL
4027 scriptnames_slash_adjust();
4028# endif
4029 return NULL;
4030}
4031#endif
4032
4033/*
4034 * Process the new 'shiftwidth' or the 'tabstop' option value.
4035 */
4036 char *
4037did_set_shiftwidth_tabstop(optset_T *args)
4038{
4039 long *pp = (long *)args->os_varp;
4040 char *errmsg = NULL;
4041
4042 if (curbuf->b_p_sw < 0)
4043 {
4044 errmsg = e_argument_must_be_positive;
4045#ifdef FEAT_VARTABS
4046 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4047 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4048 ? tabstop_first(curbuf->b_p_vts_array)
4049 : curbuf->b_p_ts;
4050#else
4051 curbuf->b_p_sw = curbuf->b_p_ts;
4052#endif
4053 }
4054
4055#ifdef FEAT_FOLDING
4056 if (foldmethodIsIndent(curwin))
4057 foldUpdateAll(curwin);
4058#endif
4059 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4060 // parse 'cinoptions'.
4061 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4062 parse_cino(curbuf);
4063
4064 return errmsg;
4065}
4066
4067/*
4068 * Process the new 'showtabline' option value.
4069 */
4070 char *
4071did_set_showtabline(optset_T *args UNUSED)
4072{
4073 shell_new_rows(); // recompute window positions and heights
4074 return NULL;
4075}
4076
4077/*
4078 * Process the updated 'smoothscroll' option value.
4079 */
4080 char *
4081did_set_smoothscroll(optset_T *args UNUSED)
4082{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004083 if (!curwin->w_p_sms)
4084 curwin->w_skipcol = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004085
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004086 return NULL;
4087}
4088
4089#if defined(FEAT_SPELL) || defined(PROTO)
4090/*
4091 * Process the updated 'spell' option value.
4092 */
4093 char *
4094did_set_spell(optset_T *args UNUSED)
4095{
4096 if (curwin->w_p_spell)
4097 return parse_spelllang(curwin);
4098
4099 return NULL;
4100}
4101#endif
4102
4103/*
4104 * Process the updated 'swapfile' option value.
4105 */
4106 char *
4107did_set_swapfile(optset_T *args UNUSED)
4108{
4109 // when 'swf' is set, create swapfile, when reset remove swapfile
4110 if (curbuf->b_p_swf && p_uc)
4111 ml_open_file(curbuf); // create the swap file
4112 else
4113 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4114 // buf->b_p_swf
4115 mf_close_file(curbuf, TRUE); // remove the swap file
4116 return NULL;
4117}
4118
4119#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004120 char *
4121did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004122{
4123# ifdef FEAT_VTP
4124 // Do not turn on 'tgc' when 24-bit colors are not supported.
4125 if (
4126# ifdef VIMDLL
4127 !gui.in_use && !gui.starting &&
4128# endif
4129 !has_vtp_working())
4130 {
4131 p_tgc = 0;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004132 return e_24_bit_colors_are_not_supported_on_this_environment;
4133 }
4134 if (is_term_win32())
4135 swap_tcap();
4136# endif
4137# ifdef FEAT_GUI
4138 if (!gui.in_use && !gui.starting)
4139# endif
4140 highlight_gui_started();
4141# ifdef FEAT_VTP
4142 // reset t_Co
4143 if (is_term_win32())
4144 {
4145 control_console_color_rgb();
4146 set_termname(T_NAME);
4147 init_highlight(TRUE, FALSE);
4148 }
4149# endif
4150# ifdef FEAT_TERMINAL
4151 term_update_colors_all();
4152 term_update_palette_all();
4153 term_update_wincolor_all();
4154# endif
4155
4156 return NULL;
4157}
4158#endif
4159
4160/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004161 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004163 char *
4164did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004166 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004168 // when 'terse' is set change 'shortmess'
4169 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004171 // insert 's' in p_shm
4172 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004173 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004174 STRCPY(IObuff, p_shm);
4175 STRCAT(IObuff, "s");
4176 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004177 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004178 // remove 's' from p_shm
4179 else if (!p_terse && p != NULL)
4180 STRMOVE(p, p + 1);
4181 return NULL;
4182}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004184/*
4185 * Process the updated 'textauto' option value.
4186 */
4187 char *
4188did_set_textauto(optset_T *args)
4189{
4190 // when 'textauto' is set or reset also change 'fileformats'
4191 set_string_option_direct((char_u *)"ffs", -1,
4192 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4193 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004194
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004195 return NULL;
4196}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004198/*
4199 * Process the updated 'textmode' option value.
4200 */
4201 char *
4202did_set_textmode(optset_T *args)
4203{
4204 // when 'textmode' is set or reset also change 'fileformat'
4205 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4206
4207 return NULL;
4208}
4209
4210/*
4211 * Process the new 'textwidth' option value.
4212 */
4213 char *
4214did_set_textwidth(optset_T *args UNUSED)
4215{
4216 char *errmsg = NULL;
4217
4218 if (curbuf->b_p_tw < 0)
4219 {
4220 errmsg = e_argument_must_be_positive;
4221 curbuf->b_p_tw = 0;
4222 }
4223#ifdef FEAT_SYN_HL
4224 {
4225 win_T *wp;
4226 tabpage_T *tp;
4227
4228 FOR_ALL_TAB_WINDOWS(tp, wp)
4229 check_colorcolumn(wp);
4230 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004231#endif
4232
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004233 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234}
4235
4236/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004237 * Process the updated 'title' or the 'icon' option value.
4238 */
4239 char *
4240did_set_title_icon(optset_T *args UNUSED)
4241{
4242 // when 'title' changed, may need to change the title; same for 'icon'
4243 did_set_title();
4244 return NULL;
4245}
4246
4247/*
4248 * Process the new 'titlelen' option value.
4249 */
4250 char *
4251did_set_titlelen(optset_T *args)
4252{
4253 long old_value = args->os_oldval.number;
4254 char *errmsg = NULL;
4255
4256 // if 'titlelen' has changed, redraw the title
4257 if (p_titlelen < 0)
4258 {
4259 errmsg = e_argument_must_be_positive;
4260 p_titlelen = 85;
4261 }
4262 if (starting != NO_SCREEN && old_value != p_titlelen)
4263 need_maketitle = TRUE;
4264
4265 return errmsg;
4266}
4267
4268#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4269/*
4270 * Process the updated 'undofile' option value.
4271 */
4272 char *
4273did_set_undofile(optset_T *args)
4274{
4275 // Only take action when the option was set.
4276 if (!curbuf->b_p_udf && !p_udf)
4277 return NULL;
4278
4279 // When reset we do not delete the undo file, the option may be set again
4280 // without making any changes in between.
4281 char_u hash[UNDO_HASH_SIZE];
4282 buf_T *save_curbuf = curbuf;
4283
4284 FOR_ALL_BUFFERS(curbuf)
4285 {
4286 // When 'undofile' is set globally: for every buffer, otherwise
4287 // only for the current buffer: Try to read in the undofile,
4288 // if one exists, the buffer wasn't changed and the buffer was
4289 // loaded
4290 if ((curbuf == save_curbuf
4291 || (args->os_flags & OPT_GLOBAL)
4292 || args->os_flags == 0)
4293 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4294 {
4295#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004296 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004297 continue;
4298#endif
4299 u_compute_hash(hash);
4300 u_read_undo(NULL, hash, curbuf->b_fname);
4301 }
4302 }
4303 curbuf = save_curbuf;
4304
4305 return NULL;
4306}
4307#endif
4308
4309/*
4310 * Process the new global 'undolevels' option value.
4311 */
4312 static void
4313update_global_undolevels(long value, long old_value)
4314{
4315 // sync undo before 'undolevels' changes
4316
4317 // use the old value, otherwise u_sync() may not work properly
4318 p_ul = old_value;
4319 u_sync(TRUE);
4320 p_ul = value;
4321}
4322
4323/*
4324 * Process the new buffer local 'undolevels' option value.
4325 */
4326 static void
4327update_buflocal_undolevels(long value, long old_value)
4328{
4329 // use the old value, otherwise u_sync() may not work properly
4330 curbuf->b_p_ul = old_value;
4331 u_sync(TRUE);
4332 curbuf->b_p_ul = value;
4333}
4334
4335/*
4336 * Process the new 'undolevels' option value.
4337 */
4338 char *
4339did_set_undolevels(optset_T *args)
4340{
4341 long *pp = (long *)args->os_varp;
4342
4343 if (pp == &p_ul) // global 'undolevels'
4344 update_global_undolevels(args->os_newval.number,
4345 args->os_oldval.number);
4346 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4347 update_buflocal_undolevels(args->os_newval.number,
4348 args->os_oldval.number);
4349
4350 return NULL;
4351}
4352
4353/*
4354 * Process the new 'updatecount' option value.
4355 */
4356 char *
4357did_set_updatecount(optset_T *args)
4358{
4359 long old_value = args->os_oldval.number;
4360 char *errmsg = NULL;
4361
4362 // when 'updatecount' changes from zero to non-zero, open swap files
4363 if (p_uc < 0)
4364 {
4365 errmsg = e_argument_must_be_positive;
4366 p_uc = 100;
4367 }
4368 if (p_uc && !old_value)
4369 ml_open_files();
4370
4371 return errmsg;
4372}
4373
4374/*
4375 * Process the updated 'weirdinvert' option value.
4376 */
4377 char *
4378did_set_weirdinvert(optset_T *args)
4379{
4380 // When 'weirdinvert' changed, set/reset 't_xs'.
4381 // Then set 'weirdinvert' according to value of 't_xs'.
4382 if (p_wiv && !args->os_oldval.boolean)
4383 T_XS = (char_u *)"y";
4384 else if (!p_wiv && args->os_oldval.boolean)
4385 T_XS = empty_option;
4386 p_wiv = (*T_XS != NUL);
4387
4388 return NULL;
4389}
4390
4391/*
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02004392 * Process the new 'wildchar' / 'wildcharm' option value.
4393 */
4394 char *
4395did_set_wildchar(optset_T *args)
4396{
4397 long c = *(long *)args->os_varp;
4398
4399 // Don't allow key values that wouldn't work as wildchar.
4400 if (c == Ctrl_C || c == '\n' || c == '\r' || c == K_KENTER)
4401 return e_invalid_argument;
4402
4403 return NULL;
4404}
4405
4406/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004407 * Process the new 'window' option value.
4408 */
4409 char *
4410did_set_window(optset_T *args UNUSED)
4411{
4412 if (p_window < 1)
4413 p_window = 1;
4414 else if (p_window >= Rows)
4415 p_window = Rows - 1;
4416 return NULL;
4417}
4418
4419/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004420 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004422 char *
4423did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004425 long *pp = (long *)args->os_varp;
4426 char *errmsg = NULL;
4427
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004428 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004430 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004431 p_wh = 1;
4432 }
4433 if (p_wmh > p_wh)
4434 {
4435 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4436 p_wh = p_wmh;
4437 }
4438 if (p_hh < 0)
4439 {
4440 errmsg = e_argument_must_be_positive;
4441 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004444 // Change window height NOW
4445 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004447 if (pp == &p_wh && curwin->w_height < p_wh)
4448 win_setheight((int)p_wh);
4449 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4450 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004453 return errmsg;
4454}
4455
4456/*
4457 * Process the new 'winminheight' option value.
4458 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004459 char *
4460did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004461{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004462 char *errmsg = NULL;
4463
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004464 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004466 errmsg = e_argument_must_be_positive;
4467 p_wmh = 0;
4468 }
4469 if (p_wmh > p_wh)
4470 {
4471 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4472 p_wmh = p_wh;
4473 }
4474 win_setminheight();
4475
4476 return errmsg;
4477}
4478
4479/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004480 * Process the new 'winminwidth' option value.
4481 */
4482 char *
4483did_set_winminwidth(optset_T *args UNUSED)
4484{
4485 char *errmsg = NULL;
4486
4487 if (p_wmw < 0)
4488 {
4489 errmsg = e_argument_must_be_positive;
4490 p_wmw = 0;
4491 }
4492 if (p_wmw > p_wiw)
4493 {
4494 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4495 p_wmw = p_wiw;
4496 }
4497 win_setminwidth();
4498
4499 return errmsg;
4500}
4501
4502/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004503 * Process the new 'winwidth' option value.
4504 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004505 char *
4506did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004507{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004508 char *errmsg = NULL;
4509
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004510 if (p_wiw < 1)
4511 {
4512 errmsg = e_argument_must_be_positive;
4513 p_wiw = 1;
4514 }
4515 if (p_wmw > p_wiw)
4516 {
4517 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4518 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004520
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004521 // Change window width NOW
4522 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4523 win_setwidth((int)p_wiw);
4524
4525 return errmsg;
4526}
4527
4528/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004529 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004530 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004531 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004532did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004533{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004534 // Set w_leftcol or w_skipcol to zero.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004535 if (curwin->w_p_wrap)
4536 curwin->w_leftcol = 0;
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004537 else
4538 curwin->w_skipcol = 0;
4539
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004540 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004541}
4542
4543/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004544 * Set the value of a boolean option, and take care of side effects.
4545 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004546 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004547 static char *
4548set_bool_option(
4549 int opt_idx, // index in options[] table
4550 char_u *varp, // pointer to the option variable
4551 int value, // new value
4552 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004553{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004554 int old_value = *(int *)varp;
4555#if defined(FEAT_EVAL)
4556 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004558 char *errmsg = NULL;
4559
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004560 // Disallow changing some options from secure mode
4561 if ((secure
4562#ifdef HAVE_SANDBOX
4563 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004564#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004565 ) && (options[opt_idx].flags & P_SECURE))
4566 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004567
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004568#if defined(FEAT_EVAL)
4569 // Save the global value before changing anything. This is needed as for
4570 // a global-only option setting the "local value" in fact sets the global
4571 // value (since there is only one value).
4572 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4573 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4574 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004576
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004577 *(int *)varp = value; // set the new value
4578#ifdef FEAT_EVAL
4579 // Remember where the option was set.
4580 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004581#endif
4582
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004584 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004587 // May set global value for local option.
4588 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4589 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004590
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004591 // Handle side effects of changing a bool option.
4592 if (options[opt_idx].opt_did_set_cb != NULL)
4593 {
4594 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004595
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004596 CLEAR_FIELD(args);
4597 args.os_varp = varp;
4598 args.os_flags = opt_flags;
4599 args.os_oldval.boolean = old_value;
4600 args.os_newval.boolean = value;
4601 args.os_errbuf = NULL;
4602 errmsg = options[opt_idx].opt_did_set_cb(&args);
zeertzjqdeba02d2023-11-02 21:01:19 +01004603 if (errmsg != NULL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004604 return errmsg;
4605 }
4606
4607 // after handling side effects, call autocommand
4608
4609 options[opt_idx].flags |= P_WAS_SET;
4610
4611#if defined(FEAT_EVAL)
4612 apply_optionset_autocmd(opt_idx, opt_flags,
4613 (long)(old_value ? TRUE : FALSE),
4614 (long)(old_global_value ? TRUE : FALSE),
4615 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004616#endif
4617
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004618 comp_col(); // in case 'ruler' or 'showcmd' changed
4619 if (curwin->w_curswant != MAXCOL
4620 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
4621 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004622
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004623 if ((opt_flags & OPT_NO_REDRAW) == 0)
4624 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004625
4626 return errmsg;
4627}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004628
4629/*
4630 * Check the bounds of numeric options.
4631 */
4632 static char *
4633check_num_option_bounds(
4634 long *pp,
4635 long old_value,
4636 long old_Rows,
4637 long old_Columns,
4638 char *errbuf,
4639 size_t errbuflen,
4640 char *errmsg)
4641{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 if (Rows < min_rows() && full_screen)
4643 {
4644 if (errbuf != NULL)
4645 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004646 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004647 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 errmsg = errbuf;
4649 }
4650 Rows = min_rows();
4651 }
4652 if (Columns < MIN_COLUMNS && full_screen)
4653 {
4654 if (errbuf != NULL)
4655 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004656 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004657 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 errmsg = errbuf;
4659 }
4660 Columns = MIN_COLUMNS;
4661 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004662 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004664 // If the screen (shell) height has been changed, assume it is the
4665 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 if (old_Rows != Rows || old_Columns != Columns)
4667 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004668 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (updating_screen)
4670 *pp = old_value;
4671 else if (full_screen
4672#ifdef FEAT_GUI
4673 && !gui.starting
4674#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004675 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 set_shellsize((int)Columns, (int)Rows, TRUE);
4677 else
4678 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004679 // Postpone the resizing; check the size and cmdline position for
4680 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 check_shellsize();
4682 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4683 cmdline_row = Rows - p_ch;
4684 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004685 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004686 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
4688
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 if (curbuf->b_p_ts <= 0)
4690 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004691 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 curbuf->b_p_ts = 8;
4693 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004694 else if (curbuf->b_p_ts > TABSTOP_MAX)
4695 {
4696 errmsg = e_invalid_argument;
4697 curbuf->b_p_ts = 8;
4698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 if (p_tm < 0)
4700 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004701 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 p_tm = 0;
4703 }
4704 if ((curwin->w_p_scr <= 0
4705 || (curwin->w_p_scr > curwin->w_height
4706 && curwin->w_height > 0))
4707 && full_screen)
4708 {
4709 if (pp == &(curwin->w_p_scr))
4710 {
4711 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004712 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 win_comp_scroll(curwin);
4714 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004715 // If 'scroll' became invalid because of a side effect silently adjust
4716 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 else if (curwin->w_p_scr <= 0)
4718 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004719 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 curwin->w_p_scr = curwin->w_height;
4721 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004722 if (p_hi < 0)
4723 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004724 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004725 p_hi = 0;
4726 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004727 else if (p_hi > 10000)
4728 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004729 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004730 p_hi = 10000;
4731 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004732 if (p_re < 0 || p_re > 2)
4733 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004734 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004735 p_re = 0;
4736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 if (p_report < 0)
4738 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004739 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 p_report = 1;
4741 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004742 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004744 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 p_sj = Rows / 2;
4746 else
4747 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004748 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 p_sj = 1;
4750 }
4751 }
4752 if (p_so < 0 && full_screen)
4753 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004754 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 p_so = 0;
4756 }
4757 if (p_siso < 0 && full_screen)
4758 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004759 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 p_siso = 0;
4761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 if (p_cwh < 1)
4763 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004764 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 p_cwh = 1;
4766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (p_ut < 0)
4768 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004769 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 p_ut = 2000;
4771 }
4772 if (p_ss < 0)
4773 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004774 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 p_ss = 0;
4776 }
4777
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004778 return errmsg;
4779}
4780
4781/*
4782 * Set the value of a number option, and take care of side effects.
4783 * Returns NULL for success, or an error message for an error.
4784 */
4785 static char *
4786set_num_option(
4787 int opt_idx, // index in options[] table
4788 char_u *varp, // pointer to the option variable
4789 long value, // new value
4790 char *errbuf, // buffer for error messages
4791 size_t errbuflen, // length of "errbuf"
4792 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4793 // OPT_MODELINE, etc.
4794{
4795 char *errmsg = NULL;
4796 long old_value = *(long *)varp;
4797#if defined(FEAT_EVAL)
4798 long old_global_value = 0; // only used when setting a local and
4799 // global option
4800#endif
4801 long old_Rows = Rows; // remember old Rows
4802 long old_Columns = Columns; // remember old Columns
4803 long *pp = (long *)varp;
4804
4805 // Disallow changing some options from secure mode.
4806 if ((secure
4807#ifdef HAVE_SANDBOX
4808 || sandbox != 0
4809#endif
4810 ) && (options[opt_idx].flags & P_SECURE))
4811 return e_not_allowed_here;
4812
4813#if defined(FEAT_EVAL)
4814 // Save the global value before changing anything. This is needed as for
4815 // a global-only option setting the "local value" in fact sets the global
4816 // value (since there is only one value).
4817 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4818 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4819 OPT_GLOBAL);
4820#endif
4821
4822 *pp = value;
4823#ifdef FEAT_EVAL
4824 // Remember where the option was set.
4825 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4826#endif
4827#ifdef FEAT_GUI
4828 need_mouse_correct = TRUE;
4829#endif
4830
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004831 // Invoke the option specific callback function to validate and apply the
4832 // new value.
4833 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004834 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004835 optset_T args;
4836
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004837 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004838 args.os_varp = varp;
4839 args.os_flags = opt_flags;
4840 args.os_oldval.number = old_value;
4841 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004842 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004843 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004844 }
4845
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004846 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004847 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4848 errbuf, errbuflen, errmsg);
4849
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004850 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4852 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4853
4854 options[opt_idx].flags |= P_WAS_SET;
4855
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004856#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004857 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4858 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004859#endif
4860
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004861 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004862 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004863 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004864 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004865 if ((opt_flags & OPT_NO_REDRAW) == 0)
4866 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
4868 return errmsg;
4869}
4870
4871/*
4872 * Called after an option changed: check if something needs to be redrawn.
4873 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004874 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004875check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004877 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004878 int doclear = (flags & P_RCLR) == P_RCLR;
4879 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004881 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4885 changed_window_setting();
4886 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004887 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004888 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004889 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004890 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004891 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004893 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894}
4895
4896/*
4897 * Find index for option 'arg'.
4898 * Return -1 if not found.
4899 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004900 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004901findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
4903 int opt_idx;
4904 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004905 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 int is_term_opt;
4907
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004908 // For first call: Initialize the quick-access table.
4909 // It contains the index for the first option that starts with a certain
4910 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 if (quick_tab[1] == 0)
4912 {
4913 p = options[0].fullname;
4914 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4915 {
4916 if (s[0] != p[0])
4917 {
4918 if (s[0] == 't' && s[1] == '_')
4919 quick_tab[26] = opt_idx;
4920 else
4921 quick_tab[CharOrdLow(s[0])] = opt_idx;
4922 }
4923 p = s;
4924 }
4925 }
4926
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004927 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 return -1;
4930
4931 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4932 if (is_term_opt)
4933 opt_idx = quick_tab[26];
4934 else
4935 opt_idx = quick_tab[CharOrdLow(arg[0])];
zeertzjqf48558e2023-12-08 21:34:31 +01004936 for (; (s = options[opt_idx].fullname) != NULL && s[0] == arg[0]; opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004938 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 break;
4940 }
zeertzjqf48558e2023-12-08 21:34:31 +01004941 if (s != NULL && s[0] != arg[0])
4942 s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 if (s == NULL && !is_term_opt)
4944 {
4945 opt_idx = quick_tab[CharOrdLow(arg[0])];
4946 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4947 {
4948 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004949 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 break;
4951 s = NULL;
4952 }
4953 }
4954 if (s == NULL)
4955 opt_idx = -1;
4956 return opt_idx;
4957}
4958
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004959#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4960 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961/*
4962 * Get the value for an option.
4963 *
4964 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004965 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004966 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004967 * String option: gov_string, *stringval gets allocated string.
4968 * Hidden Number option: gov_hidden_number.
4969 * Hidden Toggle option: gov_hidden_bool.
4970 * Hidden String option: gov_hidden_string.
4971 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004972 *
4973 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004975 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004976get_option_value(
4977 char_u *name,
4978 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004979 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004980 int *flagsp,
4981 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982{
4983 int opt_idx;
4984 char_u *varp;
4985
4986 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004987 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004988 {
4989 int key;
4990
4991 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004992 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004993 {
4994 char_u key_name[2];
4995 char_u *p;
4996
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004997 if (flagsp != NULL)
4998 *flagsp = 0; // terminal option has no flags
4999
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005000 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01005001 if (key < 0)
5002 {
5003 key_name[0] = KEY2TERMCAP0(key);
5004 key_name[1] = KEY2TERMCAP1(key);
5005 }
5006 else
5007 {
5008 key_name[0] = KS_KEY;
5009 key_name[1] = (key & 0xff);
5010 }
5011 p = find_termcode(key_name);
5012 if (p != NULL)
5013 {
5014 if (stringval != NULL)
5015 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005016 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01005017 }
5018 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005019 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01005020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005022 varp = get_varp_scope(&(options[opt_idx]), scope);
5023
5024 if (flagsp != NULL)
5025 // Return the P_xxxx option flags.
5026 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027
5028 if (options[opt_idx].flags & P_STRING)
5029 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005030 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005031 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if (stringval != NULL)
5033 {
zeertzjq51f0bc32022-05-09 13:33:39 +01005034 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005035 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5036 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005038 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005039 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005040 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005043 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 *stringval = vim_strsave(*(char_u **)(varp));
5045 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005046 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
5048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005049 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005050 return (options[opt_idx].flags & P_NUM)
5051 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (options[opt_idx].flags & P_NUM)
5053 *numval = *(long *)varp;
5054 else
5055 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005056 // Special case: 'modified' is b_changed, but we also want to consider
5057 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 if ((int *)varp == &curbuf->b_changed)
5059 *numval = curbufIsChanged();
5060 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005061 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005063 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064}
5065#endif
5066
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005067#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005068/*
5069 * Returns the option attributes and its value. Unlike the above function it
5070 * will return either global value or local value of the option depending on
5071 * what was requested, but it will never return global value if it was
5072 * requested to return local one and vice versa. Neither it will return
5073 * buffer-local value if it was requested to return window-local one.
5074 *
5075 * Pretends that option is absent if it is not present in the requested scope
5076 * (i.e. has no global, window-local or buffer-local value depending on
5077 * opt_type). Uses
5078 *
5079 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005080 * 0 hidden or unknown option, also option that does not have requested
5081 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005082 * see SOPT_* in vim.h for other flags
5083 *
5084 * Possible opt_type values: see SREQ_* in vim.h
5085 */
5086 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005087get_option_value_strict(
5088 char_u *name,
5089 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005090 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005091 int opt_type,
5092 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005093{
5094 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005095 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005096 struct vimoption *p;
5097 int r = 0;
5098
5099 opt_idx = findoption(name);
5100 if (opt_idx < 0)
5101 return 0;
5102
5103 p = &(options[opt_idx]);
5104
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005105 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005106 if (p->var == NULL)
5107 return 0;
5108
5109 if (p->flags & P_BOOL)
5110 r |= SOPT_BOOL;
5111 else if (p->flags & P_NUM)
5112 r |= SOPT_NUM;
5113 else if (p->flags & P_STRING)
5114 r |= SOPT_STRING;
5115
5116 if (p->indir == PV_NONE)
5117 {
5118 if (opt_type == SREQ_GLOBAL)
5119 r |= SOPT_GLOBAL;
5120 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005121 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005122 }
5123 else
5124 {
5125 if (p->indir & PV_BOTH)
5126 r |= SOPT_GLOBAL;
5127 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005128 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005129
5130 if (p->indir & PV_WIN)
5131 {
5132 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005133 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005134 else
5135 r |= SOPT_WIN;
5136 }
5137 else if (p->indir & PV_BUF)
5138 {
5139 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005140 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005141 else
5142 r |= SOPT_BUF;
5143 }
5144 }
5145
5146 if (stringval == NULL)
5147 return r;
5148
5149 if (opt_type == SREQ_GLOBAL)
5150 varp = p->var;
5151 else
5152 {
5153 if (opt_type == SREQ_BUF)
5154 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005155 // Special case: 'modified' is b_changed, but we also want to
5156 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005157 if (p->indir == PV_MOD)
5158 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005159 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005160 varp = NULL;
5161 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005162# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005163 else if (p->indir == PV_KEY)
5164 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005165 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005166 *stringval = NULL;
5167 varp = NULL;
5168 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005169# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005170 else
5171 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005172 buf_T *save_curbuf = curbuf;
5173
5174 // only getting a pointer, no need to use aucmd_prepbuf()
5175 curbuf = (buf_T *)from;
5176 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005177 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005178 curbuf = save_curbuf;
5179 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005180 }
5181 }
5182 else if (opt_type == SREQ_WIN)
5183 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005184 win_T *save_curwin = curwin;
5185
5186 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005187 curbuf = curwin->w_buffer;
5188 varp = get_varp(p);
5189 curwin = save_curwin;
5190 curbuf = curwin->w_buffer;
5191 }
5192 if (varp == p->var)
5193 return (r | SOPT_UNSET);
5194 }
5195
5196 if (varp != NULL)
5197 {
5198 if (p->flags & P_STRING)
5199 *stringval = vim_strsave(*(char_u **)(varp));
5200 else if (p->flags & P_NUM)
5201 *numval = *(long *) varp;
5202 else
5203 *numval = *(int *)varp;
5204 }
5205
5206 return r;
5207}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005208
5209/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005210 * Iterate over options. First argument is a pointer to a pointer to a
5211 * structure inside options[] array, second is option type like in the above
5212 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005213 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005214 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005215 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005216 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005217 * first argument to NULL.
5218 *
5219 * Returns full option name for current option on each call.
5220 */
5221 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005222option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005223{
5224 struct vimoption *ret = NULL;
5225 do
5226 {
5227 if (*option == NULL)
5228 *option = (void *) options;
5229 else if (((struct vimoption *) (*option))->fullname == NULL)
5230 {
5231 *option = NULL;
5232 return NULL;
5233 }
5234 else
5235 *option = (void *) (((struct vimoption *) (*option)) + 1);
5236
5237 ret = ((struct vimoption *) (*option));
5238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005239 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005240 if (ret->var == NULL)
5241 {
5242 ret = NULL;
5243 continue;
5244 }
5245
5246 switch (opt_type)
5247 {
5248 case SREQ_GLOBAL:
5249 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5250 ret = NULL;
5251 break;
5252 case SREQ_BUF:
5253 if (!(ret->indir & PV_BUF))
5254 ret = NULL;
5255 break;
5256 case SREQ_WIN:
5257 if (!(ret->indir & PV_WIN))
5258 ret = NULL;
5259 break;
5260 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005261 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005262 return NULL;
5263 }
5264 }
5265 while (ret == NULL);
5266
5267 return (char_u *)ret->fullname;
5268}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005269#endif
5270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005272 * Return the flags for the option at 'opt_idx'.
5273 */
5274 long_u
5275get_option_flags(int opt_idx)
5276{
5277 return options[opt_idx].flags;
5278}
5279
5280/*
5281 * Set a flag for the option at 'opt_idx'.
5282 */
5283 void
5284set_option_flag(int opt_idx, long_u flag)
5285{
5286 options[opt_idx].flags |= flag;
5287}
5288
5289/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005290 * Returns TRUE if the option at 'opt_idx' is a global option
5291 */
5292 int
5293is_global_option(int opt_idx)
5294{
5295 return options[opt_idx].indir == PV_NONE;
5296}
5297
5298/*
5299 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5300 * local value.
5301 */
5302 int
5303is_global_local_option(int opt_idx)
5304{
5305 return options[opt_idx].indir & PV_BOTH;
5306}
5307
5308/*
5309 * Returns TRUE if the option at 'opt_idx' is a window-local option
5310 */
5311 int
5312is_window_local_option(int opt_idx)
5313{
5314 return options[opt_idx].var == VAR_WIN;
5315}
5316
5317/*
5318 * Returns TRUE if the option at 'opt_idx' is a hidden option
5319 */
5320 int
5321is_hidden_option(int opt_idx)
5322{
5323 return options[opt_idx].var == NULL;
5324}
5325
5326#if defined(FEAT_CRYPT) || defined(PROTO)
5327/*
5328 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5329 */
5330 int
5331is_crypt_key_option(int opt_idx)
5332{
5333 return options[opt_idx].indir == PV_KEY;
5334}
5335#endif
5336
5337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 * Set the value of option "name".
5339 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005340 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005341 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005343 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005344set_option_value(
5345 char_u *name,
5346 long number,
5347 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005348 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349{
5350 int opt_idx;
5351 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005352 long_u flags;
Christian Brabandtb39b2402023-11-29 11:34:05 +01005353 static char errbuf[ERR_BUFLEN];
5354 int errbuflen = ERR_BUFLEN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
5356 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005357 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005358 {
5359 int key;
5360
5361 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005362 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005363 {
5364 char_u key_name[2];
5365
5366 if (key < 0)
5367 {
5368 key_name[0] = KEY2TERMCAP0(key);
5369 key_name[1] = KEY2TERMCAP1(key);
5370 }
5371 else
5372 {
5373 key_name[0] = KS_KEY;
5374 key_name[1] = (key & 0xff);
5375 }
5376 add_termcode(key_name, string, FALSE);
5377 if (full_screen)
5378 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005379 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005380 return NULL;
5381 }
5382
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005383 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 else
5386 {
5387 flags = options[opt_idx].flags;
5388#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005389 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005391 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005392 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005393 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005396 if (flags & P_STRING)
Christian Brabandtb39b2402023-11-29 11:34:05 +01005397 return set_string_option(opt_idx, string, opt_flags, errbuf, errbuflen);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005398
5399 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5400 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005402 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005404 int idx;
5405
5406 // Either we are given a string or we are setting option
5407 // to zero.
5408 for (idx = 0; string[idx] == '0'; ++idx)
5409 ;
5410 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005411 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005412 // There's another character after zeros or the string
5413 // is empty. In both cases, we are trying to set a
5414 // num option using a string.
5415 semsg(_(e_number_required_after_str_equal_str),
5416 name, string);
5417 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005418
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005421 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005422 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005423 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005424 errbuf, sizeof(errbuf), opt_flags);
5425 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005426 else
5427 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005431 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432}
5433
5434/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005435 * Call set_option_value() and when an error is returned report it.
5436 */
5437 void
5438set_option_value_give_err(
5439 char_u *name,
5440 long number,
5441 char_u *string,
5442 int opt_flags) // OPT_LOCAL or 0 (both)
5443{
5444 char *errmsg = set_option_value(name, number, string, opt_flags);
5445
5446 if (errmsg != NULL)
5447 emsg(_(errmsg));
5448}
5449
5450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 * Get the terminal code for a terminal option.
5452 * Returns NULL when not found.
5453 */
5454 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005455get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456{
5457 int opt_idx;
5458 char_u *varp;
5459
5460 if (tname[0] != 't' || tname[1] != '_' ||
5461 tname[2] == NUL || tname[3] == NUL)
5462 return NULL;
5463 if ((opt_idx = findoption(tname)) >= 0)
5464 {
5465 varp = get_varp(&(options[opt_idx]));
5466 if (varp != NULL)
5467 varp = *(char_u **)(varp);
5468 return varp;
5469 }
5470 return find_termcode(tname + 2);
5471}
5472
5473 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005474get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 int i;
5477
5478 i = findoption((char_u *)"hl");
5479 if (i >= 0)
5480 return options[i].def_val[VI_DEFAULT];
5481 return (char_u *)NULL;
5482}
5483
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005484 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005485get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005486{
5487 int i;
5488
5489 i = findoption((char_u *)"enc");
5490 if (i >= 0)
5491 return options[i].def_val[VI_DEFAULT];
5492 return (char_u *)NULL;
5493}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005494
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005495#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005496 int
5497is_option_allocated(char *name)
5498{
5499 int idx = findoption((char_u *)name);
5500
5501 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5502}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005503#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005504
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505/*
5506 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005507 * When "has_lt" is true there is a '<' before "*arg_arg".
5508 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 */
5510 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005511find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005513 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005515 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005517 // Don't use get_special_key_code() for t_xx, we don't want it to call
5518 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5520 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005521 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005523 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005525 key = find_special_key(&arg, &modifiers,
5526 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005527 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 key = 0;
5529 }
5530 return key;
5531}
5532
5533/*
5534 * if 'all' == 0: show changed options
5535 * if 'all' == 1: show all normal options
5536 * if 'all' == 2: show all terminal options
5537 */
5538 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005539showoptions(
5540 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005541 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
5543 struct vimoption *p;
5544 int col;
5545 int isterm;
5546 char_u *varp;
5547 struct vimoption **items;
5548 int item_count;
5549 int run;
5550 int row, rows;
5551 int cols;
5552 int i;
5553 int len;
5554
5555#define INC 20
5556#define GAP 3
5557
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005558 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 if (items == NULL)
5560 return;
5561
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005562 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005564 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005566 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005568 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005570 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005572 // Do the loop two times:
5573 // 1. display the short items
5574 // 2. display the long items (only strings and numbers)
5575 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 for (run = 1; run <= 2 && !got_int; ++run)
5577 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005578 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 item_count = 0;
5580 for (p = &options[0]; p->fullname != NULL; p++)
5581 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005582 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005583 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005584 continue;
5585
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 varp = NULL;
5587 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005588 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 {
5590 if (p->indir != PV_NONE && !isterm)
5591 varp = get_varp_scope(p, opt_flags);
5592 }
5593 else
5594 varp = get_varp(p);
5595 if (varp != NULL
5596 && ((all == 2 && isterm)
5597 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005598 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005600 if (opt_flags & OPT_ONECOLUMN)
5601 len = Columns;
5602 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005603 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 else
5605 {
5606 option_value2string(p, opt_flags);
5607 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5608 }
5609 if ((len <= INC - GAP && run == 1) ||
5610 (len > INC - GAP && run == 2))
5611 items[item_count++] = p;
5612 }
5613 }
5614
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005615 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 if (run == 1)
5617 {
5618 cols = (Columns + GAP - 3) / INC;
5619 if (cols == 0)
5620 cols = 1;
5621 rows = (item_count + cols - 1) / cols;
5622 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005623 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 rows = item_count;
5625 for (row = 0; row < rows && !got_int; ++row)
5626 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005627 msg_putchar('\n'); // go to next line
5628 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 break;
5630 col = 0;
5631 for (i = row; i < item_count; i += rows)
5632 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005633 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 showoneopt(items[i], opt_flags);
5635 col += INC;
5636 }
5637 out_flush();
5638 ui_breakcheck();
5639 }
5640 }
5641 vim_free(items);
5642}
5643
5644/*
5645 * Return TRUE if option "p" has its default value.
5646 */
5647 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005648optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
5650 int dvi;
5651
5652 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005653 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005654 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005656 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005658 // the cast to long is required for Manx C, long_i is
5659 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005660 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005661 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5663}
5664
5665/*
5666 * showoneopt: show the value of one option
5667 * must not be called with a hidden option!
5668 */
5669 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005670showoneopt(
5671 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005672 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005674 char_u *varp;
5675 int save_silent = silent_mode;
5676
5677 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005678 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
5680 varp = get_varp_scope(p, opt_flags);
5681
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005682 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5684 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005685 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005687 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005689 msg_puts(" ");
5690 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 if (!(p->flags & P_BOOL))
5692 {
5693 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005694 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 option_value2string(p, opt_flags);
5696 msg_outtrans(NameBuff);
5697 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005698
5699 silent_mode = save_silent;
5700 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701}
5702
5703/*
5704 * Write modified options as ":set" commands to a file.
5705 *
5706 * There are three values for "opt_flags":
5707 * OPT_GLOBAL: Write global option values and fresh values of
5708 * buffer-local options (used for start of a session
5709 * file).
5710 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5711 * curwin (used for a vimrc file).
5712 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5713 * and local values for window-local options of
5714 * curwin. Local values are also written when at the
5715 * default value, because a modeline or autocommand
5716 * may have set them when doing ":edit file" and the
5717 * user has set them back at the default or fresh
5718 * value.
5719 * When "local_only" is TRUE, don't write fresh
5720 * values, only local values (for ":mkview").
5721 * (fresh value = value used for a new buffer or window for a local option).
5722 *
5723 * Return FAIL on error, OK otherwise.
5724 */
5725 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005726makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
5728 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005729 char_u *varp; // currently used value
5730 char_u *varp_fresh; // local value
5731 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 char *cmd;
5733 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005734 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
5736 /*
5737 * The options that don't have a default (terminal name, columns, lines)
5738 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005739 * Do the loop over "options[]" twice: once for options with the
5740 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005742 for (pri = 1; pri >= 0; --pri)
5743 {
5744 for (p = &options[0]; !istermoption(p); p++)
5745 if (!(p->flags & P_NO_MKRC)
5746 && !istermoption(p)
5747 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005749 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5751 continue;
5752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005753 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5754 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5756 continue;
5757
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005758 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005760 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 continue;
5762
Bram Moolenaard23b7142021-04-17 21:04:34 +02005763 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5764 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005765 continue;
5766
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 round = 2;
5768 if (p->indir != PV_NONE)
5769 {
5770 if (p->var == VAR_WIN)
5771 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005772 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 if (!(opt_flags & OPT_LOCAL))
5774 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005775 // When fresh value of window-local option is not at the
5776 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5778 {
5779 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005780 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 {
5782 round = 1;
5783 varp_local = varp;
5784 varp = varp_fresh;
5785 }
5786 }
5787 }
5788 }
5789
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005790 // Round 1: fresh value for window-local options.
5791 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 for ( ; round <= 2; varp = varp_local, ++round)
5793 {
5794 if (round == 1 || (opt_flags & OPT_GLOBAL))
5795 cmd = "set";
5796 else
5797 cmd = "setlocal";
5798
5799 if (p->flags & P_BOOL)
5800 {
5801 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5802 return FAIL;
5803 }
5804 else if (p->flags & P_NUM)
5805 {
5806 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5807 return FAIL;
5808 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005809 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005811 int do_endif = FALSE;
5812
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005813 // Don't set 'syntax' and 'filetype' again if the value is
5814 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005815 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005816#if defined(FEAT_SYN_HL)
5817 p->indir == PV_SYN ||
5818#endif
5819 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 {
5821 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5822 *(char_u **)(varp)) < 0
5823 || put_eol(fd) < 0)
5824 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005825 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 }
5827 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005828 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005830 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
5832 if (put_line(fd, "endif") == FAIL)
5833 return FAIL;
5834 }
5835 }
5836 }
5837 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 return OK;
5840}
5841
5842#if defined(FEAT_FOLDING) || defined(PROTO)
5843/*
5844 * Generate set commands for the local fold options only. Used when
5845 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5846 */
5847 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005848makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005850 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005852 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 == FAIL
5854# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005855 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005857 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 == FAIL
5859 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5860 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5861 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5862 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5863 )
5864 return FAIL;
5865
5866 return OK;
5867}
5868#endif
5869
5870 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005871put_setstring(
5872 FILE *fd,
5873 char *cmd,
5874 char *name,
5875 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005876 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877{
5878 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005879 char_u *buf = NULL;
5880 char_u *part = NULL;
5881 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
5883 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5884 return FAIL;
5885 if (*valuep != NULL)
5886 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005887 // Output 'pastetoggle' as key names. For other
5888 // options some characters have to be escaped with
5889 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 if (valuep == &p_pt)
5891 {
5892 s = *valuep;
5893 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005894 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 return FAIL;
5896 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005897 // expand the option value, replace $HOME by ~
5898 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005900 int size = (int)STRLEN(*valuep) + 1;
5901
5902 // replace home directory in the whole option value into "buf"
5903 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005904 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005905 goto fail;
5906 home_replace(NULL, *valuep, buf, size, FALSE);
5907
5908 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005909 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005910 // can be expanded when read back.
5911 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5912 && vim_strchr(*valuep, ',') != NULL)
5913 {
5914 part = alloc(size);
5915 if (part == NULL)
5916 goto fail;
5917
5918 // write line break to clear the option, e.g. ':set rtp='
5919 if (put_eol(fd) == FAIL)
5920 goto fail;
5921
5922 p = buf;
5923 while (*p != NUL)
5924 {
5925 // for each comma separated option part, append value to
5926 // the option, :set rtp+=value
5927 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5928 goto fail;
5929 (void)copy_option_part(&p, part, size, ",");
5930 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5931 goto fail;
5932 }
5933 vim_free(buf);
5934 vim_free(part);
5935 return OK;
5936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005938 {
5939 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005941 }
5942 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 }
5944 else if (put_escstr(fd, *valuep, 2) == FAIL)
5945 return FAIL;
5946 }
5947 if (put_eol(fd) < 0)
5948 return FAIL;
5949 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005950fail:
5951 vim_free(buf);
5952 vim_free(part);
5953 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954}
5955
5956 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005957put_setnum(
5958 FILE *fd,
5959 char *cmd,
5960 char *name,
5961 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
5963 long wc;
5964
5965 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5966 return FAIL;
5967 if (wc_use_keyname((char_u *)valuep, &wc))
5968 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005969 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5971 return FAIL;
5972 }
5973 else if (fprintf(fd, "%ld", *valuep) < 0)
5974 return FAIL;
5975 if (put_eol(fd) < 0)
5976 return FAIL;
5977 return OK;
5978}
5979
5980 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005981put_setbool(
5982 FILE *fd,
5983 char *cmd,
5984 char *name,
5985 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005987 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005988 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5990 || put_eol(fd) < 0)
5991 return FAIL;
5992 return OK;
5993}
5994
5995/*
5996 * Clear all the terminal options.
5997 * If the option has been allocated, free the memory.
5998 * Terminal options are never hidden or indirect.
5999 */
6000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006001clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006003 // Reset a few things before clearing the old options. This may cause
6004 // outputting a few things that the terminal doesn't understand, but the
6005 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006006 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006007 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006009 // When starting the GUI close the display opened for the clipboard.
6010 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 if (gui.starting)
6012 clear_xterm_clip();
6013#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006014 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006016 free_termoptions();
6017}
6018
6019 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006020free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006021{
6022 struct vimoption *p;
6023
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006024 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 if (istermoption(p))
6026 {
6027 if (p->flags & P_ALLOCED)
6028 free_string_option(*(char_u **)(p->var));
6029 if (p->flags & P_DEF_ALLOCED)
6030 free_string_option(p->def_val[VI_DEFAULT]);
6031 *(char_u **)(p->var) = empty_option;
6032 p->def_val[VI_DEFAULT] = empty_option;
6033 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006034#ifdef FEAT_EVAL
6035 // remember where the option was cleared
6036 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 }
6039 clear_termcodes();
6040}
6041
6042/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006043 * Free the string for one term option, if it was allocated.
6044 * Set the string to empty_option and clear allocated flag.
6045 * "var" points to the option value.
6046 */
6047 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006048free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006049{
6050 struct vimoption *p;
6051
6052 for (p = &options[0]; p->fullname != NULL; p++)
6053 if (p->var == var)
6054 {
6055 if (p->flags & P_ALLOCED)
6056 free_string_option(*(char_u **)(p->var));
6057 *(char_u **)(p->var) = empty_option;
6058 p->flags &= ~P_ALLOCED;
6059 break;
6060 }
6061}
6062
6063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 * Set the terminal option defaults to the current value.
6065 * Used after setting the terminal name.
6066 */
6067 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006068set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069{
6070 struct vimoption *p;
6071
6072 for (p = &options[0]; p->fullname != NULL; p++)
6073 {
6074 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6075 {
6076 if (p->flags & P_DEF_ALLOCED)
6077 {
6078 free_string_option(p->def_val[VI_DEFAULT]);
6079 p->flags &= ~P_DEF_ALLOCED;
6080 }
6081 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6082 if (p->flags & P_ALLOCED)
6083 {
6084 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006085 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 }
6087 }
6088 }
6089}
6090
6091/*
6092 * return TRUE if 'p' starts with 't_'
6093 */
6094 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006095istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096{
6097 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6098}
6099
Bram Moolenaardac13472019-09-16 21:06:21 +02006100/*
6101 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6102 */
6103 int
6104istermoption_idx(int opt_idx)
6105{
6106 return istermoption(&options[opt_idx]);
6107}
6108
Bram Moolenaar113e1072019-01-20 15:30:40 +01006109#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006111 * Unset local option value, similar to ":set opt<".
6112 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006113 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006114unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006115{
6116 struct vimoption *p;
6117 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006118 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006119
6120 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006121 if (opt_idx < 0)
6122 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006123 p = &(options[opt_idx]);
6124
6125 switch ((int)p->indir)
6126 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006127 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006128 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006129 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006130 break;
6131 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006132 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006133 break;
6134 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006135 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006136 break;
6137 case PV_AR:
6138 buf->b_p_ar = -1;
6139 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006140 case PV_BKC:
6141 clear_string_option(&buf->b_p_bkc);
6142 buf->b_bkc_flags = 0;
6143 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006144 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006145 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006146 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006147 case PV_TC:
6148 clear_string_option(&buf->b_p_tc);
6149 buf->b_tc_flags = 0;
6150 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006151 case PV_SISO:
6152 curwin->w_p_siso = -1;
6153 break;
6154 case PV_SO:
6155 curwin->w_p_so = -1;
6156 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006157# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006158 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006159 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006160 break;
6161 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006162 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006163 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006164# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006165 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006166 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006167 break;
6168 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006169 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006170 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006171# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006172 case PV_TSRFU:
6173 clear_string_option(&buf->b_p_tsrfu);
6174 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006175# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006176 case PV_FP:
6177 clear_string_option(&buf->b_p_fp);
6178 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006179# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006180 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006181 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006182 break;
6183 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006184 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006185 break;
6186 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006187 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006188 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006189# endif
6190# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006191 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006192 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006193 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006194# endif
6195# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006196 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006197 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006198 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006199# endif
6200# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006201 case PV_SBR:
6202 clear_string_option(&((win_T *)from)->w_p_sbr);
6203 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006204# endif
6205# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006206 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006207 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006208 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006209# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006210 case PV_UL:
6211 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6212 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006213 case PV_LW:
6214 clear_string_option(&buf->b_p_lw);
6215 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006216 case PV_MENC:
6217 clear_string_option(&buf->b_p_menc);
6218 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006219 case PV_LCS:
6220 clear_string_option(&((win_T *)from)->w_p_lcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006221 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006222 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006223 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006224 case PV_FCS:
6225 clear_string_option(&((win_T *)from)->w_p_fcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006226 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006227 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006228 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006229 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006230 clear_string_option(&((win_T *)from)->w_p_ve);
6231 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006232 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006233 }
6234}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006235#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006236
6237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006239 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 */
6241 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006242get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006244 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 {
6246 if (p->var == VAR_WIN)
6247 return (char_u *)GLOBAL_WO(get_varp(p));
6248 return p->var;
6249 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006250 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 {
6252 switch ((int)p->indir)
6253 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006254 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006256 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6257 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6258 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006260 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6261 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6262 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006263 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006264 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006265 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006266 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6267 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006269 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6270 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006272 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6273 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006274#ifdef FEAT_COMPL_FUNC
6275 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6276#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006277#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6278 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6279#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006280#if defined(FEAT_CRYPT)
6281 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6282#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006283#ifdef FEAT_LINEBREAK
6284 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6285#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006286#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006287 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006288#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006289 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006290 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006291 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006292 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006293 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006294 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006295 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006296
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006298 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 }
6300 return get_varp(p);
6301}
6302
6303/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006304 * Get pointer to option variable at 'opt_idx', depending on local or global
6305 * scope.
6306 */
6307 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006308get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006309{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006310 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006311}
6312
6313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 * Get pointer to option variable.
6315 */
6316 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006317get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006319 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 if (p->var == NULL)
6321 return NULL;
6322
6323 switch ((int)p->indir)
6324 {
6325 case PV_NONE: return p->var;
6326
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006327 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006328 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006330 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006332 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006334 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006336 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006338 case PV_TC: return *curbuf->b_p_tc != NUL
6339 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006340 case PV_BKC: return *curbuf->b_p_bkc != NUL
6341 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006342 case PV_SISO: return curwin->w_p_siso >= 0
6343 ? (char_u *)&(curwin->w_p_siso) : p->var;
6344 case PV_SO: return curwin->w_p_so >= 0
6345 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006347 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006349 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6351#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006352 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006354 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006356#ifdef FEAT_COMPL_FUNC
6357 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6358 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6359#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006360 case PV_FP: return *curbuf->b_p_fp != NUL
6361 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006363 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006365 case PV_GP: return *curbuf->b_p_gp != NUL
6366 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6367 case PV_MP: return *curbuf->b_p_mp != NUL
6368 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006370#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6371 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6372 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6373#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006374#if defined(FEAT_CRYPT)
6375 case PV_CM: return *curbuf->b_p_cm != NUL
6376 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6377#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006378#ifdef FEAT_LINEBREAK
6379 case PV_SBR: return *curwin->w_p_sbr != NUL
6380 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6381#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006382#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006383 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006384 ? (char_u *)&(curwin->w_p_stl) : p->var;
6385#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006386 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6387 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006388 case PV_LW: return *curbuf->b_p_lw != NUL
6389 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006390 case PV_MENC: return *curbuf->b_p_menc != NUL
6391 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392#ifdef FEAT_ARABIC
6393 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6394#endif
6395 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006396 case PV_LCS: return *curwin->w_p_lcs != NUL
6397 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006398 case PV_FCS: return *curwin->w_p_fcs != NUL
6399 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006400 case PV_VE: return *curwin->w_p_ve != NUL
6401 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006402#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006403 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6404#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006405#ifdef FEAT_SYN_HL
6406 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6407 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006408 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006409 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411#ifdef FEAT_DIFF
6412 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6413#endif
6414#ifdef FEAT_FOLDING
6415 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6416 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6417 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6418 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6419 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6420 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6421 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6422# ifdef FEAT_EVAL
6423 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6424 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6425# endif
6426 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6427#endif
6428 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006429 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006430#ifdef FEAT_LINEBREAK
6431 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006434 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006435#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6437#endif
6438#ifdef FEAT_RIGHTLEFT
6439 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6440 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6441#endif
6442 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006443 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6445#ifdef FEAT_LINEBREAK
6446 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006447 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6448 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006450 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006452 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006453#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006454 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6455 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6456#endif
6457#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006458 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6459 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6460 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462
6463 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6464 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6467 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6469 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6471 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6472 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006473 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476#ifdef FEAT_FOLDING
6477 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006480#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006481 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006483#ifdef FEAT_COMPL_FUNC
6484 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006485 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006486#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006487#ifdef FEAT_EVAL
6488 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6489#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006490 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006492 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006498 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6500 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6501 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6502 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6503#ifdef FEAT_FIND_ID
6504# ifdef FEAT_EVAL
6505 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6506# endif
6507#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006508#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6510 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6511#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006512#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006513 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515#ifdef FEAT_CRYPT
6516 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006519 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6521 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6522 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6523 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6524 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006526 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6533#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006534 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006535 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6536#endif
6537#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006538 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6539 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6540 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006541 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542#endif
6543 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6544 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6545 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6546 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006547#ifdef FEAT_PERSISTENT_UNDO
6548 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6551#ifdef FEAT_KEYMAP
6552 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6553#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006554#ifdef FEAT_SIGNS
6555 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6556#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006557#ifdef FEAT_VARTABS
6558 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6559 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6560#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006561 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006563 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 return (char_u *)&(curbuf->b_p_wm);
6565}
6566
6567/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006568 * Return a pointer to the variable for option at 'opt_idx'
6569 */
6570 char_u *
6571get_option_var(int opt_idx)
6572{
6573 return options[opt_idx].var;
6574}
6575
Dominique Pelle748b3082022-01-08 12:41:16 +00006576#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006577/*
6578 * Return the full name of the option at 'opt_idx'
6579 */
6580 char_u *
6581get_option_fullname(int opt_idx)
6582{
6583 return (char_u *)options[opt_idx].fullname;
6584}
Dominique Pelle748b3082022-01-08 12:41:16 +00006585#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006586
6587/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006588 * Return the did_set callback function for the option at 'opt_idx'
6589 */
6590 opt_did_set_cb_T
6591get_option_did_set_cb(int opt_idx)
6592{
6593 return options[opt_idx].opt_did_set_cb;
6594}
6595
6596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 * Get the value of 'equalprg', either the buffer-local one or the global one.
6598 */
6599 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006600get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
6602 if (*curbuf->b_p_ep == NUL)
6603 return p_ep;
6604 return curbuf->b_p_ep;
6605}
6606
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607/*
6608 * Copy options from one window to another.
6609 * Used when splitting a window.
6610 */
6611 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006612win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
6614 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6615 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006616 after_copy_winopt(wp_to);
6617}
6618
6619/*
6620 * After copying window options: update variables depending on options.
6621 */
6622 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006623after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006624{
6625#ifdef FEAT_LINEBREAK
6626 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006627#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006628#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006629 fill_culopt_flags(NULL, wp);
6630 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006631#endif
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006632 set_listchars_option(wp, wp->w_p_lcs, TRUE);
6633 set_fillchars_option(wp, wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006634}
6635
6636 static char_u *
6637copy_option_val(char_u *val)
6638{
6639 if (val == empty_option)
6640 return empty_option; // no need to allocate memory
6641 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643
6644/*
6645 * Copy the options from one winopt_T to another.
6646 * Doesn't free the old option values in "to", use clear_winopt() for that.
6647 * The 'scroll' option is not copied, because it depends on the window height.
6648 * The 'previewwindow' option is reset, there can be only one preview window.
6649 */
6650 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006651copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652{
6653#ifdef FEAT_ARABIC
6654 to->wo_arab = from->wo_arab;
6655#endif
6656 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006657 to->wo_lcs = copy_option_val(from->wo_lcs);
6658 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006660 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006661 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006662 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006663#ifdef FEAT_LINEBREAK
6664 to->wo_nuw = from->wo_nuw;
6665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666#ifdef FEAT_RIGHTLEFT
6667 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006668 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006670#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006671 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006672#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006673#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006674 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006677#ifdef FEAT_DIFF
6678 to->wo_wrap_save = from->wo_wrap_save;
6679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680#ifdef FEAT_LINEBREAK
6681 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006682 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006683 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006685 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006687 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006688 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006689 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006690 to->wo_crb_save = from->wo_crb_save;
Christian Brabandt4a8eb6e2023-08-13 19:43:42 +02006691 to->wo_siso = from->wo_siso;
6692 to->wo_so = from->wo_so;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006693#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006694 to->wo_spell = from->wo_spell;
6695#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006696#ifdef FEAT_SYN_HL
6697 to->wo_cuc = from->wo_cuc;
6698 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006699 to->wo_culopt = copy_option_val(from->wo_culopt);
6700 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702#ifdef FEAT_DIFF
6703 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006704 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006706#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006707 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006708 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006709#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006710#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006711 to->wo_twk = copy_option_val(from->wo_twk);
6712 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714#ifdef FEAT_FOLDING
6715 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006716 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006718 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006719 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 to->wo_fml = from->wo_fml;
6721 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006722 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006723 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006724 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006725 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 to->wo_fdn = from->wo_fdn;
6727# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006728 to->wo_fde = copy_option_val(from->wo_fde);
6729 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006731 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006733#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006734 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006735#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006736
6737#ifdef FEAT_EVAL
6738 // Copy the script context so that we know where the value was last set.
6739 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6740 sizeof(to->wo_script_ctx));
6741#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006742 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743}
6744
6745/*
6746 * Check string options in a window for a NULL value.
6747 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006748 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006749check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750{
6751 check_winopt(&win->w_onebuf_opt);
6752 check_winopt(&win->w_allbuf_opt);
6753}
6754
6755/*
6756 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6757 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006758 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006759check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760{
6761#ifdef FEAT_FOLDING
6762 check_string_option(&wop->wo_fdi);
6763 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006764 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765# ifdef FEAT_EVAL
6766 check_string_option(&wop->wo_fde);
6767 check_string_option(&wop->wo_fdt);
6768# endif
6769 check_string_option(&wop->wo_fmr);
6770#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006771#ifdef FEAT_SIGNS
6772 check_string_option(&wop->wo_scl);
6773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774#ifdef FEAT_RIGHTLEFT
6775 check_string_option(&wop->wo_rlc);
6776#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006777#ifdef FEAT_LINEBREAK
6778 check_string_option(&wop->wo_sbr);
6779#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006780#ifdef FEAT_STL_OPT
6781 check_string_option(&wop->wo_stl);
6782#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006783#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006784 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006785 check_string_option(&wop->wo_cc);
6786#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006787#ifdef FEAT_CONCEAL
6788 check_string_option(&wop->wo_cocu);
6789#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006790#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006791 check_string_option(&wop->wo_twk);
6792 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006793#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006794#ifdef FEAT_LINEBREAK
6795 check_string_option(&wop->wo_briopt);
6796#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006797 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006798 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006799 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006800 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801}
6802
6803/*
6804 * Free the allocated memory inside a winopt_T.
6805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006807clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808{
6809#ifdef FEAT_FOLDING
6810 clear_string_option(&wop->wo_fdi);
6811 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006812 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813# ifdef FEAT_EVAL
6814 clear_string_option(&wop->wo_fde);
6815 clear_string_option(&wop->wo_fdt);
6816# endif
6817 clear_string_option(&wop->wo_fmr);
6818#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006819#ifdef FEAT_SIGNS
6820 clear_string_option(&wop->wo_scl);
6821#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006822#ifdef FEAT_LINEBREAK
6823 clear_string_option(&wop->wo_briopt);
6824#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006825 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826#ifdef FEAT_RIGHTLEFT
6827 clear_string_option(&wop->wo_rlc);
6828#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006829#ifdef FEAT_LINEBREAK
6830 clear_string_option(&wop->wo_sbr);
6831#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006832#ifdef FEAT_STL_OPT
6833 clear_string_option(&wop->wo_stl);
6834#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006835#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006836 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006837 clear_string_option(&wop->wo_cc);
6838#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006839#ifdef FEAT_CONCEAL
6840 clear_string_option(&wop->wo_cocu);
6841#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006842#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006843 clear_string_option(&wop->wo_twk);
6844 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006845#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006846 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006847 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006848 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849}
6850
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006851#ifdef FEAT_EVAL
6852// Index into the options table for a buffer-local option enum.
6853static int buf_opt_idx[BV_COUNT];
6854# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6855
6856/*
6857 * Initialize buf_opt_idx[] if not done already.
6858 */
6859 static void
6860init_buf_opt_idx(void)
6861{
6862 static int did_init_buf_opt_idx = FALSE;
6863 int i;
6864
6865 if (did_init_buf_opt_idx)
6866 return;
6867 did_init_buf_opt_idx = TRUE;
6868 for (i = 0; !istermoption_idx(i); i++)
6869 if (options[i].indir & PV_BUF)
6870 buf_opt_idx[options[i].indir & PV_MASK] = i;
6871}
6872#else
6873# define COPY_OPT_SCTX(buf, bv)
6874#endif
6875
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876/*
6877 * Copy global option values to local options for one buffer.
6878 * Used when creating a new buffer and sometimes when entering a buffer.
6879 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006880 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6882 * appropriate.
6883 * BCO_NOHELP Don't copy the values to a help buffer.
6884 */
6885 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006886buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887{
6888 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006889 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 int dont_do_help;
6891 int did_isk = FALSE;
6892
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006893 // Skip this when the option defaults have not been set yet. Happens when
6894 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 if (p_cpo != NULL)
6896 {
6897 /*
6898 * Always copy when entering and 'cpo' contains 'S'.
6899 * Don't copy when already initialized.
6900 * Don't copy when 'cpo' contains 's' and not entering.
6901 * 'S' BCO_ENTER initialized 's' should_copy
6902 * yes yes X X TRUE
6903 * yes no yes X FALSE
6904 * no X yes X FALSE
6905 * X no no yes FALSE
6906 * X no no no TRUE
6907 * no yes no X TRUE
6908 */
6909 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6910 && (buf->b_p_initialized
6911 || (!(flags & BCO_ENTER)
6912 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6913 should_copy = FALSE;
6914
6915 if (should_copy || (flags & BCO_ALWAYS))
6916 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006917#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006918 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006919 init_buf_opt_idx();
6920#endif
6921 // Don't copy the options specific to a help buffer when
6922 // BCO_NOHELP is given or the options were initialized already
6923 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6925 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006926 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 {
6928 save_p_isk = buf->b_p_isk;
6929 buf->b_p_isk = NULL;
6930 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006931 // Always free the allocated strings. If not already initialized,
6932 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 if (!buf->b_p_initialized)
6934 {
6935 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006936 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006939 switch (*p_ffs)
6940 {
6941 case 'm':
6942 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6943 case 'd':
6944 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6945 case 'u':
6946 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6947 default:
6948 buf->b_p_ff = vim_strsave(p_ff);
6949 }
6950 if (buf->b_p_ff != NULL)
6951 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 buf->b_p_bh = empty_option;
6953 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 }
6955 else
6956 free_buf_options(buf, FALSE);
6957
6958 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006959 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 buf->b_p_ai_nopaste = p_ai_nopaste;
6961 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006962 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006964 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 buf->b_p_tw_nopaste = p_tw_nopaste;
6966 buf->b_p_tw_nobin = p_tw_nobin;
6967 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006968 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 buf->b_p_wm_nopaste = p_wm_nopaste;
6970 buf->b_p_wm_nobin = p_wm_nobin;
6971 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006972 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006973 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006974 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006975 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006976 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006978 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006980 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006982 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 buf->b_p_ml_nobin = p_ml_nobin;
6984 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006985 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006986 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006987 buf->b_p_swf = FALSE;
6988 else
6989 {
6990 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006991 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006994 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006995#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006996 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006997 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006999#ifdef FEAT_COMPL_FUNC
7000 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007001 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007002 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00007003 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007004 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007005 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007006#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007007#ifdef FEAT_EVAL
7008 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007009 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007010 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007013 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007015#ifdef FEAT_VARTABS
7016 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007017 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007018 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007019 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007020 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007021 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007022 buf->b_p_vsts_nopaste = p_vsts_nopaste
7023 ? vim_strsave(p_vsts_nopaste) : NULL;
7024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007026 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007028 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029#ifdef FEAT_FOLDING
7030 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007031 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032#endif
7033 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007034 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007035 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007036 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007037 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7038 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007040 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007042 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007044 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007046 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007047
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007049 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007051 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007053 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007054 buf->b_p_cinsd = vim_strsave(p_cinsd);
7055 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007056 buf->b_p_lop = vim_strsave(p_lop);
7057 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007058
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007059 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007062 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007064 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007066 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007068 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007070 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007071 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007072 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007073#endif
7074#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007075 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007076 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007077 (void)compile_cap_prog(&buf->b_s);
7078 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007079 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007080 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007081 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007082 buf->b_s.b_p_spo = vim_strsave(p_spo);
7083 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007085#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007087 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007089 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007091 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007092#if defined(FEAT_EVAL)
7093 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007094 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096#ifdef FEAT_CRYPT
7097 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007098 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007101 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102#ifdef FEAT_KEYMAP
7103 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007104 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 buf->b_kmap_state |= KEYMAP_INIT;
7106#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007107#ifdef FEAT_TERMINAL
7108 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007109 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007110#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007111 // This isn't really an option, but copying the langmap and IME
7112 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007114 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007116 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007118 // options that are normally global but also have a local value
7119 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007121 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007122 buf->b_p_bkc = empty_option;
7123 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124#ifdef FEAT_QUICKFIX
7125 buf->b_p_gp = empty_option;
7126 buf->b_p_mp = empty_option;
7127 buf->b_p_efm = empty_option;
7128#endif
7129 buf->b_p_ep = empty_option;
7130 buf->b_p_kp = empty_option;
7131 buf->b_p_path = empty_option;
7132 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007133 buf->b_p_tc = empty_option;
7134 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135#ifdef FEAT_FIND_ID
7136 buf->b_p_def = empty_option;
7137 buf->b_p_inc = empty_option;
7138# ifdef FEAT_EVAL
7139 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007140 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141# endif
7142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 buf->b_p_dict = empty_option;
7144 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007145#ifdef FEAT_COMPL_FUNC
7146 buf->b_p_tsrfu = empty_option;
7147#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007148 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007149 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007150#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7151 buf->b_p_bexpr = empty_option;
7152#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007153#if defined(FEAT_CRYPT)
7154 buf->b_p_cm = empty_option;
7155#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007156#ifdef FEAT_PERSISTENT_UNDO
7157 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007158 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007159#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007160 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007161 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007163 // Don't copy the options set by ex_help(), use the saved values,
7164 // when going from a help buffer to a non-help buffer.
7165 // Don't touch these at all when BCO_NOHELP is used and going from
7166 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007170#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007171 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007172 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007173 else
7174 buf->b_p_vts_array = NULL;
7175#endif
7176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 else
7178 {
7179 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007180 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 did_isk = TRUE;
7182 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007183 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007184#ifdef FEAT_VARTABS
7185 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007186 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007187 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007188 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007189 else
7190 buf->b_p_vts_array = NULL;
7191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 if (buf->b_p_bt[0] == 'h')
7194 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007196 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 }
7198 }
7199
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007200 // When the options should be copied (ignoring BCO_ALWAYS), set the
7201 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 if (should_copy)
7203 buf->b_p_initialized = TRUE;
7204 }
7205
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007206 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 if (did_isk)
7208 (void)buf_init_chartab(buf, FALSE);
7209}
7210
7211/*
7212 * Reset the 'modifiable' option and its default value.
7213 */
7214 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007215reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 int opt_idx;
7218
7219 curbuf->b_p_ma = FALSE;
7220 p_ma = FALSE;
7221 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007222 if (opt_idx >= 0)
7223 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224}
7225
7226/*
7227 * Set the global value for 'iminsert' to the local value.
7228 */
7229 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007230set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231{
7232 p_iminsert = curbuf->b_p_iminsert;
7233}
7234
7235/*
7236 * Set the global value for 'imsearch' to the local value.
7237 */
7238 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007239set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240{
7241 p_imsearch = curbuf->b_p_imsearch;
7242}
7243
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244static int expand_option_idx = -1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007245static int expand_option_start_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7247static int expand_option_flags = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007248static int expand_option_append = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249
7250 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007251set_context_in_set_cmd(
7252 expand_T *xp,
7253 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007254 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255{
7256 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007257 long_u flags = 0; // init for GCC
7258 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 char_u *p;
7260 char_u *s;
7261 int is_term_option = FALSE;
7262 int key;
7263
7264 expand_option_flags = opt_flags;
7265
7266 xp->xp_context = EXPAND_SETTINGS;
7267 if (*arg == NUL)
7268 {
7269 xp->xp_pattern = arg;
7270 return;
7271 }
7272 p = arg + STRLEN(arg) - 1;
7273 if (*p == ' ' && *(p - 1) != '\\')
7274 {
7275 xp->xp_pattern = p + 1;
7276 return;
7277 }
7278 while (p > arg)
7279 {
7280 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007281 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 if (*p == ' ' || *p == ',')
7283 {
7284 while (s > arg && *(s - 1) == '\\')
7285 --s;
7286 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007287 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 if (*p == ' ' && ((p - s) & 1) == 0)
7289 {
7290 ++p;
7291 break;
7292 }
7293 --p;
7294 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007295 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 {
7297 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007298 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 p += 2;
7300 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007301 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 {
7303 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007304 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 p += 3;
7306 }
7307 xp->xp_pattern = arg = p;
7308 if (*arg == '<')
7309 {
7310 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007311 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 return;
7313 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007314 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 {
7316 xp->xp_context = EXPAND_NOTHING;
7317 return;
7318 }
7319 nextchar = *++p;
7320 is_term_option = TRUE;
7321 expand_option_name[2] = KEY2TERMCAP0(key);
7322 expand_option_name[3] = KEY2TERMCAP1(key);
7323 }
7324 else
7325 {
7326 if (p[0] == 't' && p[1] == '_')
7327 {
7328 p += 2;
7329 if (*p != NUL)
7330 ++p;
7331 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007332 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 nextchar = *++p;
7334 is_term_option = TRUE;
7335 expand_option_name[2] = p[-2];
7336 expand_option_name[3] = p[-1];
7337 }
7338 else
7339 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007340 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7342 p++;
7343 if (*p == NUL)
7344 return;
7345 nextchar = *p;
7346 *p = NUL;
7347 opt_idx = findoption(arg);
7348 *p = nextchar;
7349 if (opt_idx == -1 || options[opt_idx].var == NULL)
7350 {
7351 xp->xp_context = EXPAND_NOTHING;
7352 return;
7353 }
7354 flags = options[opt_idx].flags;
7355 if (flags & P_BOOL)
7356 {
7357 xp->xp_context = EXPAND_NOTHING;
7358 return;
7359 }
7360 }
7361 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007362 // handle "-=" and "+="
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007363 expand_option_append = FALSE;
7364 int expand_option_subtract = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7366 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007367 if (nextchar == '-')
7368 expand_option_subtract = TRUE;
7369 if (nextchar == '+' || nextchar == '^')
7370 expand_option_append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 ++p;
7372 nextchar = '=';
7373 }
7374 if ((nextchar != '=' && nextchar != ':')
7375 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7376 {
7377 xp->xp_context = EXPAND_UNSUCCESSFUL;
7378 return;
7379 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007380
7381 // Below are for handling expanding a specific option's value after the '='
7382 // or ':'
7383
7384 if (is_term_option)
7385 expand_option_idx = -1;
7386 else
7387 expand_option_idx = opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02007389 if (!is_term_option)
7390 {
7391 if (options[opt_idx].flags & P_NO_CMD_EXPAND)
7392 {
7393 xp->xp_context=EXPAND_UNSUCCESSFUL;
7394 return;
7395 }
7396 }
7397
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 xp->xp_pattern = p + 1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007399 expand_option_start_col = (int)(p + 1 - xp->xp_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007401 // Certain options currently have special case handling to reuse the
7402 // expansion logic with other commands.
Doug Kearns6dfdff32023-08-27 18:48:51 +02007403#ifdef FEAT_SYN_HL
7404 if (options[opt_idx].var == (char_u *)&p_syn)
7405 {
7406 xp->xp_context = EXPAND_OWNSYNTAX;
7407 return;
7408 }
7409#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007410 if (options[opt_idx].var == (char_u *)&p_ft)
7411 {
7412 xp->xp_context = EXPAND_FILETYPE;
7413 return;
7414 }
Doug Kearns6dfdff32023-08-27 18:48:51 +02007415
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007416 // Now pick. If the option has a custom expander, use that. Otherwise, just
7417 // fill with the existing option value.
7418 if (expand_option_subtract)
7419 {
7420 xp->xp_context = EXPAND_SETTING_SUBTRACT;
7421 return;
7422 }
7423 else if (expand_option_idx >= 0 &&
7424 options[expand_option_idx].opt_expand_cb != NULL)
7425 {
7426 xp->xp_context = EXPAND_STRING_SETTING;
7427 }
7428 else if (*xp->xp_pattern == NUL)
7429 {
7430 xp->xp_context = EXPAND_OLD_SETTING;
7431 return;
7432 }
7433 else
7434 xp->xp_context = EXPAND_NOTHING;
7435
7436 if (is_term_option || (flags & P_NUM))
7437 return;
7438
7439 // Only string options below
7440
7441 // Options that have P_EXPAND are considered to all use file/dir expansion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 if (flags & P_EXPAND)
7443 {
7444 p = options[opt_idx].var;
7445 if (p == (char_u *)&p_bdir
7446 || p == (char_u *)&p_dir
7447 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007448 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451#ifdef FEAT_SESSION
7452 || p == (char_u *)&p_vdir
7453#endif
7454 )
7455 {
7456 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007457 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 xp->xp_backslash = XP_BS_THREE;
7459 else
7460 xp->xp_backslash = XP_BS_ONE;
7461 }
7462 else
7463 {
7464 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007465 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 if (p == (char_u *)&p_tags)
7467 xp->xp_backslash = XP_BS_THREE;
7468 else
7469 xp->xp_backslash = XP_BS_ONE;
7470 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02007471 if (flags & P_COMMA)
7472 xp->xp_backslash |= XP_BS_COMMA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 }
7474
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007475 // For an option that is a list of file names, or comma/colon-separated
7476 // values, split it by the delimiter and find the start of the current
7477 // pattern, while accounting for backslash-escaped space/commas/colons.
7478 // Triple-backslashed escaped file names (e.g. 'path') can also be
7479 // delimited by space.
7480 if ((flags & P_EXPAND) || (flags & P_COMMA) || (flags & P_COLON))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007482 for (p = arg + STRLEN(arg) - 1; p >= xp->xp_pattern; --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007484 // count number of backslashes before ' ' or ',' or ':'
7485 if (*p == ' ' || *p == ',' ||
7486 (*p == ':' && (flags & P_COLON)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007488 s = p;
7489 while (s > xp->xp_pattern && *(s - 1) == '\\')
7490 --s;
Yee Cheng Chin54844852023-10-09 18:12:31 +02007491 if ((*p == ' ' && ((xp->xp_backslash & XP_BS_THREE) && (p - s) < 3))
7492#if defined(BACKSLASH_IN_FILENAME)
7493 || (*p == ',' && (flags & P_COMMA) && (p - s) < 1)
7494#else
7495 || (*p == ',' && (flags & P_COMMA) && (p - s) < 2)
7496#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007497 || (*p == ':' && (flags & P_COLON)))
7498 {
7499 xp->xp_pattern = p + 1;
7500 break;
7501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 }
7503 }
7504 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007505
7506 // An option that is a list of single-character flags should always start
7507 // at the end as we don't complete words.
7508 if (flags & P_FLAGLIST)
7509 xp->xp_pattern = arg + STRLEN(arg);
7510
7511 // Some options can either be using file/dir expansions, or custom value
7512 // expansion depending on what the user typed. Unfortunately we have to
7513 // manually handle it here to make sure we have the correct xp_context set.
7514#ifdef FEAT_SPELL
7515 if (options[opt_idx].var == (char_u *)&p_sps)
7516 {
7517 if (STRNCMP(xp->xp_pattern, "file:", 5) == 0)
7518 {
7519 xp->xp_pattern += 5;
7520 return;
7521 }
7522 else if (options[expand_option_idx].opt_expand_cb != NULL)
7523 {
7524 xp->xp_context = EXPAND_STRING_SETTING;
7525 }
7526 }
7527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528}
7529
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007530/*
7531 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7532 *
7533 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7534 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7535 *
7536 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7537 * regular expression 'regmatch', then stores the match in matches[idx] and
7538 * returns TRUE.
7539 *
7540 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7541 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7542 *
7543 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7544 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7545 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02007546 static int
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007547match_str(
7548 char_u *str,
7549 regmatch_T *regmatch,
7550 char_u **matches,
7551 int idx,
7552 int test_only,
7553 int fuzzy,
7554 char_u *fuzzystr,
7555 fuzmatch_str_T *fuzmatch)
7556{
7557 if (!fuzzy)
7558 {
7559 if (vim_regexec(regmatch, str, (colnr_T)0))
7560 {
7561 if (!test_only)
7562 matches[idx] = vim_strsave(str);
7563 return TRUE;
7564 }
7565 }
7566 else
7567 {
7568 int score;
7569
7570 score = fuzzy_match_str(str, fuzzystr);
7571 if (score != 0)
7572 {
7573 if (!test_only)
7574 {
7575 fuzmatch[idx].idx = idx;
7576 fuzmatch[idx].str = vim_strsave(str);
7577 fuzmatch[idx].score = score;
7578 }
7579
7580 return TRUE;
7581 }
7582 }
7583
7584 return FALSE;
7585}
7586
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007588ExpandSettings(
7589 expand_T *xp,
7590 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007591 char_u *fuzzystr,
7592 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007593 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007594 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007596 int num_normal = 0; // Nr of matching non-term-code settings
7597 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 int opt_idx;
7599 int match;
7600 int count = 0;
7601 char_u *str;
7602 int loop;
7603 int is_term_opt;
7604 char_u name_buf[MAX_KEY_NAME_LEN];
7605 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007606 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007607 int fuzzy;
7608 fuzmatch_str_T *fuzmatch = NULL;
7609
Christian Brabandtcb747892022-05-08 21:10:56 +01007610 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007612 // do this loop twice:
7613 // loop == 0: count the number of matching options
7614 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 for (loop = 0; loop <= 1; ++loop)
7616 {
7617 regmatch->rm_ic = ic;
7618 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7619 {
K.Takataeeec2542021-06-02 13:28:16 +02007620 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007621 {
7622 if (match_str((char_u *)names[match], regmatch, *matches,
7623 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {
7625 if (loop == 0)
7626 num_normal++;
7627 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007628 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
7632 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7633 opt_idx++)
7634 {
7635 if (options[opt_idx].var == NULL)
7636 continue;
7637 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007638 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007640 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 if (is_term_opt && num_normal > 0)
7642 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007643
7644 if (match_str(str, regmatch, *matches, count, (loop == 0),
7645 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 {
7647 if (loop == 0)
7648 {
7649 if (is_term_opt)
7650 num_term++;
7651 else
7652 num_normal++;
7653 }
7654 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007655 count++;
7656 }
7657 else if (!fuzzy && options[opt_idx].shortname != NULL
7658 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007659 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007660 {
7661 // Compare against the abbreviated option name (for regular
7662 // expression match). Fuzzy matching (previous if) already
7663 // matches against both the expanded and abbreviated names.
7664 if (loop == 0)
7665 {
7666 if (is_term_opt)
7667 num_term++;
7668 else
7669 num_normal++;
7670 }
7671 else
7672 (*matches)[count++] = vim_strsave(str);
7673 }
7674 else if (is_term_opt)
7675 {
7676 name_buf[0] = '<';
7677 name_buf[1] = 't';
7678 name_buf[2] = '_';
7679 name_buf[3] = str[2];
7680 name_buf[4] = str[3];
7681 name_buf[5] = '>';
7682 name_buf[6] = NUL;
7683
7684 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7685 fuzzy, fuzzystr, fuzmatch))
7686 {
7687 if (loop == 0)
7688 num_term++;
7689 else
7690 count++;
7691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 }
7693 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007694
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007695 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7697 {
7698 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7699 {
Keith Thompson184f71c2024-01-04 21:19:04 +01007700 if (!SAFE_isprint(str[0]) || !SAFE_isprint(str[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 continue;
7702
7703 name_buf[0] = 't';
7704 name_buf[1] = '_';
7705 name_buf[2] = str[0];
7706 name_buf[3] = str[1];
7707 name_buf[4] = NUL;
7708
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007709 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007710 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007711 {
7712 if (loop == 0)
7713 num_term++;
7714 else
7715 count++;
7716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 else
7718 {
7719 name_buf[0] = '<';
7720 name_buf[1] = 't';
7721 name_buf[2] = '_';
7722 name_buf[3] = str[0];
7723 name_buf[4] = str[1];
7724 name_buf[5] = '>';
7725 name_buf[6] = NUL;
7726
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007727 if (match_str(name_buf, regmatch, *matches, count,
7728 (loop == 0), fuzzy, fuzzystr,
7729 fuzmatch))
7730 {
7731 if (loop == 0)
7732 num_term++;
7733 else
7734 count++;
7735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 }
7737 }
7738
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007739 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007740 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7742 {
7743 name_buf[0] = '<';
7744 STRCPY(name_buf + 1, str);
7745 STRCAT(name_buf, ">");
7746
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007747 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7748 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {
7750 if (loop == 0)
7751 num_term++;
7752 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007753 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 }
7755 }
7756 }
7757 if (loop == 0)
7758 {
7759 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007760 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007762 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 else
7764 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007765 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007767 *matches = ALLOC_MULT(char_u *, *numMatches);
7768 if (*matches == NULL)
7769 {
7770 *matches = (char_u **)"";
7771 return FAIL;
7772 }
7773 }
7774 else
7775 {
7776 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7777 if (fuzmatch == NULL)
7778 {
7779 *matches = (char_u **)"";
7780 return FAIL;
7781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
7783 }
7784 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007785
7786 if (fuzzy &&
7787 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7788 return FAIL;
7789
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 return OK;
7791}
7792
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007793// Escape an option value that can be used on the command-line with :set.
7794// Caller needs to free the returned string, unless NULL is returned.
7795 static char_u*
7796escape_option_str_cmdline(char_u *var)
7797{
7798 char_u *buf;
7799
7800 // A backslash is required before some characters. This is the reverse of
7801 // what happens in do_set().
7802 buf = vim_strsave_escaped(var, escape_chars);
7803 if (buf == NULL)
7804 return NULL;
7805
7806#ifdef BACKSLASH_IN_FILENAME
7807 // For MS-Windows et al. we don't double backslashes at the start and
7808 // before a file name character.
7809 // The reverse is found at stropt_copy_value().
7810 for (var = buf; *var != NUL; MB_PTR_ADV(var))
7811 if (var[0] == '\\' && var[1] == '\\'
7812 && expand_option_idx >= 0
7813 && (options[expand_option_idx].flags & P_EXPAND)
7814 && vim_isfilec(var[2])
7815 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
7816 STRMOVE(var, var + 1);
7817#endif
7818 return buf;
7819}
7820
7821/*
7822 * Expansion handler for :set= when we just want to fill in with the existing value.
7823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007825ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007827 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 char_u *buf;
7829
zeertzjqb0d45ec2023-01-25 15:04:22 +00007830 *numMatches = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007831 *matches = ALLOC_MULT(char_u *, 1);
zeertzjqb0d45ec2023-01-25 15:04:22 +00007832 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 return FAIL;
7834
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007835 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 if (expand_option_idx < 0)
7837 {
7838 var = find_termcode(expand_option_name + 2);
7839 if (var == NULL)
7840 expand_option_idx = findoption(expand_option_name);
7841 }
7842
7843 if (expand_option_idx >= 0)
7844 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007845 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 option_value2string(&options[expand_option_idx], expand_option_flags);
7847 var = NameBuff;
7848 }
7849 else if (var == NULL)
7850 var = (char_u *)"";
7851
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007852 buf = escape_option_str_cmdline(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 if (buf == NULL)
7854 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007855 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 return FAIL;
7857 }
7858
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007859 (*matches)[0] = buf;
zeertzjqb0d45ec2023-01-25 15:04:22 +00007860 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 return OK;
7862}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863
7864/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007865 * Expansion handler for :set=/:set+= when the option has a custom expansion handler.
7866 */
7867 int
7868ExpandStringSetting(
7869 expand_T *xp,
7870 regmatch_T *regmatch,
7871 int *numMatches,
7872 char_u ***matches)
7873{
7874 char_u *var = NULL; // init for GCC
7875 char_u *buf;
7876
7877 if (expand_option_idx < 0 ||
7878 options[expand_option_idx].opt_expand_cb == NULL)
7879 {
7880 // Not supposed to reach this. This function is only for options with
7881 // custom expansion callbacks.
7882 return FAIL;
7883 }
7884
7885 optexpand_T args;
7886 args.oe_varp = get_varp_scope(&options[expand_option_idx], expand_option_flags);
7887 args.oe_append = expand_option_append;
7888 args.oe_regmatch = regmatch;
7889 args.oe_xp = xp;
7890 args.oe_set_arg = xp->xp_line + expand_option_start_col;
7891 args.oe_include_orig_val =
7892 !expand_option_append &&
7893 (*args.oe_set_arg == NUL);
7894
7895 // Retrieve the existing value, but escape it as a reverse of setting it.
7896 // We technically only need to do this when oe_append or
7897 // oe_include_orig_val is true.
7898 option_value2string(&options[expand_option_idx], expand_option_flags);
7899 var = NameBuff;
7900 buf = escape_option_str_cmdline(var);
7901 if (buf == NULL)
7902 return FAIL;
7903
7904 args.oe_opt_value = buf;
7905
7906 int num_ret = options[expand_option_idx].opt_expand_cb(&args, numMatches, matches);
7907
7908 vim_free(buf);
7909 return num_ret;
7910}
7911
7912/*
7913 * Expansion handler for :set-=
7914 */
7915 int
7916ExpandSettingSubtract(
7917 expand_T *xp,
7918 regmatch_T *regmatch,
7919 int *numMatches,
7920 char_u ***matches)
7921{
7922 if (expand_option_idx < 0)
7923 // term option
7924 return ExpandOldSetting(numMatches, matches);
7925
7926 char_u *option_val = *(char_u**)get_option_varp_scope(
7927 expand_option_idx, expand_option_flags);
7928
7929 long_u option_flags = options[expand_option_idx].flags;
7930
7931 if (option_flags & P_NUM)
7932 return ExpandOldSetting(numMatches, matches);
7933 else if (option_flags & P_COMMA)
7934 {
7935 // Split the option by comma, then present each option to the user if
7936 // it matches the pattern.
7937 // This condition needs to go first, because 'whichwrap' has both
7938 // P_COMMA and P_FLAGLIST.
7939 garray_T ga;
7940
7941 char_u *item;
7942 char_u *option_copy;
7943 char_u *next_val;
7944 char_u *comma;
7945
7946 if (*option_val == NUL)
7947 return FAIL;
7948
7949 // Make a copy as we need to inject null characters destructively.
7950 option_copy = vim_strsave(option_val);
7951 if (option_copy == NULL)
7952 return FAIL;
7953 next_val = option_copy;
7954
7955 ga_init2(&ga, sizeof(char_u *), 10);
7956
7957 do
7958 {
7959 item = next_val;
7960 comma = vim_strchr(next_val, ',');
7961 while (comma != NULL && comma != next_val && *(comma - 1) == '\\')
7962 {
7963 // "\," is interpreted as a literal comma rather than option
7964 // separator when reading options in copy_option_part(). Skip
7965 // it.
7966 comma = vim_strchr(comma + 1, ',');
7967 }
7968 if (comma != NULL)
7969 {
7970 *comma = NUL; // null-terminate this value, required by later functions
7971 next_val = comma + 1;
7972 }
7973 else
7974 next_val = NULL;
7975
7976 if (*item == NUL)
7977 // empty value, don't add to list
7978 continue;
7979
7980 if (!vim_regexec(regmatch, item, (colnr_T)0))
7981 continue;
7982
7983 char_u *buf = escape_option_str_cmdline(item);
7984 if (buf == NULL)
7985 {
7986 vim_free(option_copy);
7987 ga_clear_strings(&ga);
7988 return FAIL;
7989 }
7990 if (ga_add_string(&ga, buf) != OK)
7991 {
7992 vim_free(buf);
7993 break;
7994 }
7995 } while (next_val != NULL);
7996
7997 vim_free(option_copy);
7998
7999 *matches = ga.ga_data;
8000 *numMatches = ga.ga_len;
8001 return OK;
8002 }
8003 else if (option_flags & P_FLAGLIST)
8004 {
8005 // Only present the flags that are set on the option as the other flags
8006 // are not meaningful to do set-= on.
8007
8008 if (*xp->xp_pattern != NUL)
8009 {
8010 // Don't suggest anything if cmdline is non-empty. Vim's set-=
8011 // behavior requires consecutive strings and it's usually
Viktor Szépedbf749b2023-10-16 09:53:37 +02008012 // unintuitive to users if they try to subtract multiple flags at
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008013 // once.
8014 return FAIL;
8015 }
8016
Yee Cheng Chin6d113472023-10-02 21:38:39 +02008017 size_t num_flags = STRLEN(option_val);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008018 if (num_flags == 0)
8019 return FAIL;
8020
8021 *matches = ALLOC_MULT(char_u *, num_flags + 1);
8022 if (*matches == NULL)
8023 return FAIL;
8024
8025 int count = 0;
8026 char_u *p;
8027
8028 p = vim_strsave(option_val);
8029 if (p == NULL)
8030 {
8031 VIM_CLEAR(*matches);
8032 return FAIL;
8033 }
8034 (*matches)[count++] = p;
8035
8036 if (num_flags > 1)
8037 {
8038 // If more than one flags, split the flags up and expose each
8039 // character as individual choice.
8040 for (char_u *flag = option_val; *flag != NUL; flag++)
8041 {
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02008042 p = vim_strnsave(flag, 1);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008043 if (p == NULL)
8044 break;
8045 (*matches)[count++] = p;
8046 }
8047 }
8048
8049 *numMatches = count;
8050 return OK;
8051 }
8052
8053 return ExpandOldSetting(numMatches, matches);
8054}
8055
8056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 * Get the value for the numeric or string option *opp in a nice format into
8058 * NameBuff[]. Must not be called with a hidden option!
8059 */
8060 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008061option_value2string(
8062 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008063 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064{
8065 char_u *varp;
8066
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008067 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068
8069 if (opp->flags & P_NUM)
8070 {
8071 long wc = 0;
8072
8073 if (wc_use_keyname(varp, &wc))
8074 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
8075 else if (wc != 0)
8076 STRCPY(NameBuff, transchar((int)wc));
8077 else
8078 sprintf((char *)NameBuff, "%ld", *(long *)varp);
8079 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008080 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {
8082 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008083 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 NameBuff[0] = NUL;
8085#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008086 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008087 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 STRCPY(NameBuff, "*****");
8089#endif
8090 else if (opp->flags & P_EXPAND)
8091 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008092 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 else if ((char_u **)opp->var == &p_pt)
8094 str2specialbuf(p_pt, NameBuff, MAXPATHL);
8095 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008096 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 }
8098}
8099
8100/*
8101 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
8102 * printed as a keyname.
8103 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
8104 */
8105 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008106wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
8109 {
8110 *wcp = *(long *)varp;
8111 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
8112 return TRUE;
8113 }
8114 return FALSE;
8115}
8116
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 * Return TRUE if "x" is present in 'shortmess' option, or
8119 * 'shortmess' contains 'a' and "x" is present in SHM_A.
8120 */
8121 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008122shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01008124 return p_shm != NULL &&
8125 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 || (vim_strchr(p_shm, 'a') != NULL
8127 && vim_strchr((char_u *)SHM_A, x) != NULL));
8128}
8129
8130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
8132 *
8133 * Reset 'compatible' and set the values for options that didn't get set yet
8134 * to the Vim defaults.
8135 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008136 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 */
8138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008139vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008141 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00008142 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008143 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144
8145 if (!option_was_set((char_u *)"cp"))
8146 {
8147 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02008148 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
8150 set_option_default(opt_idx, OPT_FREE, FALSE);
8151 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008152 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008154
8155 if (fname != NULL)
8156 {
8157 p = vim_getenv(envname, &dofree);
8158 if (p == NULL)
8159 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008160 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008161 p = FullName_save(fname, FALSE);
8162 if (p != NULL)
8163 {
8164 vim_setenv(envname, p);
8165 vim_free(p);
8166 }
8167 }
8168 else if (dofree)
8169 vim_free(p);
8170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171}
8172
8173/*
8174 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
8175 */
8176 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008177change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008179 int opt_idx;
8180
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 if (p_cp != on)
8182 {
8183 p_cp = on;
8184 compatible_set();
8185 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008186 opt_idx = findoption((char_u *)"cp");
8187 if (opt_idx >= 0)
8188 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189}
8190
8191/*
8192 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008193 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 */
8195 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008196option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197{
8198 int idx;
8199
8200 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008201 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 return FALSE;
8203 if (options[idx].flags & P_WAS_SET)
8204 return TRUE;
8205 return FALSE;
8206}
8207
8208/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008209 * Reset the flag indicating option "name" was set.
8210 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008211 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008212reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008213{
8214 int idx = findoption(name);
8215
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008216 if (idx < 0)
8217 return FAIL;
8218
8219 options[idx].flags &= ~P_WAS_SET;
8220 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008221}
8222
8223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 * compatible_set() - Called when 'compatible' has been set or unset.
8225 *
8226 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8227 * flag) to a Vi compatible value.
8228 * When 'compatible' is unset: Set all options that have a different default
8229 * for Vim (without the P_VI_DEF flag) to that default.
8230 */
8231 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008232compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
8234 int opt_idx;
8235
Bram Moolenaardac13472019-09-16 21:06:21 +02008236 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8238 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8239 set_option_default(opt_idx, OPT_FREE, p_cp);
8240 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008241 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242}
8243
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 * Check if backspacing over something is allowed.
8246 */
8247 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008248can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008249 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008251#ifdef FEAT_JOB_CHANNEL
8252 if (what == BS_START && bt_prompt(curbuf))
8253 return FALSE;
8254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 switch (*p_bs)
8256 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008257 case '3': return TRUE;
8258 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 case '1': return (what != BS_START);
8260 case '0': return FALSE;
8261 }
8262 return vim_strchr(p_bs, what) != NULL;
8263}
8264
8265/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008266 * Return the effective 'scrolloff' value for the current window, using the
8267 * global value when appropriate.
8268 */
8269 long
8270get_scrolloff_value(void)
8271{
8272 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8273}
8274
8275/*
8276 * Return the effective 'sidescrolloff' value for the current window, using the
8277 * global value when appropriate.
8278 */
8279 long
8280get_sidescrolloff_value(void)
8281{
8282 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8283}
8284
8285/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008286 * Get the local or global value of 'backupcopy'.
8287 */
8288 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008289get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008290{
8291 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8292}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008293
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008294#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008295/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008296 * Get the local or global value of 'formatlistpat'.
8297 */
8298 char_u *
8299get_flp_value(buf_T *buf)
8300{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008301 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8302 return p_flp;
8303 return buf->b_p_flp;
8304}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008305#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008306
8307/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008308 * Get the local or global value of the 'virtualedit' flags.
8309 */
8310 unsigned int
8311get_ve_flags(void)
8312{
Gary Johnson51ad8502021-08-03 18:33:08 +02008313 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8314 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008315}
8316
Bram Moolenaaree857022019-11-09 23:26:40 +01008317#if defined(FEAT_LINEBREAK) || defined(PROTO)
8318/*
8319 * Get the local or global value of 'showbreak'.
8320 */
8321 char_u *
8322get_showbreak_value(win_T *win)
8323{
8324 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8325 return p_sbr;
8326 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8327 return empty_option;
8328 return win->w_p_sbr;
8329}
8330#endif
8331
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008332#if defined(FEAT_EVAL) || defined(PROTO)
8333/*
8334 * Get window or buffer local options.
8335 */
8336 dict_T *
8337get_winbuf_options(int bufopt)
8338{
8339 dict_T *d;
8340 int opt_idx;
8341
8342 d = dict_alloc();
8343 if (d == NULL)
8344 return NULL;
8345
Bram Moolenaardac13472019-09-16 21:06:21 +02008346 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008347 {
8348 struct vimoption *opt = &options[opt_idx];
8349
8350 if ((bufopt && (opt->indir & PV_BUF))
8351 || (!bufopt && (opt->indir & PV_WIN)))
8352 {
8353 char_u *varp = get_varp(opt);
8354
8355 if (varp != NULL)
8356 {
8357 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008358 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008359 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008360 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008361 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008362 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008363 }
8364 }
8365 }
8366
8367 return d;
8368}
8369#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008370
Bram Moolenaardac13472019-09-16 21:06:21 +02008371#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008372/*
8373 * This is called when 'culopt' is changed
8374 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008375 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008376fill_culopt_flags(char_u *val, win_T *wp)
8377{
8378 char_u *p;
8379 char_u culopt_flags_new = 0;
8380
8381 if (val == NULL)
8382 p = wp->w_p_culopt;
8383 else
8384 p = val;
8385 while (*p != NUL)
8386 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008387 // Note: Keep this in sync with p_culopt_values.
Bram Moolenaar017ba072019-09-14 21:01:23 +02008388 if (STRNCMP(p, "line", 4) == 0)
8389 {
8390 p += 4;
8391 culopt_flags_new |= CULOPT_LINE;
8392 }
8393 else if (STRNCMP(p, "both", 4) == 0)
8394 {
8395 p += 4;
8396 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8397 }
8398 else if (STRNCMP(p, "number", 6) == 0)
8399 {
8400 p += 6;
8401 culopt_flags_new |= CULOPT_NBR;
8402 }
8403 else if (STRNCMP(p, "screenline", 10) == 0)
8404 {
8405 p += 10;
8406 culopt_flags_new |= CULOPT_SCRLINE;
8407 }
8408
8409 if (*p != ',' && *p != NUL)
8410 return FAIL;
8411 if (*p == ',')
8412 ++p;
8413 }
8414
8415 // Can't have both "line" and "screenline".
8416 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8417 return FAIL;
8418 wp->w_p_culopt_flags = culopt_flags_new;
8419
8420 return OK;
8421}
8422#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008423
8424/*
8425 * Get the value of 'magic' adjusted for Vim9 script.
8426 */
8427 int
8428magic_isset(void)
8429{
8430 switch (magic_overruled)
8431 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008432 case OPTION_MAGIC_ON: return TRUE;
8433 case OPTION_MAGIC_OFF: return FALSE;
8434 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008435 }
8436#ifdef FEAT_EVAL
8437 if (in_vim9script())
8438 return TRUE;
8439#endif
8440 return p_magic;
8441}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008442
8443/*
8444 * Set the callback function value for an option that accepts a function name,
8445 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8446 * Returns OK if the option is successfully set to a function, otherwise
8447 * returns FAIL.
8448 */
8449 int
8450option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8451{
8452#ifdef FEAT_EVAL
8453 typval_T *tv;
8454 callback_T cb;
8455
8456 if (optval == NULL || *optval == NUL)
8457 {
8458 free_callback(optcb);
8459 return OK;
8460 }
8461
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008462 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008463 || (STRNCMP(optval, "function(", 9) == 0)
8464 || (STRNCMP(optval, "funcref(", 8) == 0))
8465 // Lambda expression or a funcref
8466 tv = eval_expr(optval, NULL);
8467 else
8468 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008469 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008470 if (tv == NULL)
8471 return FAIL;
8472
8473 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008474 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008475 {
8476 free_tv(tv);
8477 return FAIL;
8478 }
8479
8480 free_callback(optcb);
8481 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008482 if (cb.cb_free_name)
8483 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008484 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008485
8486 // when using Vim9 style "import.funcname" it needs to be expanded to
8487 // "import#funcname".
8488 expand_autload_callback(optcb);
8489
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008490 return OK;
8491#else
8492 return FAIL;
8493#endif
8494}
Christian Brabandt757593c2023-08-22 21:44:10 +02008495
8496#if defined(FEAT_EVAL) || defined(PROTO)
8497 static void
8498didset_options_sctx(int opt_flags, char **buf)
8499{
8500 for (int i = 0; ; ++i)
8501 {
8502 if (buf[i] == NULL)
8503 break;
8504
8505 int idx = findoption((char_u *)buf[i]);
8506 if (idx >= 0)
8507 set_option_sctx_idx(idx, opt_flags, current_sctx);
8508 }
8509}
8510#endif