blob: f0e77270a49375fe7573f3a28194727dbed1f31a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000013 * - Put it in the options array in optiondefs.h (copy an existing entry).
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000016 * - Add a PV_XX macro definition to the optiondefs.h file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000019 * - For a window string option, add code to check_win_options() and
20 * clear_winopt().
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 * - For a buffer option, add some code to buf_copy_options().
22 * - For a buffer string option, add code to check_buf_options().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000023 * - If it's a numeric option, add any necessary bounds checks to
24 * set_num_option().
25 * - If it's a list of flags, add some code in did_set_string_option(), search
26 * for WW_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 * - When adding an option with expansion (P_EXPAND), but with a different
28 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020029 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * options.txt, and any other related places.
31 * - Add an entry in runtime/optwin.vim.
32 * When making changes:
33 * - Adjust the help for the option in doc/option.txt.
34 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
35 * comment at the help for the 'compatible' option.
36 */
37
38#define IN_OPTION_C
39#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020040#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010043static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static char_u *option_expand(int opt_idx, char_u *val);
46static void didset_options(void);
47static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000050#else
51# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
52#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010053static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
54static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020055static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020057static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010059static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
61static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020062static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000063static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000066static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void check_winopt(winopt_T *wop);
68static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Christian Brabandt757593c2023-08-22 21:44:10 +020071#if defined(FEAT_EVAL) || defined(PROTO)
72static char *(p_bin_dep_opts[]) = {"textwidth", "wrapmargin", "modeline", "expandtab", NULL};
73static char *(p_paste_dep_opts[]) = {"autoindent", "expandtab", "ruler", "showmatch", "smarttab",
74 "softtabstop", "textwidth", "wrapmargin",
75#ifdef FEAT_RIGHTLEFT
76 "hkmap", "revins",
77#endif
78#ifdef FEAT_VARTABS
79 "varsofttabstop",
80#endif
81 NULL};
82static void didset_options_sctx(int opt_flags, char **buf);
83#endif
84
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086/*
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000087 * Initialize the 'shell' option to a default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000089 static void
90set_init_default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000091{
92 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000094 // Find default value for 'shell' option.
95 // Don't use it if it is empty.
Bram Moolenaar7c626922005-02-07 22:01:03 +000096 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010097#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +000098 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010099 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200102#if defined(MSWIN)
103 {
104 // For MS-Windows put the path in quotes instead of escaping spaces.
105 char_u *cmd;
106 size_t len;
107
108 if (vim_strchr(p, ' ') != NULL)
109 {
110 len = STRLEN(p) + 3; // two quotes and a trailing NUL
111 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200112 if (cmd != NULL)
113 {
114 vim_snprintf((char *)cmd, len, "\"%s\"", p);
115 set_string_default("sh", cmd);
116 vim_free(cmd);
117 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200118 }
119 else
120 set_string_default("sh", p);
121 }
122#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100123 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200124#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000125}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000127/*
128 * Set the default for 'backupskip' to include environment variables for
129 * temp files.
130 */
131 static void
132set_init_default_backupskip(void)
133{
134 int opt_idx;
135 long_u n;
136 char_u *p;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100137#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000138 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100139#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000140 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100141#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000142 int len;
143 garray_T ga;
Bram Moolenaar14338022023-03-15 22:05:44 +0000144 char_u *item;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200145
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000146 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000148 ga_init2(&ga, 1, 100);
149 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
150 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000151 int mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100152#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000153 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100154# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000155 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100156# else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000157 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000159 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100160#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000161 p = vim_getenv((char_u *)names[n], &mustfree);
162 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000164 // First time count the NUL, otherwise count the ','.
165 len = (int)STRLEN(p) + 3;
166 item = alloc(len);
Bram Moolenaar14338022023-03-15 22:05:44 +0000167 if (item != NULL)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000168 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000169 STRCPY(item, p);
170 add_pathsep(item);
171 STRCAT(item, "*");
172 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
173 == NULL
174 && ga_grow(&ga, len) == OK)
175 {
176 if (ga.ga_len > 0)
177 STRCAT(ga.ga_data, ",");
178 STRCAT(ga.ga_data, item);
179 ga.ga_len += len;
180 }
181 vim_free(item);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000184 if (mustfree)
185 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000187 if (ga.ga_data != NULL)
188 {
189 set_string_default("bsk", ga.ga_data);
190 vim_free(ga.ga_data);
191 }
192}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000194/*
195 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
196 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
197 */
198 static void
199set_init_default_maxmemtot(void)
200{
201 int opt_idx;
202 long_u n;
203
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000205 if (opt_idx < 0)
206 return;
207
208#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
209 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
ichizok02560422022-04-05 14:18:44 +0100212#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000213 // Use amount of memory available at this moment.
214 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100215#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000216 // Use amount of memory available to Vim.
217 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100218#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000219 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000221 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
222 opt_idx = findoption((char_u *)"maxmem");
223 if (opt_idx >= 0)
224 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000225#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000226 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
227 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000228#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000229 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000232}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000234/*
235 * Initialize the 'cdpath' option to a default value.
236 */
237 static void
238set_init_default_cdpath(void)
239{
240 int opt_idx;
241 char_u *cdpath;
242 char_u *buf;
243 int i;
244 int j;
245 int mustfree = FALSE;
246
247 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
248 if (cdpath == NULL)
249 return;
250
251 buf = alloc((STRLEN(cdpath) << 1) + 2);
252 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000254 buf[0] = ','; // start with ",", current dir first
255 j = 1;
256 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000258 if (vim_ispathlistsep(cdpath[i]))
259 buf[j++] = ',';
260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000262 if (cdpath[i] == ' ' || cdpath[i] == ',')
263 buf[j++] = '\\';
264 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000267 buf[j] = NUL;
268 opt_idx = findoption((char_u *)"cdpath");
269 if (opt_idx >= 0)
270 {
271 options[opt_idx].def_val[VI_DEFAULT] = buf;
272 options[opt_idx].flags |= P_DEF_ALLOCED;
273 }
274 else
275 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000277 if (mustfree)
278 vim_free(cdpath);
279}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000281/*
282 * Initialize the 'printencoding' option to a default value.
283 */
284 static void
285set_init_default_printencoding(void)
286{
ichizok02560422022-04-05 14:18:44 +0100287#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000288 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100289 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100291# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000292 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100293# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000294 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100295# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000296 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100297# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000298 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000300 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000302}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
304#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000305/*
306 * Initialize the 'printexpr' option to a default value.
307 */
308 static void
309set_init_default_printexpr(void)
310{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100311 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100313# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000314 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100315# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
317
ichizok02560422022-04-05 14:18:44 +0100318# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# endif
321 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000322}
323#endif
324
325#ifdef UNIX
326/*
327 * Force restricted-mode on for "nologin" or "false" $SHELL
328 */
329 static void
330set_init_restricted_mode(void)
331{
332 char_u *p;
333
334 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000335 if (p == NULL)
336 return;
337 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
338 restricted = TRUE;
339 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000340}
341#endif
342
343#ifdef CLEAN_RUNTIMEPATH
344/*
345 * When Vim is started with the "--clean" argument, set the default value
346 * for the 'runtimepath' and 'packpath' options.
347 */
348 static void
349set_init_clean_rtp(void)
350{
351 int opt_idx;
352
353 opt_idx = findoption((char_u *)"runtimepath");
354 if (opt_idx >= 0)
355 {
356 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
357 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
358 }
359 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000360 if (opt_idx < 0)
361 return;
362 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
363 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000364}
365#endif
366
367/*
368 * Expand environment variables and things like "~" for the defaults.
369 * If option_expand() returns non-NULL the variable is expanded. This can
370 * only happen for non-indirect options.
371 * Also set the default to the expanded value, so ":set" does not list
372 * them.
373 * Don't set the P_ALLOCED flag, because we don't want to free the
374 * default.
375 */
376 static void
377set_init_expand_env(void)
378{
379 int opt_idx;
380 char_u *p;
381
382 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
383 {
384 if ((options[opt_idx].flags & P_GETTEXT)
385 && options[opt_idx].var != NULL)
386 p = (char_u *)_(*(char **)options[opt_idx].var);
387 else
388 p = option_expand(opt_idx, NULL);
389 if (p != NULL && (p = vim_strsave(p)) != NULL)
390 {
391 *(char_u **)options[opt_idx].var = p;
392 // VIMEXP
393 // Defaults for all expanded options are currently the same for Vi
394 // and Vim. When this changes, add some code here! Also need to
395 // split P_DEF_ALLOCED in two.
396 if (options[opt_idx].flags & P_DEF_ALLOCED)
397 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
398 options[opt_idx].def_val[VI_DEFAULT] = p;
399 options[opt_idx].flags |= P_DEF_ALLOCED;
400 }
401 }
402}
403
404/*
405 * Initialize the 'LANG' environment variable to a default value.
406 */
407 static void
408set_init_lang_env(void)
409{
410#if defined(MSWIN) && defined(FEAT_GETTEXT)
411 // If $LANG isn't set, try to get a good value for it. This makes the
412 // right language be used automatically. Don't do this for English.
413 if (mch_getenv((char_u *)"LANG") == NULL)
414 {
415 char buf[20];
416 long_u n;
417
418 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
419 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
420 // only the first two.
421 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
422 (LPTSTR)buf, 20);
423 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
424 {
425 // There are a few exceptions (probably more)
426 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
427 STRCPY(buf, "zh_TW");
428 else if (STRNICMP(buf, "chs", 3) == 0
429 || STRNICMP(buf, "zhc", 3) == 0)
430 STRCPY(buf, "zh_CN");
431 else if (STRNICMP(buf, "jp", 2) == 0)
432 STRCPY(buf, "ja");
433 else
434 buf[2] = NUL; // truncate to two-letter code
435 vim_setenv((char_u *)"LANG", (char_u *)buf);
436 }
437 }
438#elif defined(MACOS_CONVERT)
439 // Moved to os_mac_conv.c to avoid dependency problems.
440 mac_lang_init();
441#endif
442}
443
444/*
445 * Initialize the 'encoding' option to a default value.
446 */
447 static void
448set_init_default_encoding(void)
449{
450 char_u *p;
451 int opt_idx;
452
Igor Todorovski497e5282024-01-12 17:59:18 +0100453# if defined(MSWIN) || defined(__MVS__)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000454 // MS-Windows has builtin support for conversion to and from Unicode, using
455 // "utf-8" for 'encoding' should work best for most users.
Igor Todorovski497e5282024-01-12 17:59:18 +0100456 // z/OS built should default to UTF-8 mode as setlocale does not respect utf-8 environment variable locales
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000457 p = vim_strsave((char_u *)ENC_DFLT);
458# else
459 // enc_locale() will try to find the encoding of the current locale.
460 // This works best for properly configured systems, old and new.
461 p = enc_locale();
462# endif
463 if (p == NULL)
464 return;
465
466 // Try setting 'encoding' and check if the value is valid.
467 // If not, go back to the default encoding.
468 char_u *save_enc = p_enc;
469 p_enc = p;
470 if (STRCMP(p_enc, "gb18030") == 0)
471 {
472 // We don't support "gb18030", but "cp936" is a good substitute
473 // for practical purposes, thus use that. It's not an alias to
474 // still support conversion between gb18030 and utf-8.
475 p_enc = vim_strsave((char_u *)"cp936");
476 vim_free(p);
477 }
478 if (mb_init() == NULL)
479 {
480 opt_idx = findoption((char_u *)"encoding");
481 if (opt_idx >= 0)
482 {
483 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
484 options[opt_idx].flags |= P_DEF_ALLOCED;
485 }
486
487#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
488 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
489 {
490 // Adjust the default for 'isprint' and 'iskeyword' to match
491 // latin1. Also set the defaults for when 'nocompatible' is
492 // set.
493 set_string_option_direct((char_u *)"isp", -1,
494 ISP_LATIN1, OPT_FREE, SID_NONE);
495 set_string_option_direct((char_u *)"isk", -1,
496 ISK_LATIN1, OPT_FREE, SID_NONE);
497 opt_idx = findoption((char_u *)"isp");
498 if (opt_idx >= 0)
499 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
500 opt_idx = findoption((char_u *)"isk");
501 if (opt_idx >= 0)
502 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
503 (void)init_chartab();
504 }
505#endif
506
507#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
508 // Win32 console: When GetACP() returns a different value from
509 // GetConsoleCP() set 'termencoding'.
510 if (
511# ifdef VIMDLL
512 (!gui.in_use && !gui.starting) &&
513# endif
514 GetACP() != GetConsoleCP())
515 {
516 char buf[50];
517
518 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
519 // Use an alternative value.
520 if (GetConsoleCP() == 0)
521 sprintf(buf, "cp%ld", (long)GetACP());
522 else
523 sprintf(buf, "cp%ld", (long)GetConsoleCP());
524 p_tenc = vim_strsave((char_u *)buf);
525 if (p_tenc != NULL)
526 {
527 opt_idx = findoption((char_u *)"termencoding");
528 if (opt_idx >= 0)
529 {
530 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
531 options[opt_idx].flags |= P_DEF_ALLOCED;
532 }
533 convert_setup(&input_conv, p_tenc, p_enc);
534 convert_setup(&output_conv, p_enc, p_tenc);
535 }
536 else
537 p_tenc = empty_option;
538 }
539#endif
540#if defined(MSWIN)
541 // $HOME may have characters in active code page.
542 init_homedir();
543#endif
544 }
545 else
546 {
547 vim_free(p_enc);
548 p_enc = save_enc;
549 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000550}
551
552/*
553 * Initialize the options, first part.
554 *
555 * Called only once from main(), just after creating the first buffer.
556 * If "clean_arg" is TRUE Vim was started with --clean.
557 */
558 void
559set_init_1(int clean_arg)
560{
561#ifdef FEAT_LANGMAP
562 langmap_init();
563#endif
564
565 // Be Vi compatible by default
566 p_cp = TRUE;
567
568 // Use POSIX compatibility when $VIM_POSIX is set.
569 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
570 {
571 set_string_default("cpo", (char_u *)CPO_ALL);
572 set_string_default("shm", (char_u *)SHM_POSIX);
573 }
574
575 set_init_default_shell();
576 set_init_default_backupskip();
577 set_init_default_maxmemtot();
578 set_init_default_cdpath();
579 set_init_default_printencoding();
580#ifdef FEAT_POSTSCRIPT
581 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582#endif
583
584 /*
585 * Set all the options (except the terminal options) to their default
586 * value. Also set the global value for local options.
587 */
588 set_options_default(0);
589
matveytadbb1bf2022-02-01 17:26:12 +0000590#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000591 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000592#endif
593
Bram Moolenaar07268702018-03-01 21:57:32 +0100594#ifdef CLEAN_RUNTIMEPATH
595 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000596 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100597#endif
598
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599#ifdef FEAT_GUI
600 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100601 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602#endif
603
604 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100606 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 check_buf_options(curbuf);
608 check_win_options(curwin);
609 check_options();
610
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 didset_options();
613
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000614#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100615 // Use the current chartab for the generic chartab. This is not in
616 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000617 init_spell_chartab();
618#endif
619
K.Takatace3189d2023-02-15 19:13:43 +0000620 set_init_default_encoding();
621
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000622 // Expand environment variables and things like "~" for the defaults.
623 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100625 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100628 // Detect use of mlterm.
629 // Mlterm is a terminal emulator akin to xterm that has some special
630 // abilities (bidi namely).
631 // NOTE: mlterm's author is being asked to 'set' a variable
632 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100634 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635#endif
636
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200637 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000639 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100642 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 set_helplang_default(get_mess_lang());
644#endif
645}
646
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200647static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
648
649/*
650 * Set the "fileencodings" option to the default value for when 'encoding' is
651 * utf-8.
652 */
653 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000654set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200655{
656 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
657 OPT_FREE, 0);
658}
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660/*
661 * Set an option to its default value.
662 * This does not take care of side effects!
663 */
664 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100665set_option_default(
666 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100667 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
668 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 char_u *varp; // pointer to variable for current option
671 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000673 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
675
676 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
677 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100678 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
680 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
681 if (flags & P_STRING)
682 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200683 // 'fencs' default value depends on 'encoding'
684 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
685 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100686 // Use set_string_option_direct() for local options to handle
687 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200688 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200689 set_string_option_direct(NULL, opt_idx,
690 options[opt_idx].def_val[dvi], opt_flags, 0);
691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200693 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
694 free_string_option(*(char_u **)(varp));
695 *(char_u **)varp = options[opt_idx].def_val[dvi];
696 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 }
698 }
699 else if (flags & P_NUM)
700 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000701 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 win_comp_scroll(curwin);
703 else
704 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100705 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
706
707 if ((long *)varp == &curwin->w_p_so
708 || (long *)varp == &curwin->w_p_siso)
709 // 'scrolloff' and 'sidescrolloff' local values have a
710 // different default value than the global default.
711 *(long *)varp = -1;
712 else
713 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100714 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (both)
716 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100717 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100720 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100722 // the cast to long is required for Manx C, long_i is needed for
723 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000724 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000725#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100726 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000727 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
728 *(int *)varp = FALSE;
729#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100730 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 if (both)
732 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
733 *(int *)varp;
734 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000735
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100736 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000737 flagsp = insecure_flag(opt_idx, opt_flags);
738 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
740
741#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200742 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743#endif
744}
745
746/*
747 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200748 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 */
750 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100751set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100752 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753{
754 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000756 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
Bram Moolenaardac13472019-09-16 21:06:21 +0200758 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200759 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200760 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100761 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200762# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200763 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200764 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200765# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100766 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 set_option_default(i, opt_flags, p_cp);
768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100769 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000770 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200772 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773}
774
775/*
776 * Set the Vi-default value of a string option.
777 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100778 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100780 static void
781set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
783 char_u *p;
784 int opt_idx;
785
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100786 if (escape && vim_strchr(val, ' ') != NULL)
787 p = vim_strsave_escaped(val, (char_u *)" ");
788 else
789 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000790 if (p == NULL) // we don't want a NULL
791 return;
792
793 opt_idx = findoption((char_u *)name);
794 if (opt_idx < 0)
795 return;
796
797 if (options[opt_idx].flags & P_DEF_ALLOCED)
798 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
799 options[opt_idx].def_val[VI_DEFAULT] = p;
800 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801}
802
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100803 void
804set_string_default(char *name, char_u *val)
805{
806 set_string_default_esc(name, val, FALSE);
807}
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200810 * For an option value that contains comma separated items, find "newval" in
811 * "origval". Return NULL if not found.
812 */
813 static char_u *
814find_dup_item(char_u *origval, char_u *newval, long_u flags)
815{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200816 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200817 size_t newlen;
818 char_u *s;
819
820 if (origval == NULL)
821 return NULL;
822
823 newlen = STRLEN(newval);
824 for (s = origval; *s != NUL; ++s)
825 {
826 if ((!(flags & P_COMMA)
827 || s == origval
828 || (s[-1] == ',' && !(bs & 1)))
829 && STRNCMP(s, newval, newlen) == 0
830 && (!(flags & P_COMMA)
831 || s[newlen] == ','
832 || s[newlen] == NUL))
833 return s;
834 // Count backslashes. Only a comma with an even number of backslashes
835 // or a single backslash preceded by a comma before it is recognized as
836 // a separator.
837 if ((s > origval + 1
838 && s[-1] == '\\'
839 && s[-2] != ',')
840 || (s == origval + 1
841 && s[-1] == '\\'))
842 ++bs;
843 else
844 bs = 0;
845 }
846 return NULL;
847}
848
849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 * Set the Vi-default value of a number option.
851 * Used for 'lines' and 'columns'.
852 */
853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100854set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000856 int opt_idx;
857
858 opt_idx = findoption((char_u *)name);
859 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000860 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861}
862
Dominique Pelle748b3082022-01-08 12:41:16 +0000863#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200864/*
865 * Set all window-local and buffer-local options to the Vim default.
866 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200867 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200868 */
869 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200870set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200871{
872 win_T *save_curwin = curwin;
873 int i;
874
875 curwin = wp;
876 curbuf = curwin->w_buffer;
877 block_autocmds();
878
Bram Moolenaardac13472019-09-16 21:06:21 +0200879 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200880 {
881 struct vimoption *p = &(options[i]);
882 char_u *varp = get_varp_scope(p, OPT_LOCAL);
883
884 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200885 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200886 && !(options[i].flags & P_NODEFAULT)
887 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200888 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200889 }
890
891 unblock_autocmds();
892 curwin = save_curwin;
893 curbuf = curwin->w_buffer;
894}
Dominique Pelle748b3082022-01-08 12:41:16 +0000895#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200896
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000897#if defined(EXITFREE) || defined(PROTO)
898/*
899 * Free all options.
900 */
901 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100902free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000903{
904 int i;
905
Bram Moolenaardac13472019-09-16 21:06:21 +0200906 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000907 {
908 if (options[i].indir == PV_NONE)
909 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100910 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100911 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000912 free_string_option(*(char_u **)options[i].var);
913 if (options[i].flags & P_DEF_ALLOCED)
914 free_string_option(options[i].def_val[VI_DEFAULT]);
915 }
916 else if (options[i].var != VAR_WIN
917 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200919 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000920 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000921 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000922 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000923}
924#endif
925
926
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927/*
928 * Initialize the options, part two: After getting Rows and Columns and
929 * setting 'term'.
930 */
931 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100932set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000934 int idx;
935
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000936 // 'scroll' defaults to half the window height. The stored default is zero,
937 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000938 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000939 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000940 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 comp_col();
942
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000943 // 'window' is only for backwards compatibility with Vi.
944 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +0000945 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000946 p_window = Rows - 1;
947 set_number_default("window", Rows - 1);
948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100949 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100950#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000951 // If 'background' wasn't set by the user, try guessing the value,
952 // depending on the terminal name. Only need to check for terminals
953 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000954 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000955 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
956 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000958 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100959 // don't mark it as set, when starting the GUI it may be
960 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000961 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
963#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000964
965#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000966 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000967#endif
968#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000969 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000970#endif
971#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000972 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974}
975
976/*
977 * Initialize the options, part three: After reading the .vimrc
978 */
979 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100980set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100982#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983/*
984 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
985 * This is done after other initializations, where 'shell' might have been
986 * set, but only if they have not been set before.
987 */
988 char_u *p;
989 int idx_srr;
990 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100991# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 int idx_sp;
993 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000997 if (idx_srr < 0)
998 do_srr = FALSE;
999 else
1000 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001001# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001003 if (idx_sp < 0)
1004 do_sp = FALSE;
1005 else
1006 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001007# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001008 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (p != NULL)
1010 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001011 // Default for p_sp is "| tee", for p_srr is ">".
1012 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if ( fnamecmp(p, "csh") == 0
1014 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001015# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 || fnamecmp(p, "csh.exe") == 0
1017 || fnamecmp(p, "tcsh.exe") == 0
1018# endif
1019 )
1020 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001021# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (do_sp)
1023 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001024# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001026# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1030 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (do_srr)
1033 {
1034 p_srr = (char_u *)">&";
1035 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1036 }
1037 }
Mike Williams12795022021-06-28 20:53:58 +02001038# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001039 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1040 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001041 else if ( fnamecmp(p, "powershell") == 0
1042 || fnamecmp(p, "powershell.exe") == 0
1043 )
1044 {
1045# if defined(FEAT_QUICKFIX)
1046 if (do_sp)
1047 {
1048 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1049 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1050 }
1051# endif
1052 if (do_srr)
1053 {
1054 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1055 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1056 }
1057 }
1058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 else
Natanael Copa56318362021-05-06 18:46:35 +02001060 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if ( fnamecmp(p, "sh") == 0
1062 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001063 || fnamecmp(p, "mksh") == 0
1064 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001066 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001068 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001069 || fnamecmp(p, "ash") == 0
1070 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001071 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001072# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 || fnamecmp(p, "cmd") == 0
1074 || fnamecmp(p, "sh.exe") == 0
1075 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001076 || fnamecmp(p, "mksh.exe") == 0
1077 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001079 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 || fnamecmp(p, "bash.exe") == 0
1081 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001082 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001083 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001085 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001087# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (do_sp)
1089 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001090# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001092# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001093 if (fnamecmp(p, "pwsh") == 0)
1094 p_sp = (char_u *)">%s 2>&1";
1095 else
1096 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1099 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001100# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 if (do_srr)
1102 {
1103 p_srr = (char_u *)">%s 2>&1";
1104 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1105 }
1106 }
1107 vim_free(p);
1108 }
1109#endif
1110
Bram Moolenaar4f974752019-02-17 17:44:42 +01001111#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001113 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1114 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001116 * set, but only if they have not been set before.
1117 * Default values depend on shell (cmd.exe is default shell):
1118 *
1119 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001120 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001121 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001122 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001123 * "sh" like shells - "-c" "\""
1124 *
1125 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 */
Mike Williams12795022021-06-28 20:53:58 +02001127 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1128 {
1129 int idx_opt;
1130
1131 idx_opt = findoption((char_u *)"shcf");
1132 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1133 {
1134 p_shcf = (char_u*)"-Command";
1135 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1136 }
1137
1138 idx_opt = findoption((char_u *)"sxq");
1139 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1140 {
1141 p_sxq = (char_u*)"\"";
1142 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1143 }
1144 }
1145 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 {
1147 int idx3;
1148
1149 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001150 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
1152 p_shcf = (char_u *)"-c";
1153 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1154 }
1155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001156 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001158 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 p_sxq = (char_u *)"\"";
1161 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001164 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1165 {
1166 int idx3;
1167
1168 /*
1169 * cmd.exe on Windows will strip the first and last double quote given
1170 * on the command line, e.g. most of the time things like:
1171 * cmd /c "my path/to/echo" "my args to echo"
1172 * become:
1173 * my path/to/echo" "my args to echo
1174 * when executed.
1175 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001176 * To avoid this, set shellxquote to surround the command in
1177 * parenthesis. This appears to make most commands work, without
1178 * breaking commands that worked previously, such as
1179 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001180 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001181 idx3 = findoption((char_u *)"sxq");
1182 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1183 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001184 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001185 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1186 }
1187
Bram Moolenaara64ba222012-02-12 23:23:31 +01001188 idx3 = findoption((char_u *)"shcf");
1189 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1190 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001191 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001192 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1193 }
1194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#endif
1196
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001197 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001198 {
1199 int idx_ffs = findoption((char_u *)"ffs");
1200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001201 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001202 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1203 set_fileformat(default_fileformat(), OPT_LOCAL);
1204 }
1205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207}
1208
1209#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1210/*
1211 * When 'helplang' is still at its default value, set it to "lang".
1212 * Only the first two characters of "lang" are used.
1213 */
1214 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001215set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216{
1217 int idx;
1218
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001219 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 return;
1221 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001222 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1223 return;
1224
1225 if (options[idx].flags & P_ALLOCED)
1226 free_string_option(p_hlg);
1227 p_hlg = vim_strsave(lang);
1228 if (p_hlg == NULL)
1229 p_hlg = empty_option;
1230 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001232 // zh_CN becomes "cn", zh_TW becomes "tw"
1233 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001234 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001235 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1236 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001237 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001238 // any C like setting, such as C.UTF-8, becomes "en"
1239 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1240 {
1241 p_hlg[0] = 'e';
1242 p_hlg[1] = 'n';
1243 }
1244 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001246 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247}
1248#endif
1249
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250/*
1251 * 'title' and 'icon' only default to true if they have not been set or reset
1252 * in .vimrc and we can read the old value.
1253 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1254 * they can be reset. This reduces startup time when using X on a remote
1255 * machine.
1256 */
1257 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001258set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259{
1260 int idx1;
1261 long val;
1262
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001263 // If GUI is (going to be) used, we can always set the window title and
1264 // icon name. Saves a bit of time, because the X11 display server does
1265 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001267 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
1269#ifdef FEAT_GUI
1270 if (gui.starting || gui.in_use)
1271 val = TRUE;
1272 else
1273#endif
1274 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001275 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 p_title = val;
1277 }
1278 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001279 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001281 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001283
1284#ifdef FEAT_GUI
1285 if (gui.starting || gui.in_use)
1286 val = TRUE;
1287 else
1288#endif
1289 val = mch_can_restore_icon();
1290 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1291 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001294 void
1295ex_set(exarg_T *eap)
1296{
1297 int flags = 0;
1298
1299 if (eap->cmdidx == CMD_setlocal)
1300 flags = OPT_LOCAL;
1301 else if (eap->cmdidx == CMD_setglobal)
1302 flags = OPT_GLOBAL;
1303#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001304 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001305 ex_options(eap);
1306 else
1307#endif
1308 {
1309 if (eap->forceit)
1310 flags |= OPT_ONECOLUMN;
1311 (void)do_set(eap->arg, flags);
1312 }
1313}
1314
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001315/*
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001316 * :set boolean option prefix
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001317 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001318typedef enum {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001319 PREFIX_NO = 0, // "no" prefix
1320 PREFIX_NONE, // no prefix
1321 PREFIX_INV, // "inv" prefix
1322} set_prefix_T;
1323
1324/*
1325 * Return the prefix type for the option name in *argp.
1326 */
1327 static set_prefix_T
1328get_option_prefix(char_u **argp)
1329{
1330 int prefix = PREFIX_NONE;
1331 char_u *arg = *argp;
1332
1333 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1334 {
1335 prefix = PREFIX_NO;
1336 arg += 2;
1337 }
1338 else if (STRNCMP(arg, "inv", 3) == 0)
1339 {
1340 prefix = PREFIX_INV;
1341 arg += 3;
1342 }
1343
1344 *argp = arg;
1345 return prefix;
1346}
1347
1348/*
1349 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1350 * and the option name length in "*lenp". For a <t_xx> option, return the key
1351 * number in "*keyp".
1352 *
1353 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1354 * otherwise returns OK.
1355 */
1356 static int
1357parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1358{
1359 int key = 0;
1360 int len;
1361 int opt_idx;
1362
1363 if (*arg == '<')
1364 {
1365 opt_idx = -1;
1366 // look out for <t_>;>
1367 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1368 len = 5;
1369 else
1370 {
1371 len = 1;
1372 while (arg[len] != NUL && arg[len] != '>')
1373 ++len;
1374 }
1375 if (arg[len] != '>')
1376 return FAIL;
1377
1378 arg[len] = NUL; // put NUL after name
1379 if (arg[1] == 't' && arg[2] == '_') // could be term code
1380 opt_idx = findoption(arg + 1);
1381 arg[len++] = '>'; // restore '>'
1382 if (opt_idx == -1)
1383 key = find_key_option(arg + 1, TRUE);
1384 }
1385 else
1386 {
1387 int nextchar; // next non-white char after option name
1388
1389 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001390 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001391 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1392 len = 4;
1393 else
1394 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1395 ++len;
1396 nextchar = arg[len];
1397 arg[len] = NUL; // put NUL after name
1398 opt_idx = findoption(arg);
1399 arg[len] = nextchar; // restore nextchar
1400 if (opt_idx == -1)
1401 key = find_key_option(arg, FALSE);
1402 }
1403
1404 *keyp = key;
1405 *lenp = len;
1406 *opt_idxp = opt_idx;
1407
1408 return OK;
1409}
1410
1411/*
1412 * Get the option operator (+=, ^=, -=).
1413 */
1414 static set_op_T
1415get_opt_op(char_u *arg)
1416{
1417 set_op_T op = OP_NONE;
1418
1419 if (*arg != NUL && *(arg + 1) == '=')
1420 {
1421 if (*arg == '+')
1422 op = OP_ADDING; // "+="
1423 else if (*arg == '^')
1424 op = OP_PREPENDING; // "^="
1425 else if (*arg == '-')
1426 op = OP_REMOVING; // "-="
1427 }
1428
1429 return op;
1430}
1431
1432/*
1433 * Validate whether the value of the option in "opt_idx" can be changed.
1434 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1435 * if it can be changed.
1436 */
1437 static int
1438validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1439{
1440 // Skip all options that are not window-local (used when showing
1441 // an already loaded buffer in a window).
1442 if ((opt_flags & OPT_WINONLY)
1443 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1444 return FAIL;
1445
1446 // Skip all options that are window-local (used for :vimgrep).
1447 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1448 && options[opt_idx].var == VAR_WIN)
1449 return FAIL;
1450
1451 // Disallow changing some options from modelines.
1452 if (opt_flags & OPT_MODELINE)
1453 {
1454 if (flags & (P_SECURE | P_NO_ML))
1455 {
1456 *errmsg = e_not_allowed_in_modeline;
1457 return FAIL;
1458 }
1459 if ((flags & P_MLE) && !p_mle)
1460 {
1461 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1462 return FAIL;
1463 }
1464#ifdef FEAT_DIFF
1465 // In diff mode some options are overruled. This avoids that
1466 // 'foldmethod' becomes "marker" instead of "diff" and that
1467 // "wrap" gets set.
1468 if (curwin->w_p_diff
1469 && opt_idx >= 0 // shut up coverity warning
1470 && (
1471# ifdef FEAT_FOLDING
1472 options[opt_idx].indir == PV_FDM ||
1473# endif
1474 options[opt_idx].indir == PV_WRAP))
1475 return FAIL;
1476#endif
1477 }
1478
1479#ifdef HAVE_SANDBOX
1480 // Disallow changing some options in the sandbox
1481 if (sandbox != 0 && (flags & P_SECURE))
1482 {
1483 *errmsg = e_not_allowed_in_sandbox;
1484 return FAIL;
1485 }
1486#endif
1487
1488 return OK;
1489}
1490
Bram Moolenaar47403942022-09-21 21:12:53 +01001491/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001492 * Get the Vim/Vi default value for a string option.
1493 */
1494 static char_u *
1495stropt_get_default_val(
1496 int opt_idx,
1497 char_u *varp,
1498 int flags,
1499 int cp_val)
1500{
1501 char_u *newval;
1502
1503 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1504 ? VI_DEFAULT : VIM_DEFAULT];
1505 if ((char_u **)varp == &p_bg)
1506 {
1507 // guess the value of 'background'
1508#ifdef FEAT_GUI
1509 if (gui.in_use)
1510 newval = gui_bg_default();
1511 else
1512#endif
1513 newval = term_bg_default();
1514 }
1515 else if ((char_u **)varp == &p_fencs && enc_utf8)
1516 newval = fencs_utf8_default;
1517
1518 // expand environment variables and ~ since the default value was
1519 // already expanded, only required when an environment variable was set
1520 // later
1521 if (newval == NULL)
1522 newval = empty_option;
1523 else
1524 {
1525 char_u *s = option_expand(opt_idx, newval);
1526 if (s == NULL)
1527 s = newval;
1528 newval = vim_strsave(s);
1529 }
1530
1531 return newval;
1532}
1533
1534/*
1535 * Convert the 'backspace' option number value to a string: for adding,
1536 * prepending and removing string.
1537 */
1538 static void
1539opt_backspace_nr2str(
1540 char_u *varp,
1541 char_u **origval_p,
1542 char_u **origval_l_p,
1543 char_u **origval_g_p,
1544 char_u **oldval_p)
1545{
1546 int i = getdigits((char_u **)varp);
1547
1548 switch (i)
1549 {
1550 case 0:
1551 *(char_u **)varp = empty_option;
1552 break;
1553 case 1:
1554 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1555 break;
1556 case 2:
1557 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1558 break;
1559 case 3:
1560 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1561 break;
1562 }
1563 vim_free(*oldval_p);
1564 if (*origval_p == *oldval_p)
1565 *origval_p = *(char_u **)varp;
1566 if (*origval_l_p == *oldval_p)
1567 *origval_l_p = *(char_u **)varp;
1568 if (*origval_g_p == *oldval_p)
1569 *origval_g_p = *(char_u **)varp;
1570 *oldval_p = *(char_u **)varp;
1571}
1572
1573/*
1574 * Convert the 'whichwrap' option number value to a string, for backwards
1575 * compatibility with Vim 3.0.
1576 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1577 */
1578 static char_u *
1579opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1580{
1581 *whichwrap = NUL;
1582 int i = getdigits(argp);
1583 if (i & 1)
1584 STRCAT(whichwrap, "b,");
1585 if (i & 2)
1586 STRCAT(whichwrap, "s,");
1587 if (i & 4)
1588 STRCAT(whichwrap, "h,l,");
1589 if (i & 8)
1590 STRCAT(whichwrap, "<,>,");
1591 if (i & 16)
1592 STRCAT(whichwrap, "[,],");
1593 if (*whichwrap != NUL) // remove trailing ,
1594 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1595
1596 return whichwrap;
1597}
1598
1599/*
1600 * Copy the new string value into allocated memory for the option.
1601 * Can't use set_string_option_direct(), because we need to remove the
1602 * backslashes.
1603 */
1604 static char_u *
1605stropt_copy_value(
1606 char_u *origval,
1607 char_u **argp,
1608 set_op_T op,
1609 int flags UNUSED)
1610{
1611 char_u *arg = *argp;
1612 unsigned newlen;
1613 char_u *newval;
1614 char_u *s = NULL;
1615
1616 // get a bit too much
1617 newlen = (unsigned)STRLEN(arg) + 1;
1618 if (op != OP_NONE)
1619 newlen += (unsigned)STRLEN(origval) + 1;
1620 newval = alloc(newlen);
1621 if (newval == NULL) // out of mem, don't change
1622 return NULL;
1623 s = newval;
1624
1625 // Copy the string, skip over escaped chars.
1626 // For MS-DOS and WIN32 backslashes before normal file name characters
1627 // are not removed, and keep backslash at start, for "\\machine\path",
1628 // but do remove it for "\\\\machine\\path".
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001629 // The reverse is found in escape_option_str_cmdline().
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001630 while (*arg != NUL && !VIM_ISWHITE(*arg))
1631 {
1632 int i;
1633
1634 if (*arg == '\\' && arg[1] != NUL
1635#ifdef BACKSLASH_IN_FILENAME
1636 && !((flags & P_EXPAND)
1637 && vim_isfilec(arg[1])
1638 && !VIM_ISWHITE(arg[1])
1639 && (arg[1] != '\\'
1640 || (s == newval && arg[2] != '\\')))
1641#endif
1642 )
1643 ++arg; // remove backslash
1644 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1645 {
1646 // copy multibyte char
1647 mch_memmove(s, arg, (size_t)i);
1648 arg += i;
1649 s += i;
1650 }
1651 else
1652 *s++ = *arg++;
1653 }
1654 *s = NUL;
1655
1656 *argp = arg;
1657 return newval;
1658}
1659
1660/*
1661 * Expand environment variables and ~ in string option value 'newval'.
1662 */
1663 static char_u *
1664stropt_expand_envvar(
1665 int opt_idx,
1666 char_u *origval,
1667 char_u *newval,
1668 set_op_T op)
1669{
1670 char_u *s = option_expand(opt_idx, newval);
1671 if (s == NULL)
1672 return newval;
1673
1674 vim_free(newval);
1675 unsigned newlen = (unsigned)STRLEN(s) + 1;
1676 if (op != OP_NONE)
1677 newlen += (unsigned)STRLEN(origval) + 1;
1678
1679 newval = alloc(newlen);
1680 if (newval == NULL)
1681 return NULL;
1682
1683 STRCPY(newval, s);
1684
1685 return newval;
1686}
1687
1688/*
1689 * Concatenate the original and new values of a string option, adding a "," if
1690 * needed.
1691 */
1692 static void
1693stropt_concat_with_comma(
1694 char_u *origval,
1695 char_u *newval,
1696 set_op_T op,
1697 int flags)
1698{
1699 int len = 0;
1700
1701 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1702 if (op == OP_ADDING)
1703 {
1704 len = (int)STRLEN(origval);
1705 // strip a trailing comma, would get 2
1706 if (comma && len > 1
1707 && (flags & P_ONECOMMA) == P_ONECOMMA
1708 && origval[len - 1] == ','
1709 && origval[len - 2] != '\\')
1710 len--;
1711 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1712 mch_memmove(newval, origval, (size_t)len);
1713 }
1714 else
1715 {
1716 len = (int)STRLEN(newval);
1717 STRMOVE(newval + len + comma, origval);
1718 }
1719 if (comma)
1720 newval[len] = ',';
1721}
1722
1723/*
1724 * Remove a value from a string option. Copy string option value in "origval"
1725 * to "newval" and then remove the string "strval" of length "len".
1726 */
1727 static void
1728stropt_remove_val(
1729 char_u *origval,
1730 char_u *newval,
1731 int flags,
1732 char_u *strval,
1733 int len)
1734{
1735 // Remove newval[] from origval[]. (Note: "len" has been set above
1736 // and is used here).
1737 STRCPY(newval, origval);
1738 if (*strval)
1739 {
1740 // may need to remove a comma
1741 if (flags & P_COMMA)
1742 {
1743 if (strval == origval)
1744 {
1745 // include comma after string
1746 if (strval[len] == ',')
1747 ++len;
1748 }
1749 else
1750 {
1751 // include comma before string
1752 --strval;
1753 ++len;
1754 }
1755 }
1756 STRMOVE(newval + (strval - origval), strval + len);
1757 }
1758}
1759
1760/*
1761 * Remove flags that appear twice in the string option value 'newval'.
1762 */
1763 static void
1764stropt_remove_dupflags(char_u *newval, int flags)
1765{
1766 char_u *s = newval;
1767
1768 // Remove flags that appear twice.
1769 while (*s)
1770 {
1771 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1772 if (flags & P_ONECOMMA)
1773 {
1774 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1775 {
1776 // Remove the duplicated value and the next comma.
1777 STRMOVE(s, s + 2);
1778 continue;
1779 }
1780 }
1781 else
1782 {
1783 if ((!(flags & P_COMMA) || *s != ',')
1784 && vim_strchr(s + 1, *s) != NULL)
1785 {
1786 STRMOVE(s, s + 1);
1787 continue;
1788 }
1789 }
1790 ++s;
1791 }
1792}
1793
1794/*
1795 * Get the string value specified for a ":set" command. The following set
1796 * options are supported:
1797 * set {opt}&
1798 * set {opt}<
1799 * set {opt}={val}
1800 * set {opt}:{val}
1801 */
1802 static char_u *
1803stropt_get_newval(
1804 int nextchar,
1805 int opt_idx,
1806 char_u **argp,
1807 char_u *varp,
1808 char_u **origval_arg,
1809 char_u **origval_l_arg,
1810 char_u **origval_g_arg,
1811 char_u **oldval_arg,
1812 set_op_T *op_arg,
1813 int flags,
1814 int cp_val)
1815{
1816 char_u *arg = *argp;
1817 char_u *origval = *origval_arg;
1818 char_u *origval_l = *origval_l_arg;
1819 char_u *origval_g = *origval_g_arg;
1820 char_u *oldval = *oldval_arg;
1821 set_op_T op = *op_arg;
1822 char_u *save_arg = NULL;
1823 char_u *newval;
1824 char_u *s = NULL;
1825 char_u whichwrap[80];
1826
1827 if (nextchar == '&') // set to default val
1828 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1829 else if (nextchar == '<') // set to global val
1830 newval = vim_strsave(*(char_u **)get_varp_scope(
1831 &(options[opt_idx]), OPT_GLOBAL));
1832 else
1833 {
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001834 ++arg; // jump to after the '=' or ':'
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001835
1836 // Set 'keywordprg' to ":help" if an empty
1837 // value was passed to :set by the user.
1838 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1839 {
1840 save_arg = arg;
1841 arg = (char_u *)":help";
1842 }
1843 // Convert 'backspace' number to string
1844 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1845 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1846 &oldval);
1847 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1848 {
1849 // Convert 'whichwrap' number to string, for backwards
1850 // compatibility with Vim 3.0.
1851 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1852 save_arg = arg;
1853 arg = t;
1854 }
1855 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1856 // version 3.0
1857 else if (*arg == '>' && (varp == (char_u *)&p_dir
1858 || varp == (char_u *)&p_bdir))
1859 ++arg;
1860
1861 // Copy the new string into allocated memory.
1862 newval = stropt_copy_value(origval, &arg, op, flags);
1863 if (newval == NULL)
1864 goto done;
1865
1866 // Expand environment variables and ~.
1867 // Don't do it when adding without inserting a comma.
1868 if (op == OP_NONE || (flags & P_COMMA))
1869 {
1870 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1871 if (newval == NULL)
1872 goto done;
1873 }
1874
1875 // locate newval[] in origval[] when removing it and when adding to
1876 // avoid duplicates
1877 int len = 0;
1878 if (op == OP_REMOVING || (flags & P_NODUP))
1879 {
1880 len = (int)STRLEN(newval);
1881 s = find_dup_item(origval, newval, flags);
1882
1883 // do not add if already there
1884 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1885 {
1886 op = OP_NONE;
1887 STRCPY(newval, origval);
1888 }
1889
1890 // if no duplicate, move pointer to end of original value
1891 if (s == NULL)
1892 s = origval + (int)STRLEN(origval);
1893 }
1894
1895 // concatenate the two strings; add a ',' if needed
1896 if (op == OP_ADDING || op == OP_PREPENDING)
1897 stropt_concat_with_comma(origval, newval, op, flags);
1898 else if (op == OP_REMOVING)
1899 // Remove newval[] from origval[]. (Note: "len" has been set above
1900 // and is used here).
1901 stropt_remove_val(origval, newval, flags, s, len);
1902
1903 if (flags & P_FLAGLIST)
1904 // Remove flags that appear twice.
1905 stropt_remove_dupflags(newval, flags);
1906 }
1907
1908done:
1909 if (save_arg != NULL)
1910 arg = save_arg; // arg was temporarily changed, restore it
1911 *argp = arg;
1912 *origval_arg = origval;
1913 *origval_l_arg = origval_l;
1914 *origval_g_arg = origval_g;
1915 *oldval_arg = oldval;
1916 *op_arg = op;
1917
1918 return newval;
1919}
1920
1921/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001922 * Part of do_set() for string options.
1923 * Returns FAIL on failure, do not process further options.
1924 */
1925 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001926do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001927 int opt_idx,
1928 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001929 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001930 int nextchar,
1931 set_op_T op_arg,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001932 long_u flags,
Bram Moolenaar47403942022-09-21 21:12:53 +01001933 int cp_val,
1934 char_u *varp_arg,
1935 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +01001936 size_t errbuflen,
Bram Moolenaar47403942022-09-21 21:12:53 +01001937 int *value_checked,
1938 char **errmsg)
1939{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001940 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001941 set_op_T op = op_arg;
1942 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001943 char_u *oldval = NULL; // previous value if *varp
1944 char_u *newval;
1945 char_u *origval = NULL;
1946 char_u *origval_l = NULL;
1947 char_u *origval_g = NULL;
1948#if defined(FEAT_EVAL)
1949 char_u *saved_origval = NULL;
1950 char_u *saved_origval_l = NULL;
1951 char_u *saved_origval_g = NULL;
1952 char_u *saved_newval = NULL;
1953#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01001954
1955 // When using ":set opt=val" for a global option
1956 // with a local value the local value will be
1957 // reset, use the global value here.
1958 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1959 && ((int)options[opt_idx].indir & PV_BOTH))
1960 varp = options[opt_idx].var;
1961
1962 // The old value is kept until we are sure that the new value is valid.
1963 oldval = *(char_u **)varp;
1964
1965 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1966 {
1967 origval_l = *(char_u **)get_varp_scope(
1968 &(options[opt_idx]), OPT_LOCAL);
1969 origval_g = *(char_u **)get_varp_scope(
1970 &(options[opt_idx]), OPT_GLOBAL);
1971
1972 // A global-local string option might have an empty option as value to
1973 // indicate that the global value should be used.
1974 if (((int)options[opt_idx].indir & PV_BOTH)
1975 && origval_l == empty_option)
1976 origval_l = origval_g;
1977 }
1978
1979 // When setting the local value of a global option, the old value may be
1980 // the global value.
1981 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1982 origval = *(char_u **)get_varp(&options[opt_idx]);
1983 else
1984 origval = oldval;
1985
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001986 // Get the new value for the option
1987 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
1988 &origval_l, &origval_g, &oldval, &op, flags,
1989 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01001990
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001991 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01001992 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00001993 if (newval == NULL)
1994 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01001995
1996#if defined(FEAT_EVAL)
1997 if (!starting
1998# ifdef FEAT_CRYPT
1999 && options[opt_idx].indir != PV_KEY
2000# endif
2001 && origval != NULL && newval != NULL)
2002 {
2003 // origval may be freed by did_set_string_option(), make a copy.
2004 saved_origval = vim_strsave(origval);
2005 // newval (and varp) may become invalid if the buffer is closed by
2006 // autocommands.
2007 saved_newval = vim_strsave(newval);
2008 if (origval_l != NULL)
2009 saved_origval_l = vim_strsave(origval_l);
2010 if (origval_g != NULL)
2011 saved_origval_g = vim_strsave(origval_g);
2012 }
2013#endif
2014
2015 {
2016 long_u *p = insecure_flag(opt_idx, opt_flags);
2017 int secure_saved = secure;
2018
2019 // When an option is set in the sandbox, from a modeline or in secure
2020 // mode, then deal with side effects in secure mode. Also when the
2021 // value was set with the P_INSECURE flag and is not completely
2022 // replaced.
2023 if ((opt_flags & OPT_MODELINE)
2024#ifdef HAVE_SANDBOX
2025 || sandbox != 0
2026#endif
2027 || (op != OP_NONE && (*p & P_INSECURE)))
2028 secure = 1;
2029
2030 // Handle side effects, and set the global value for ":set" on local
2031 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2032 // be triggered that can cause havoc.
2033 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002034 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002035 errbuflen, opt_flags, op, value_checked);
Bram Moolenaar47403942022-09-21 21:12:53 +01002036
2037 secure = secure_saved;
2038 }
2039
2040#if defined(FEAT_EVAL)
2041 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002042 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002043 saved_origval_l, saved_origval_g, saved_newval);
2044 vim_free(saved_origval);
2045 vim_free(saved_origval_l);
2046 vim_free(saved_origval_g);
2047 vim_free(saved_newval);
2048#endif
2049
Bram Moolenaar6f981142022-09-22 12:48:58 +01002050 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002051 return *errmsg == NULL ? OK : FAIL;
2052}
2053
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002055 * Set a boolean option.
2056 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002057 */
2058 static char *
2059do_set_option_bool(
2060 int opt_idx,
2061 int opt_flags,
2062 set_prefix_T prefix,
2063 long_u flags,
2064 char_u *varp,
2065 int nextchar,
2066 int afterchar,
2067 int cp_val)
2068
2069{
2070 varnumber_T value;
2071
2072 if (nextchar == '=' || nextchar == ':')
2073 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002074 if (opt_idx < 0 || varp == NULL)
2075 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002076
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002077 // ":set opt!": invert
2078 // ":set opt&": reset to default value
2079 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002080 if (nextchar == '!')
2081 value = *(int *)(varp) ^ 1;
2082 else if (nextchar == '&')
2083 value = (int)(long)(long_i)options[opt_idx].def_val[
2084 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2085 else if (nextchar == '<')
2086 {
2087 // For 'autoread' -1 means to use global value.
2088 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2089 value = -1;
2090 else
2091 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2092 }
2093 else
2094 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002095 // ":set invopt": invert
2096 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002097 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2098 return e_trailing_characters;
2099 if (prefix == PREFIX_INV)
2100 value = *(int *)(varp) ^ 1;
2101 else
2102 value = prefix == PREFIX_NO ? 0 : 1;
2103 }
2104
2105 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2106}
2107
2108/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002109 * Set a numeric option.
2110 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002111 */
2112 static char *
2113do_set_option_numeric(
2114 int opt_idx,
2115 int opt_flags,
2116 char_u **argp,
2117 int nextchar,
2118 set_op_T op,
2119 long_u flags,
2120 int cp_val,
2121 char_u *varp,
2122 char *errbuf,
2123 size_t errbuflen)
2124{
2125 char_u *arg = *argp;
2126 varnumber_T value;
2127 int i;
2128 char *errmsg = NULL;
2129
Bram Moolenaar546933f2023-02-06 16:40:49 +00002130 if (opt_idx < 0 || varp == NULL)
2131 return NULL; // "cannot happen"
2132 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002133 /*
2134 * Different ways to set a number option:
2135 * & set to default value
2136 * < set to global value
2137 * <xx> accept special key codes for 'wildchar'
2138 * c accept any non-digit for 'wildchar'
2139 * [-]0-9 set number
2140 * other error
2141 */
2142 ++arg;
2143 if (nextchar == '&')
2144 value = (long)(long_i)options[opt_idx].def_val[
2145 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2146 else if (nextchar == '<')
2147 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002148 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002149 // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002150 value = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002151 else if (opt_flags == OPT_LOCAL
2152 && ((long *)varp == &curwin->w_p_siso
2153 || (long *)varp == &curwin->w_p_so))
2154 // for 'scrolloff'/'sidescrolloff' -1 means using the global value
2155 value = -1;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002156 else
2157 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2158 }
2159 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2160 && (*arg == '<'
2161 || *arg == '^'
2162 || (*arg != NUL
2163 && (!arg[1] || VIM_ISWHITE(arg[1]))
2164 && !VIM_ISDIGIT(*arg))))
2165 {
2166 value = string_to_key(arg, FALSE);
2167 if (value == 0 && (long *)varp != &p_wcm)
2168 {
2169 errmsg = e_invalid_argument;
2170 goto skip;
2171 }
2172 }
2173 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2174 {
2175 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002176 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002177 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2178 {
2179 errmsg = e_number_required_after_equal;
2180 goto skip;
2181 }
2182 }
2183 else
2184 {
2185 errmsg = e_number_required_after_equal;
2186 goto skip;
2187 }
2188
2189 if (op == OP_ADDING)
2190 value = *(long *)varp + value;
2191 else if (op == OP_PREPENDING)
2192 value = *(long *)varp * value;
2193 else if (op == OP_REMOVING)
2194 value = *(long *)varp - value;
2195
2196 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2197 opt_flags);
2198
2199skip:
2200 *argp = arg;
2201 return errmsg;
2202}
2203
2204/*
2205 * Set a key code (t_xx) option
2206 */
2207 static char *
2208do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2209{
2210 char_u *arg = *argp;
2211 char_u *p;
2212
2213 if (nextchar == '&')
2214 {
2215 if (add_termcap_entry(key_name, TRUE) == FAIL)
2216 return e_not_found_in_termcap;
2217 }
2218 else
2219 {
2220 ++arg; // jump to after the '=' or ':'
2221 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2222 if (*p == '\\' && p[1] != NUL)
2223 ++p;
2224 nextchar = *p;
2225 *p = NUL;
2226 add_termcode(key_name, arg, FALSE);
2227 *p = nextchar;
2228 }
2229 if (full_screen)
2230 ttest(FALSE);
2231 redraw_all_later(UPD_CLEAR);
2232
2233 *argp = arg;
2234 return NULL;
2235}
2236
2237/*
2238 * Set an option to a new value.
2239 */
2240 static char *
2241do_set_option_value(
2242 int opt_idx,
2243 int opt_flags,
2244 char_u **argp,
2245 set_prefix_T prefix,
2246 set_op_T op,
2247 long_u flags,
2248 char_u *varp,
2249 char_u *key_name,
2250 int nextchar,
2251 int afterchar,
2252 int cp_val,
2253 int *stopopteval,
2254 char *errbuf,
2255 size_t errbuflen)
2256{
2257 int value_checked = FALSE;
2258 char *errmsg = NULL;
2259 char_u *arg = *argp;
2260
2261 if (flags & P_BOOL)
2262 {
2263 // boolean option
2264 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2265 nextchar, afterchar, cp_val);
2266 if (errmsg != NULL)
2267 goto skip;
2268 }
2269 else
2270 {
2271 // numeric or string option
2272 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2273 || prefix != PREFIX_NONE)
2274 {
2275 errmsg = e_invalid_argument;
2276 goto skip;
2277 }
2278
2279 if (flags & P_NUM)
2280 {
2281 // numeric option
2282 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2283 op, flags, cp_val, varp,
2284 errbuf, errbuflen);
2285 if (errmsg != NULL)
2286 goto skip;
2287 }
2288 else if (opt_idx >= 0)
2289 {
2290 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002291 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002292 flags, cp_val, varp, errbuf, errbuflen,
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002293 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002294 {
2295 if (errmsg != NULL)
2296 goto skip;
2297 *stopopteval = TRUE;
2298 goto skip;
2299 }
2300 }
2301 else
2302 {
2303 // key code option
2304 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2305 if (errmsg != NULL)
2306 goto skip;
2307 }
2308 }
2309
2310 if (opt_idx >= 0)
2311 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2312
2313skip:
2314 *argp = arg;
2315 return errmsg;
2316}
2317
2318/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002319 * Set an option to a new value.
2320 * Return NULL if OK, return an untranslated error message when something is
2321 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2322 */
2323 static char *
2324do_set_option(
2325 int opt_flags,
2326 char_u **argp,
2327 char_u *arg_start,
2328 char_u **startarg,
2329 int *did_show,
2330 int *stopopteval,
2331 char *errbuf,
2332 size_t errbuflen)
2333{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002334 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002335 char_u *arg;
2336 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2337 set_op_T op;
2338 long_u flags; // flags for current option
2339 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002340 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002341 int nextchar; // next non-white char after option name
2342 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002343 char *errmsg = NULL;
2344 int key;
2345 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002346
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002347 prefix = get_option_prefix(argp);
2348 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002349
2350 // find end of name
2351 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002352 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2353 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002354
2355 // remember character after option name
2356 afterchar = arg[len];
2357
2358 if (in_vim9script())
2359 {
2360 char_u *p = skipwhite(arg + len);
2361
2362 // disallow white space before =val, +=val, -=val, ^=val
2363 if (p > arg + len && (p[0] == '='
2364 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2365 && p[1] == '=')))
2366 {
2367 errmsg = e_no_white_space_allowed_between_option_and;
2368 arg = p;
2369 *startarg = p;
2370 goto skip;
2371 }
2372 }
2373 else
2374 // skip white space, allow ":set ai ?", ":set hlsearch !"
2375 while (VIM_ISWHITE(arg[len]))
2376 ++len;
2377
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002378 op = get_opt_op(arg + len);
2379 if (op != OP_NONE)
2380 len++;
2381
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002382 nextchar = arg[len];
2383
2384 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2385 {
2386 if (in_vim9script() && arg > arg_start
2387 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2388 errmsg = e_no_white_space_allowed_between_option_and;
2389 else
2390 errmsg = e_unknown_option;
2391 goto skip;
2392 }
2393
2394 if (opt_idx >= 0)
2395 {
2396 if (options[opt_idx].var == NULL) // hidden option: skip
2397 {
2398 // Only give an error message when requesting the value of
2399 // a hidden option, ignore setting it.
2400 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2401 && (!(options[opt_idx].flags & P_BOOL)
2402 || nextchar == '?'))
2403 errmsg = e_option_not_supported;
2404 goto skip;
2405 }
2406
2407 flags = options[opt_idx].flags;
2408 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2409 }
2410 else
2411 {
2412 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002413 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002414 if (key < 0)
2415 {
2416 key_name[0] = KEY2TERMCAP0(key);
2417 key_name[1] = KEY2TERMCAP1(key);
2418 }
2419 else
2420 {
2421 key_name[0] = KS_KEY;
2422 key_name[1] = (key & 0xff);
2423 }
2424 }
2425
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002426 // Make sure the option value can be changed.
2427 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002428 goto skip;
2429
Bram Moolenaar40b48722023-02-05 17:04:50 +00002430 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002431 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2432 {
2433 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002434 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2435 {
2436 if (arg[3] == 'm') // "opt&vim": set to Vim default
2437 {
2438 cp_val = FALSE;
2439 arg += 3;
2440 }
2441 else // "opt&vi": set to Vi default
2442 {
2443 cp_val = TRUE;
2444 arg += 2;
2445 }
2446 }
2447 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2448 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2449 {
2450 errmsg = e_trailing_characters;
2451 goto skip;
2452 }
2453 }
2454
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002455 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2456 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002457 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002458 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002459 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2460 && !(flags & P_BOOL)))
2461 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002462 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002463 if (*did_show)
2464 msg_putchar('\n'); // cursor below last one
2465 else
2466 {
2467 gotocmdline(TRUE); // cursor at status line
2468 *did_show = TRUE; // remember that we did a line
2469 }
2470 if (opt_idx >= 0)
2471 {
2472 showoneopt(&options[opt_idx], opt_flags);
2473#ifdef FEAT_EVAL
2474 if (p_verbose > 0)
2475 {
2476 // Mention where the option was last set.
2477 if (varp == options[opt_idx].var)
2478 last_set_msg(options[opt_idx].script_ctx);
2479 else if ((int)options[opt_idx].indir & PV_WIN)
2480 last_set_msg(curwin->w_p_script_ctx[
2481 (int)options[opt_idx].indir & PV_MASK]);
2482 else if ((int)options[opt_idx].indir & PV_BUF)
2483 last_set_msg(curbuf->b_p_script_ctx[
2484 (int)options[opt_idx].indir & PV_MASK]);
2485 }
2486#endif
2487 }
2488 else
2489 {
2490 char_u *p;
2491
2492 p = find_termcode(key_name);
2493 if (p == NULL)
2494 {
2495 errmsg = e_key_code_not_set;
2496 goto skip;
2497 }
2498 else
2499 (void)show_one_termcode(key_name, p, TRUE);
2500 }
2501 if (nextchar != '?'
2502 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2503 errmsg = e_trailing_characters;
2504 }
2505 else
2506 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002507 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2508 flags, varp, key_name, nextchar,
2509 afterchar, cp_val, stopopteval, errbuf,
2510 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002511 }
2512
2513skip:
2514 *argp = arg;
2515 return errmsg;
2516}
2517
2518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 * Parse 'arg' for option settings.
2520 *
2521 * 'arg' may be IObuff, but only when no errors can be present and option
2522 * does not need to be expanded with option_expand().
2523 * "opt_flags":
2524 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002525 * OPT_GLOBAL for ":setglobal"
2526 * OPT_LOCAL for ":setlocal" and a modeline
2527 * OPT_MODELINE for a modeline
2528 * OPT_WINONLY to only set window-local options
2529 * OPT_NOWIN to skip setting window-local options
2530 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002532 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 */
2534 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002535do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002536 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002537 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002539 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002541 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
2543 if (*arg == NUL)
2544 {
2545 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002546 did_show = TRUE;
2547 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002550 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002552 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002553 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002555 // ":set all" show all options.
2556 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 arg += 3;
2558 if (*arg == '&')
2559 {
2560 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002561 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002563 didset_options();
2564 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002565 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
2567 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002570 did_show = TRUE;
2571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002573 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
2575 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002576 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002577 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 arg += 7;
2579 }
2580 else
2581 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002582 int stopopteval = FALSE;
2583 char *errmsg = NULL;
Christian Brabandtb39b2402023-11-29 11:34:05 +01002584 char errbuf[ERR_BUFLEN];
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002585 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002587 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2588 &did_show, &stopopteval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002589 ERR_BUFLEN);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002590 if (stopopteval)
2591 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002593 // Advance to next argument.
2594 // - skip until a blank found, taking care of backslashes
2595 // - skip blanks
2596 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 for (i = 0; i < 2 ; ++i)
2598 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002599 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 if (*arg++ == '\\' && *arg != NUL)
2601 ++arg;
2602 arg = skipwhite(arg);
2603 if (*arg != '=')
2604 break;
2605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002607 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002609 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2610 i = (int)STRLEN(IObuff) + 2;
2611 if (i + (arg - startarg) < IOSIZE)
2612 {
2613 // append the argument with the error
2614 STRCAT(IObuff, ": ");
2615 mch_memmove(IObuff + i, startarg, (arg - startarg));
2616 IObuff[i + (arg - startarg)] = NUL;
2617 }
2618 // make sure all characters are printable
2619 trans_characters(IObuff, IOSIZE);
2620
2621 ++no_wait_return; // wait_return() done later
2622 emsg((char *)IObuff); // show error highlighted
2623 --no_wait_return;
2624
2625 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
2628
2629 arg = skipwhite(arg);
2630 }
2631
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002632theend:
2633 if (silent_mode && did_show)
2634 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002635 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002636 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002637 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002638 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002639 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002640 out_flush();
2641 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002642 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002643 }
2644
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 return OK;
2646}
2647
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002648/*
2649 * Call this when an option has been given a new value through a user command.
2650 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2651 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002652 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002653did_set_option(
2654 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002655 int opt_flags, // possibly with OPT_MODELINE
2656 int new_value, // value was replaced completely
2657 int value_checked) // value was checked to be safe, no need to set the
2658 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002659{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002660 long_u *p;
2661
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002662 options[opt_idx].flags |= P_WAS_SET;
2663
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002664 // When an option is set in the sandbox, from a modeline or in secure mode
2665 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2666 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002667 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002668 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002669#ifdef HAVE_SANDBOX
2670 || sandbox != 0
2671#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002672 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002673 *p = *p | P_INSECURE;
2674 else if (new_value)
2675 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002676}
2677
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678/*
2679 * Convert a key name or string into a key value.
2680 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002681 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002683 int
2684string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
2686 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002687 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 if (*arg == '^')
2689 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002690 if (multi_byte)
2691 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return *arg;
2693}
2694
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695/*
2696 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2697 * maketitle() to create and display it.
2698 * When switching the title or icon off, call mch_restore_title() to get
2699 * the old value back.
2700 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002701 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002702did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
2704 if (starting != NO_SCREEN
2705#ifdef FEAT_GUI
2706 && !gui.starting
2707#endif
2708 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
2712/*
2713 * set_options_bin - called when 'bin' changes value.
2714 */
2715 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002716set_options_bin(
2717 int oldval,
2718 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002719 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002721 // The option values that are changed when 'bin' changes are
2722 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (newval)
2724 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
2727 if (!(opt_flags & OPT_GLOBAL))
2728 {
2729 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2730 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2731 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2732 curbuf->b_p_et_nobin = curbuf->b_p_et;
2733 }
2734 if (!(opt_flags & OPT_LOCAL))
2735 {
2736 p_tw_nobin = p_tw;
2737 p_wm_nobin = p_wm;
2738 p_ml_nobin = p_ml;
2739 p_et_nobin = p_et;
2740 }
2741 }
2742
2743 if (!(opt_flags & OPT_GLOBAL))
2744 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 curbuf->b_p_tw = 0; // no automatic line wrap
2746 curbuf->b_p_wm = 0; // no automatic line wrap
2747 curbuf->b_p_ml = 0; // no modelines
2748 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
2750 if (!(opt_flags & OPT_LOCAL))
2751 {
2752 p_tw = 0;
2753 p_wm = 0;
2754 p_ml = FALSE;
2755 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002759 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
2761 if (!(opt_flags & OPT_GLOBAL))
2762 {
2763 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2764 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2765 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2766 curbuf->b_p_et = curbuf->b_p_et_nobin;
2767 }
2768 if (!(opt_flags & OPT_LOCAL))
2769 {
2770 p_tw = p_tw_nobin;
2771 p_wm = p_wm_nobin;
2772 p_ml = p_ml_nobin;
2773 p_et = p_et_nobin;
2774 }
2775 }
Christian Brabandt757593c2023-08-22 21:44:10 +02002776#if defined(FEAT_EVAL) || defined(PROTO)
2777 // Remember where the dependent option were reset
2778 didset_options_sctx(opt_flags, p_bin_dep_opts);
2779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780}
2781
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782/*
2783 * Expand environment variables for some string options.
2784 * These string options cannot be indirect!
2785 * If "val" is NULL expand the current value of the option.
2786 * Return pointer to NameBuff, or NULL when not expanded.
2787 */
2788 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002789option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002791 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2793 return NULL;
2794
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002795 // If val is longer than MAXPATHL no meaningful expansion can be done,
2796 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (val != NULL && STRLEN(val) > MAXPATHL)
2798 return NULL;
2799
2800 if (val == NULL)
2801 val = *(char_u **)options[opt_idx].var;
2802
2803 /*
2804 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2805 * Escape spaces when expanding 'tags', they are used to separate file
2806 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002807 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
2809 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002810 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002811#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002812 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2813#endif
2814 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002815 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 return NULL;
2817
2818 return NameBuff;
2819}
2820
2821/*
2822 * After setting various option values: recompute variables that depend on
2823 * option values.
2824 */
2825 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002826didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002828 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 (void)init_chartab();
2830
Bram Moolenaardac13472019-09-16 21:06:21 +02002831 didset_string_options();
2832
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002833#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002834 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002835 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002836 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002837 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002838#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002839 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002840 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002841#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002842 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002843 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002844#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002845 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002846}
2847
2848/*
2849 * More side effects of setting options.
2850 */
2851 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002852didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002853{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002854 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002855 (void)highlight_changed();
2856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002857 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002858 check_opt_wim();
2859
Bram Moolenaareed9d462021-02-15 20:38:25 +01002860 // Parse default for 'listchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002861 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE, NULL, 0);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002862
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002863 // Parse default for 'fillchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002864 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002865
2866#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002867 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002868 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002869#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002870#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002871 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002872 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002873 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002874 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876}
2877
2878/*
2879 * Check for string options that are NULL (normally only termcap options).
2880 */
2881 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002882check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
2884 int opt_idx;
2885
2886 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2887 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2888 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2889}
2890
2891/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002892 * Return the option index found by a pointer into term_strings[].
2893 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002895 int
2896get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002898 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
2900 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2901 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002902 return opt_idx;
2903 return -1; // cannot happen: didn't find it!
2904}
2905
2906/*
2907 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2908 * Return the option index or -1 if not found.
2909 */
2910 int
2911set_term_option_alloced(char_u **p)
2912{
2913 int opt_idx = get_term_opt_idx(p);
2914
2915 if (opt_idx >= 0)
2916 options[opt_idx].flags |= P_ALLOCED;
2917 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918}
2919
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002920#if defined(FEAT_EVAL) || defined(PROTO)
2921/*
2922 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2923 * Return FALSE when it wasn't.
2924 * Return -1 for an unknown option.
2925 */
2926 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002927was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002928{
2929 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002930 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002931
2932 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002933 {
2934 flagp = insecure_flag(idx, opt_flags);
2935 return (*flagp & P_INSECURE) != 0;
2936 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002937 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002938 return -1;
2939}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002940
2941/*
2942 * Get a pointer to the flags used for the P_INSECURE flag of option
2943 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002944 * NOTE: Caller must make sure that "curwin" is set to the window from which
2945 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002946 */
2947 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002948insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002949{
2950 if (opt_flags & OPT_LOCAL)
2951 switch ((int)options[opt_idx].indir)
2952 {
2953#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002954 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002955#endif
2956#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002957# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002958 case PV_FDE: return &curwin->w_p_fde_flags;
2959 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002960# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002961# ifdef FEAT_BEVAL
2962 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2963# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002964 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002965 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002966# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002967 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002968# endif
2969#endif
2970 }
2971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002973 return &options[opt_idx].flags;
2974}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002975#endif
2976
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002977/*
2978 * Redraw the window title and/or tab page text later.
2979 */
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002980 void
2981redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002982{
2983 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002984 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002985}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002988 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2989 * characters or characters in "allowed".
2990 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002991 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002992valid_name(char_u *val, char *allowed)
2993{
2994 char_u *s;
2995
2996 for (s = val; *s != NUL; ++s)
2997 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2998 return FALSE;
2999 return TRUE;
3000}
3001
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003002#if defined(FEAT_EVAL) || defined(PROTO)
3003/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003004 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003005 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003006 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003007 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003008set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003009{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003010 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3011 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003012 sctx_T new_script_ctx = script_ctx;
3013
Bram Moolenaar51258742020-05-03 17:19:33 +02003014 // Modeline already has the line number set.
3015 if (!(opt_flags & OPT_MODELINE))
3016 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003018 // Remember where the option was set. For local options need to do that
3019 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003020 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003021 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003022 if (both || (opt_flags & OPT_LOCAL))
3023 {
3024 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003025 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003026 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003027 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003028 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003029 if (both)
3030 // also setting the "all buffers" value
3031 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3032 new_script_ctx;
3033 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003034 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003035}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003036
3037/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003038 * Get the script context of global option "name".
3039 *
3040 */
3041 sctx_T *
3042get_option_sctx(char *name)
3043{
3044 int idx = findoption((char_u *)name);
3045
3046 if (idx >= 0)
3047 return &options[idx].script_ctx;
3048 siemsg("no such option: %s", name);
3049 return NULL;
3050}
3051
3052/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003053 * Set the script_ctx for a termcap option.
3054 * "name" must be the two character code, e.g. "RV".
3055 * When "name" is NULL use "opt_idx".
3056 */
3057 void
3058set_term_option_sctx_idx(char *name, int opt_idx)
3059{
3060 char_u buf[5];
3061 int idx;
3062
3063 if (name == NULL)
3064 idx = opt_idx;
3065 else
3066 {
3067 buf[0] = 't';
3068 buf[1] = '_';
3069 buf[2] = name[0];
3070 buf[3] = name[1];
3071 buf[4] = 0;
3072 idx = findoption(buf);
3073 }
3074 if (idx >= 0)
3075 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3076}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003077#endif
3078
Bram Moolenaarf4140482020-02-15 23:06:45 +01003079#if defined(FEAT_EVAL)
3080/*
3081 * Apply the OptionSet autocommand.
3082 */
3083 static void
3084apply_optionset_autocmd(
3085 int opt_idx,
3086 long opt_flags,
3087 long oldval,
3088 long oldval_g,
3089 long newval,
3090 char *errmsg)
3091{
3092 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3093
3094 // Don't do this while starting up, failure or recursively.
3095 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3096 return;
3097
3098 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3099 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3100 oldval_g);
3101 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3102 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3103 (opt_flags & OPT_LOCAL) ? "local" : "global");
3104 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3105 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3106 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3107 if (opt_flags & OPT_LOCAL)
3108 {
3109 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3110 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3111 }
3112 if (opt_flags & OPT_GLOBAL)
3113 {
3114 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3115 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3116 }
3117 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3118 {
3119 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3120 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3121 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3122 }
3123 if (opt_flags & OPT_MODELINE)
3124 {
3125 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3126 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3127 }
3128 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3129 NULL, FALSE, NULL);
3130 reset_v_option_vars();
3131}
3132#endif
3133
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003134#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003135/*
3136 * Process the updated 'arabic' option value.
3137 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003138 char *
3139did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003140{
3141 char *errmsg = NULL;
3142
3143 if (curwin->w_p_arab)
3144 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003145 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003146 if (!p_tbidi)
3147 {
3148 // set rightleft mode
3149 if (!curwin->w_p_rl)
3150 {
3151 curwin->w_p_rl = TRUE;
3152 changed_window_setting();
3153 }
3154
3155 // Enable Arabic shaping (major part of what Arabic requires)
3156 if (!p_arshape)
3157 {
3158 p_arshape = TRUE;
3159 redraw_later_clear();
3160 }
3161 }
3162
3163 // Arabic requires a utf-8 encoding, inform the user if it's not
3164 // set.
3165 if (STRCMP(p_enc, "utf-8") != 0)
3166 {
3167 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3168
3169 msg_source(HL_ATTR(HLF_W));
3170 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3171#ifdef FEAT_EVAL
3172 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3173#endif
3174 }
3175
3176 // set 'delcombine'
3177 p_deco = TRUE;
3178
3179# ifdef FEAT_KEYMAP
3180 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003181 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3182 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003183# endif
3184 }
3185 else
3186 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003187 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003188 if (!p_tbidi)
3189 {
3190 // reset rightleft mode
3191 if (curwin->w_p_rl)
3192 {
3193 curwin->w_p_rl = FALSE;
3194 changed_window_setting();
3195 }
3196
3197 // 'arabicshape' isn't reset, it is a global option and
3198 // another window may still need it "on".
3199 }
3200
3201 // 'delcombine' isn't reset, it is a global option and another
3202 // window may still want it "on".
3203
3204# ifdef FEAT_KEYMAP
3205 // Revert to the default keymap
3206 curbuf->b_p_iminsert = B_IMODE_NONE;
3207 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3208# endif
3209 }
3210
3211 return errmsg;
3212}
3213#endif
3214
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003215#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3216/*
3217 * Process the updated 'autochdir' option value.
3218 */
3219 char *
3220did_set_autochdir(optset_T *args UNUSED)
3221{
3222 // Change directories when the 'acd' option is set now.
3223 DO_AUTOCHDIR;
3224 return NULL;
3225}
3226#endif
3227
3228#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3229/*
3230 * Process the updated 'ballooneval' option value.
3231 */
3232 char *
3233did_set_ballooneval(optset_T *args)
3234{
3235 if (balloonEvalForTerm)
3236 return NULL;
3237
3238 if (p_beval && !args->os_oldval.boolean)
3239 gui_mch_enable_beval_area(balloonEval);
3240 else if (!p_beval && args->os_oldval.boolean)
3241 gui_mch_disable_beval_area(balloonEval);
3242
3243 return NULL;
3244}
3245#endif
3246
3247#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3248/*
3249 * Process the updated 'balloonevalterm' option value.
3250 */
3251 char *
3252did_set_balloonevalterm(optset_T *args UNUSED)
3253{
3254 mch_bevalterm_changed();
3255 return NULL;
3256}
3257#endif
3258
3259/*
3260 * Process the updated 'binary' option value.
3261 */
3262 char *
3263did_set_binary(optset_T *args)
3264{
3265 // when 'bin' is set also set some other options
3266 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3267 redraw_titles();
3268
3269 return NULL;
3270}
3271
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003272/*
3273 * Process the updated 'buflisted' option value.
3274 */
3275 char *
3276did_set_buflisted(optset_T *args)
3277{
3278 // when 'buflisted' changes, trigger autocommands
3279 if (args->os_oldval.boolean != curbuf->b_p_bl)
3280 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3281 NULL, NULL, TRUE, curbuf);
3282 return NULL;
3283}
3284
3285/*
3286 * Process the new 'cmdheight' option value.
3287 */
3288 char *
3289did_set_cmdheight(optset_T *args)
3290{
3291 long old_value = args->os_oldval.number;
3292 char *errmsg = NULL;
3293
3294 // if p_ch changed value, change the command line height
3295 if (p_ch < 1)
3296 {
3297 errmsg = e_argument_must_be_positive;
3298 p_ch = 1;
3299 }
3300 if (p_ch > Rows - min_rows() + 1)
3301 p_ch = Rows - min_rows() + 1;
3302
3303 // Only compute the new window layout when startup has been
3304 // completed. Otherwise the frame sizes may be wrong.
3305 if ((p_ch != old_value
3306 || tabline_height() + topframe->fr_height != Rows - p_ch)
3307 && full_screen
3308#ifdef FEAT_GUI
3309 && !gui.starting
3310#endif
3311 )
3312 command_height();
3313
3314 return errmsg;
3315}
3316
3317/*
3318 * Process the updated 'compatible' option value.
3319 */
3320 char *
3321did_set_compatible(optset_T *args UNUSED)
3322{
3323 compatible_set();
3324 return NULL;
3325}
3326
3327#if defined(FEAT_CONCEAL) || defined(PROTO)
3328/*
3329 * Process the new 'conceallevel' option value.
3330 */
3331 char *
3332did_set_conceallevel(optset_T *args UNUSED)
3333{
3334 char *errmsg = NULL;
3335
3336 if (curwin->w_p_cole < 0)
3337 {
3338 errmsg = e_argument_must_be_positive;
3339 curwin->w_p_cole = 0;
3340 }
3341 else if (curwin->w_p_cole > 3)
3342 {
3343 errmsg = e_invalid_argument;
3344 curwin->w_p_cole = 3;
3345 }
3346
3347 return errmsg;
3348}
3349#endif
3350
3351#if defined(FEAT_DIFF) || defined(PROTO)
3352/*
3353 * Process the updated 'diff' option value.
3354 */
3355 char *
3356did_set_diff(optset_T *args UNUSED)
3357{
3358 // May add or remove the buffer from the list of diff buffers.
3359 diff_buf_adjust(curwin);
3360# ifdef FEAT_FOLDING
3361 if (foldmethodIsDiff(curwin))
3362 foldUpdateAll(curwin);
3363# endif
3364 return NULL;
3365}
3366#endif
3367
3368/*
3369 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3370 * option value.
3371 */
3372 char *
3373did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3374{
3375 // redraw the window title and tab page text
3376 redraw_titles();
3377 return NULL;
3378}
3379
3380/*
3381 * Process the updated 'equalalways' option value.
3382 */
3383 char *
3384did_set_equalalways(optset_T *args)
3385{
3386 if (p_ea && !args->os_oldval.boolean)
3387 win_equal(curwin, FALSE, 0);
3388
3389 return NULL;
3390}
3391
3392#if defined(FEAT_FOLDING) || defined(PROTO)
3393/*
3394 * Process the new 'foldcolumn' option value.
3395 */
3396 char *
3397did_set_foldcolumn(optset_T *args UNUSED)
3398{
3399 char *errmsg = NULL;
3400
3401 if (curwin->w_p_fdc < 0)
3402 {
3403 errmsg = e_argument_must_be_positive;
3404 curwin->w_p_fdc = 0;
3405 }
3406 else if (curwin->w_p_fdc > 12)
3407 {
3408 errmsg = e_invalid_argument;
3409 curwin->w_p_fdc = 12;
3410 }
3411
3412 return errmsg;
3413}
3414
3415/*
3416 * Process the new 'foldlevel' option value.
3417 */
3418 char *
3419did_set_foldlevel(optset_T *args UNUSED)
3420{
3421 if (curwin->w_p_fdl < 0)
3422 curwin->w_p_fdl = 0;
3423 newFoldLevel();
3424 return NULL;
3425}
3426
3427/*
3428 * Process the new 'foldminlines' option value.
3429 */
3430 char *
3431did_set_foldminlines(optset_T *args UNUSED)
3432{
3433 foldUpdateAll(curwin);
3434 return NULL;
3435}
3436
3437/*
3438 * Process the new 'foldnestmax' option value.
3439 */
3440 char *
3441did_set_foldnestmax(optset_T *args UNUSED)
3442{
3443 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3444 foldUpdateAll(curwin);
3445 return NULL;
3446}
3447#endif
3448
3449#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3450/*
3451 * Process the updated 'hlsearch' option value.
3452 */
3453 char *
3454did_set_hlsearch(optset_T *args UNUSED)
3455{
3456 // when 'hlsearch' is set or reset: reset no_hlsearch
3457 set_no_hlsearch(FALSE);
3458 return NULL;
3459}
3460#endif
3461
3462/*
3463 * Process the updated 'ignorecase' option value.
3464 */
3465 char *
3466did_set_ignorecase(optset_T *args UNUSED)
3467{
3468 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3469 if (p_hls)
3470 redraw_all_later(UPD_SOME_VALID);
3471 return NULL;
3472}
3473
3474#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3475/*
3476 * Process the updated 'imdisable' option value.
3477 */
3478 char *
3479did_set_imdisable(optset_T *args UNUSED)
3480{
3481 // Only de-activate it here, it will be enabled when changing mode.
3482 if (p_imdisable)
3483 im_set_active(FALSE);
3484 else if (State & MODE_INSERT)
3485 // When the option is set from an autocommand, it may need to take
3486 // effect right away.
3487 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3488 return NULL;
3489}
3490#endif
3491
3492/*
3493 * Process the new 'iminsert' option value.
3494 */
3495 char *
3496did_set_iminsert(optset_T *args UNUSED)
3497{
3498 char *errmsg = NULL;
3499
3500 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3501 {
3502 errmsg = e_invalid_argument;
3503 curbuf->b_p_iminsert = B_IMODE_NONE;
3504 }
3505 p_iminsert = curbuf->b_p_iminsert;
3506 if (termcap_active) // don't do this in the alternate screen
3507 showmode();
3508#if defined(FEAT_KEYMAP)
3509 // Show/unshow value of 'keymap' in status lines.
3510 status_redraw_curbuf();
3511#endif
3512
3513 return errmsg;
3514}
3515
3516/*
3517 * Process the new 'imsearch' option value.
3518 */
3519 char *
3520did_set_imsearch(optset_T *args UNUSED)
3521{
3522 char *errmsg = NULL;
3523
3524 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3525 {
3526 errmsg = e_invalid_argument;
3527 curbuf->b_p_imsearch = B_IMODE_NONE;
3528 }
3529 p_imsearch = curbuf->b_p_imsearch;
3530
3531 return errmsg;
3532}
3533
3534#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3535/*
3536 * Process the new 'imstyle' option value.
3537 */
3538 char *
3539did_set_imstyle(optset_T *args UNUSED)
3540{
3541 char *errmsg = NULL;
3542
3543 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3544 errmsg = e_invalid_argument;
3545
3546 return errmsg;
3547}
3548#endif
3549
3550/*
3551 * Process the updated 'insertmode' option value.
3552 */
3553 char *
3554did_set_insertmode(optset_T *args)
3555{
3556 // when 'insertmode' is set from an autocommand need to do work here
3557 if (p_im)
3558 {
3559 if ((State & MODE_INSERT) == 0)
3560 need_start_insertmode = TRUE;
3561 stop_insert_mode = FALSE;
3562 }
3563 // only reset if it was set previously
3564 else if (args->os_oldval.boolean)
3565 {
3566 need_start_insertmode = FALSE;
3567 stop_insert_mode = TRUE;
3568 if (restart_edit != 0 && mode_displayed)
3569 clear_cmdline = TRUE; // remove "(insert)"
3570 restart_edit = 0;
3571 }
3572
3573 return NULL;
3574}
3575
3576#if defined(FEAT_LANGMAP) || defined(PROTO)
3577/*
3578 * Process the updated 'langnoremap' option value.
3579 */
3580 char *
3581did_set_langnoremap(optset_T *args UNUSED)
3582{
3583 // 'langnoremap' -> !'langremap'
3584 p_lrm = !p_lnr;
3585 return NULL;
3586}
3587
3588/*
3589 * Process the updated 'langremap' option value.
3590 */
3591 char *
3592did_set_langremap(optset_T *args UNUSED)
3593{
3594 // 'langremap' -> !'langnoremap'
3595 p_lnr = !p_lrm;
3596 return NULL;
3597}
3598#endif
3599
3600/*
3601 * Process the new 'laststatus' option value.
3602 */
3603 char *
3604did_set_laststatus(optset_T *args UNUSED)
3605{
3606 last_status(FALSE); // (re)set last window status line
3607 return NULL;
3608}
3609
3610#if defined(FEAT_GUI) || defined(PROTO)
3611/*
3612 * Process the new 'linespace' option value.
3613 */
3614 char *
3615did_set_linespace(optset_T *args UNUSED)
3616{
3617 // Recompute gui.char_height and resize the Vim window to keep the
3618 // same number of lines.
3619 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3620 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3621 return NULL;
3622}
3623#endif
3624
3625/*
3626 * Process the updated 'lisp' option value.
3627 */
3628 char *
3629did_set_lisp(optset_T *args UNUSED)
3630{
3631 // When 'lisp' option changes include/exclude '-' in keyword characters.
3632 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3633 return NULL;
3634}
3635
3636/*
3637 * Process the new 'maxcombine' option value.
3638 */
3639 char *
3640did_set_maxcombine(optset_T *args UNUSED)
3641{
3642 if (p_mco > MAX_MCO)
3643 p_mco = MAX_MCO;
3644 else if (p_mco < 0)
3645 p_mco = 0;
3646 screenclear(); // will re-allocate the screen
3647 return NULL;
3648}
3649
3650/*
3651 * Process the updated 'modifiable' option value.
3652 */
3653 char *
3654did_set_modifiable(optset_T *args UNUSED)
3655{
3656 // when 'modifiable' is changed, redraw the window title
3657
3658# ifdef FEAT_TERMINAL
3659 // Cannot set 'modifiable' when in Terminal mode.
3660 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3661 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3662 {
3663 curbuf->b_p_ma = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003664 return e_cannot_make_terminal_with_running_job_modifiable;
3665 }
3666# endif
3667 redraw_titles();
3668
3669 return NULL;
3670}
3671
3672/*
3673 * Process the updated 'modified' option value.
3674 */
3675 char *
3676did_set_modified(optset_T *args)
3677{
3678 if (!args->os_newval.boolean)
3679 save_file_ff(curbuf); // Buffer is unchanged
3680 redraw_titles();
3681 modified_was_set = args->os_newval.boolean;
3682 return NULL;
3683}
3684
3685#if defined(FEAT_GUI) || defined(PROTO)
3686/*
3687 * Process the updated 'mousehide' option value.
3688 */
3689 char *
3690did_set_mousehide(optset_T *args UNUSED)
3691{
3692 if (!p_mh)
3693 gui_mch_mousehide(FALSE);
3694 return NULL;
3695}
3696#endif
3697
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003698/*
3699 * Process the updated 'number' or 'relativenumber' option value.
3700 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003701 char *
3702did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003703{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003704#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003705 if (gui.in_use
3706 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3707 && curbuf->b_signlist != NULL)
3708 {
3709 // If the 'number' or 'relativenumber' options are modified and
3710 // 'signcolumn' is set to 'number', then clear the screen for a full
3711 // refresh. Otherwise the sign icons are not displayed properly in the
3712 // number column. If the 'number' option is set and only the
3713 // 'relativenumber' option is toggled, then don't refresh the screen
3714 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003715 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003716 redraw_all_later(UPD_CLEAR);
3717 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003718#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003719 return NULL;
3720}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003721
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003722#if defined(FEAT_LINEBREAK) || defined(PROTO)
3723/*
3724 * Process the new 'numberwidth' option value.
3725 */
3726 char *
3727did_set_numberwidth(optset_T *args UNUSED)
3728{
3729 char *errmsg = NULL;
3730
3731 // 'numberwidth' must be positive
3732 if (curwin->w_p_nuw < 1)
3733 {
3734 errmsg = e_argument_must_be_positive;
3735 curwin->w_p_nuw = 1;
3736 }
3737 if (curwin->w_p_nuw > 20)
3738 {
3739 errmsg = e_invalid_argument;
3740 curwin->w_p_nuw = 20;
3741 }
3742 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3743
3744 return errmsg;
3745}
3746#endif
3747
3748/*
3749 * Process the updated 'paste' option value. Called after p_paste was set or
3750 * reset. When 'paste' is set or reset also change other options.
3751 */
3752 char *
3753did_set_paste(optset_T *args UNUSED)
3754{
3755 static int old_p_paste = FALSE;
3756 static int save_sm = 0;
3757 static int save_sta = 0;
3758 static int save_ru = 0;
3759#ifdef FEAT_RIGHTLEFT
3760 static int save_ri = 0;
3761 static int save_hkmap = 0;
3762#endif
3763 buf_T *buf;
3764
3765 if (p_paste)
3766 {
3767 // Paste switched from off to on.
3768 // Save the current values, so they can be restored later.
3769 if (!old_p_paste)
3770 {
3771 // save options for each buffer
3772 FOR_ALL_BUFFERS(buf)
3773 {
3774 buf->b_p_tw_nopaste = buf->b_p_tw;
3775 buf->b_p_wm_nopaste = buf->b_p_wm;
3776 buf->b_p_sts_nopaste = buf->b_p_sts;
3777 buf->b_p_ai_nopaste = buf->b_p_ai;
3778 buf->b_p_et_nopaste = buf->b_p_et;
3779#ifdef FEAT_VARTABS
3780 if (buf->b_p_vsts_nopaste)
3781 vim_free(buf->b_p_vsts_nopaste);
3782 buf->b_p_vsts_nopaste =
3783 buf->b_p_vsts && buf->b_p_vsts != empty_option
3784 ? vim_strsave(buf->b_p_vsts) : NULL;
3785#endif
3786 }
3787
3788 // save global options
3789 save_sm = p_sm;
3790 save_sta = p_sta;
3791 save_ru = p_ru;
3792#ifdef FEAT_RIGHTLEFT
3793 save_ri = p_ri;
3794 save_hkmap = p_hkmap;
3795#endif
3796 // save global values for local buffer options
3797 p_ai_nopaste = p_ai;
3798 p_et_nopaste = p_et;
3799 p_sts_nopaste = p_sts;
3800 p_tw_nopaste = p_tw;
3801 p_wm_nopaste = p_wm;
3802#ifdef FEAT_VARTABS
3803 if (p_vsts_nopaste)
3804 vim_free(p_vsts_nopaste);
3805 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3806 ? vim_strsave(p_vsts) : NULL;
3807#endif
3808 }
3809
3810 // Always set the option values, also when 'paste' is set when it is
3811 // already on. Set options for each buffer.
3812 FOR_ALL_BUFFERS(buf)
3813 {
3814 buf->b_p_tw = 0; // textwidth is 0
3815 buf->b_p_wm = 0; // wrapmargin is 0
3816 buf->b_p_sts = 0; // softtabstop is 0
3817 buf->b_p_ai = 0; // no auto-indent
3818 buf->b_p_et = 0; // no expandtab
3819#ifdef FEAT_VARTABS
3820 if (buf->b_p_vsts)
3821 free_string_option(buf->b_p_vsts);
3822 buf->b_p_vsts = empty_option;
3823 VIM_CLEAR(buf->b_p_vsts_array);
3824#endif
3825 }
3826
3827 // set global options
3828 p_sm = 0; // no showmatch
3829 p_sta = 0; // no smarttab
3830 if (p_ru)
3831 status_redraw_all(); // redraw to remove the ruler
3832 p_ru = 0; // no ruler
3833#ifdef FEAT_RIGHTLEFT
3834 p_ri = 0; // no reverse insert
3835 p_hkmap = 0; // no Hebrew keyboard
3836#endif
3837 // set global values for local buffer options
3838 p_tw = 0;
3839 p_wm = 0;
3840 p_sts = 0;
3841 p_ai = 0;
Christian Brabandt757593c2023-08-22 21:44:10 +02003842 p_et = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003843#ifdef FEAT_VARTABS
3844 if (p_vsts)
3845 free_string_option(p_vsts);
3846 p_vsts = empty_option;
3847#endif
3848 }
3849
3850 // Paste switched from on to off: Restore saved values.
3851 else if (old_p_paste)
3852 {
3853 // restore options for each buffer
3854 FOR_ALL_BUFFERS(buf)
3855 {
3856 buf->b_p_tw = buf->b_p_tw_nopaste;
3857 buf->b_p_wm = buf->b_p_wm_nopaste;
3858 buf->b_p_sts = buf->b_p_sts_nopaste;
3859 buf->b_p_ai = buf->b_p_ai_nopaste;
3860 buf->b_p_et = buf->b_p_et_nopaste;
3861#ifdef FEAT_VARTABS
3862 if (buf->b_p_vsts)
3863 free_string_option(buf->b_p_vsts);
3864 buf->b_p_vsts = buf->b_p_vsts_nopaste
3865 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3866 vim_free(buf->b_p_vsts_array);
3867 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3868 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3869 else
3870 buf->b_p_vsts_array = NULL;
3871#endif
3872 }
3873
3874 // restore global options
3875 p_sm = save_sm;
3876 p_sta = save_sta;
3877 if (p_ru != save_ru)
3878 status_redraw_all(); // redraw to draw the ruler
3879 p_ru = save_ru;
3880#ifdef FEAT_RIGHTLEFT
3881 p_ri = save_ri;
3882 p_hkmap = save_hkmap;
3883#endif
3884 // set global values for local buffer options
3885 p_ai = p_ai_nopaste;
3886 p_et = p_et_nopaste;
3887 p_sts = p_sts_nopaste;
3888 p_tw = p_tw_nopaste;
3889 p_wm = p_wm_nopaste;
3890#ifdef FEAT_VARTABS
3891 if (p_vsts)
3892 free_string_option(p_vsts);
3893 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
3894#endif
3895 }
3896
3897 old_p_paste = p_paste;
3898
Christian Brabandt757593c2023-08-22 21:44:10 +02003899#if defined(FEAT_EVAL) || defined(PROTO)
3900 // Remember where the dependent options were reset
3901 didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
3902#endif
3903
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003904 return NULL;
3905}
3906
3907
3908#ifdef FEAT_QUICKFIX
3909/*
3910 * Process the updated 'previewwindow' option value.
3911 */
3912 char *
Yegappan Lakshmanan3ee25962023-12-04 20:31:14 +01003913did_set_previewwindow(optset_T *args UNUSED)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003914{
3915 if (!curwin->w_p_pvw)
3916 return NULL;
3917
3918 // There can be only one window with 'previewwindow' set.
3919 win_T *win;
3920
3921 FOR_ALL_WINDOWS(win)
3922 if (win->w_p_pvw && win != curwin)
3923 {
3924 curwin->w_p_pvw = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003925 return e_preview_window_already_exists;
3926 }
3927
3928 return NULL;
3929}
3930#endif
3931
3932#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
3933/*
3934 * Process the new 'pyxversion' option value.
3935 */
3936 char *
3937did_set_pyxversion(optset_T *args UNUSED)
3938{
3939 char *errmsg = NULL;
3940
3941 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3942 errmsg = e_invalid_argument;
3943
3944 return errmsg;
3945}
3946#endif
3947
3948/*
3949 * Process the updated 'readonly' option value.
3950 */
3951 char *
3952did_set_readonly(optset_T *args)
3953{
3954 // when 'readonly' is reset globally, also reset readonlymode
3955 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
3956 readonlymode = FALSE;
3957
3958 // when 'readonly' is set may give W10 again
3959 if (curbuf->b_p_ro)
3960 curbuf->b_did_warn = FALSE;
3961
3962 redraw_titles();
3963
3964 return NULL;
3965}
3966
3967/*
3968 * Process the updated 'scrollbind' option value.
3969 */
3970 char *
3971did_set_scrollbind(optset_T *args UNUSED)
3972{
3973 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3974 // at the end of normal_cmd()
3975 if (!curwin->w_p_scb)
3976 return NULL;
3977
3978 do_check_scrollbind(FALSE);
3979 curwin->w_scbind_pos = curwin->w_topline;
3980 return NULL;
3981}
3982
3983#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3984/*
3985 * Process the updated 'shellslash' option value.
3986 */
3987 char *
3988did_set_shellslash(optset_T *args UNUSED)
3989{
3990 if (p_ssl)
3991 {
3992 psepc = '/';
3993 psepcN = '\\';
3994 pseps[0] = '/';
3995 }
3996 else
3997 {
3998 psepc = '\\';
3999 psepcN = '/';
4000 pseps[0] = '\\';
4001 }
4002
4003 // need to adjust the file name arguments and buffer names.
4004 buflist_slash_adjust();
4005 alist_slash_adjust();
4006# ifdef FEAT_EVAL
4007 scriptnames_slash_adjust();
4008# endif
4009 return NULL;
4010}
4011#endif
4012
4013/*
4014 * Process the new 'shiftwidth' or the 'tabstop' option value.
4015 */
4016 char *
4017did_set_shiftwidth_tabstop(optset_T *args)
4018{
4019 long *pp = (long *)args->os_varp;
4020 char *errmsg = NULL;
4021
4022 if (curbuf->b_p_sw < 0)
4023 {
4024 errmsg = e_argument_must_be_positive;
4025#ifdef FEAT_VARTABS
4026 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4027 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4028 ? tabstop_first(curbuf->b_p_vts_array)
4029 : curbuf->b_p_ts;
4030#else
4031 curbuf->b_p_sw = curbuf->b_p_ts;
4032#endif
4033 }
4034
4035#ifdef FEAT_FOLDING
4036 if (foldmethodIsIndent(curwin))
4037 foldUpdateAll(curwin);
4038#endif
4039 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4040 // parse 'cinoptions'.
4041 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4042 parse_cino(curbuf);
4043
4044 return errmsg;
4045}
4046
4047/*
4048 * Process the new 'showtabline' option value.
4049 */
4050 char *
4051did_set_showtabline(optset_T *args UNUSED)
4052{
4053 shell_new_rows(); // recompute window positions and heights
4054 return NULL;
4055}
4056
4057/*
4058 * Process the updated 'smoothscroll' option value.
4059 */
4060 char *
4061did_set_smoothscroll(optset_T *args UNUSED)
4062{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004063 if (!curwin->w_p_sms)
4064 curwin->w_skipcol = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004065
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004066 return NULL;
4067}
4068
4069#if defined(FEAT_SPELL) || defined(PROTO)
4070/*
4071 * Process the updated 'spell' option value.
4072 */
4073 char *
4074did_set_spell(optset_T *args UNUSED)
4075{
4076 if (curwin->w_p_spell)
4077 return parse_spelllang(curwin);
4078
4079 return NULL;
4080}
4081#endif
4082
4083/*
4084 * Process the updated 'swapfile' option value.
4085 */
4086 char *
4087did_set_swapfile(optset_T *args UNUSED)
4088{
4089 // when 'swf' is set, create swapfile, when reset remove swapfile
4090 if (curbuf->b_p_swf && p_uc)
4091 ml_open_file(curbuf); // create the swap file
4092 else
4093 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4094 // buf->b_p_swf
4095 mf_close_file(curbuf, TRUE); // remove the swap file
4096 return NULL;
4097}
4098
4099#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004100 char *
4101did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004102{
4103# ifdef FEAT_VTP
4104 // Do not turn on 'tgc' when 24-bit colors are not supported.
4105 if (
4106# ifdef VIMDLL
4107 !gui.in_use && !gui.starting &&
4108# endif
4109 !has_vtp_working())
4110 {
4111 p_tgc = 0;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004112 return e_24_bit_colors_are_not_supported_on_this_environment;
4113 }
4114 if (is_term_win32())
4115 swap_tcap();
4116# endif
4117# ifdef FEAT_GUI
4118 if (!gui.in_use && !gui.starting)
4119# endif
4120 highlight_gui_started();
4121# ifdef FEAT_VTP
4122 // reset t_Co
4123 if (is_term_win32())
4124 {
4125 control_console_color_rgb();
4126 set_termname(T_NAME);
4127 init_highlight(TRUE, FALSE);
4128 }
4129# endif
4130# ifdef FEAT_TERMINAL
4131 term_update_colors_all();
4132 term_update_palette_all();
4133 term_update_wincolor_all();
4134# endif
4135
4136 return NULL;
4137}
4138#endif
4139
4140/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004141 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004143 char *
4144did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004146 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004148 // when 'terse' is set change 'shortmess'
4149 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004151 // insert 's' in p_shm
4152 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004153 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004154 STRCPY(IObuff, p_shm);
4155 STRCAT(IObuff, "s");
4156 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004157 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004158 // remove 's' from p_shm
4159 else if (!p_terse && p != NULL)
4160 STRMOVE(p, p + 1);
4161 return NULL;
4162}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004164/*
4165 * Process the updated 'textauto' option value.
4166 */
4167 char *
4168did_set_textauto(optset_T *args)
4169{
4170 // when 'textauto' is set or reset also change 'fileformats'
4171 set_string_option_direct((char_u *)"ffs", -1,
4172 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4173 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004174
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004175 return NULL;
4176}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004178/*
4179 * Process the updated 'textmode' option value.
4180 */
4181 char *
4182did_set_textmode(optset_T *args)
4183{
4184 // when 'textmode' is set or reset also change 'fileformat'
4185 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4186
4187 return NULL;
4188}
4189
4190/*
4191 * Process the new 'textwidth' option value.
4192 */
4193 char *
4194did_set_textwidth(optset_T *args UNUSED)
4195{
4196 char *errmsg = NULL;
4197
4198 if (curbuf->b_p_tw < 0)
4199 {
4200 errmsg = e_argument_must_be_positive;
4201 curbuf->b_p_tw = 0;
4202 }
4203#ifdef FEAT_SYN_HL
4204 {
4205 win_T *wp;
4206 tabpage_T *tp;
4207
4208 FOR_ALL_TAB_WINDOWS(tp, wp)
4209 check_colorcolumn(wp);
4210 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004211#endif
4212
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004213 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214}
4215
4216/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004217 * Process the updated 'title' or the 'icon' option value.
4218 */
4219 char *
4220did_set_title_icon(optset_T *args UNUSED)
4221{
4222 // when 'title' changed, may need to change the title; same for 'icon'
4223 did_set_title();
4224 return NULL;
4225}
4226
4227/*
4228 * Process the new 'titlelen' option value.
4229 */
4230 char *
4231did_set_titlelen(optset_T *args)
4232{
4233 long old_value = args->os_oldval.number;
4234 char *errmsg = NULL;
4235
4236 // if 'titlelen' has changed, redraw the title
4237 if (p_titlelen < 0)
4238 {
4239 errmsg = e_argument_must_be_positive;
4240 p_titlelen = 85;
4241 }
4242 if (starting != NO_SCREEN && old_value != p_titlelen)
4243 need_maketitle = TRUE;
4244
4245 return errmsg;
4246}
4247
4248#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4249/*
4250 * Process the updated 'undofile' option value.
4251 */
4252 char *
4253did_set_undofile(optset_T *args)
4254{
4255 // Only take action when the option was set.
4256 if (!curbuf->b_p_udf && !p_udf)
4257 return NULL;
4258
4259 // When reset we do not delete the undo file, the option may be set again
4260 // without making any changes in between.
4261 char_u hash[UNDO_HASH_SIZE];
4262 buf_T *save_curbuf = curbuf;
4263
4264 FOR_ALL_BUFFERS(curbuf)
4265 {
4266 // When 'undofile' is set globally: for every buffer, otherwise
4267 // only for the current buffer: Try to read in the undofile,
4268 // if one exists, the buffer wasn't changed and the buffer was
4269 // loaded
4270 if ((curbuf == save_curbuf
4271 || (args->os_flags & OPT_GLOBAL)
4272 || args->os_flags == 0)
4273 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4274 {
4275#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004276 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004277 continue;
4278#endif
4279 u_compute_hash(hash);
4280 u_read_undo(NULL, hash, curbuf->b_fname);
4281 }
4282 }
4283 curbuf = save_curbuf;
4284
4285 return NULL;
4286}
4287#endif
4288
4289/*
4290 * Process the new global 'undolevels' option value.
4291 */
4292 static void
4293update_global_undolevels(long value, long old_value)
4294{
4295 // sync undo before 'undolevels' changes
4296
4297 // use the old value, otherwise u_sync() may not work properly
4298 p_ul = old_value;
4299 u_sync(TRUE);
4300 p_ul = value;
4301}
4302
4303/*
4304 * Process the new buffer local 'undolevels' option value.
4305 */
4306 static void
4307update_buflocal_undolevels(long value, long old_value)
4308{
4309 // use the old value, otherwise u_sync() may not work properly
4310 curbuf->b_p_ul = old_value;
4311 u_sync(TRUE);
4312 curbuf->b_p_ul = value;
4313}
4314
4315/*
4316 * Process the new 'undolevels' option value.
4317 */
4318 char *
4319did_set_undolevels(optset_T *args)
4320{
4321 long *pp = (long *)args->os_varp;
4322
4323 if (pp == &p_ul) // global 'undolevels'
4324 update_global_undolevels(args->os_newval.number,
4325 args->os_oldval.number);
4326 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4327 update_buflocal_undolevels(args->os_newval.number,
4328 args->os_oldval.number);
4329
4330 return NULL;
4331}
4332
4333/*
4334 * Process the new 'updatecount' option value.
4335 */
4336 char *
4337did_set_updatecount(optset_T *args)
4338{
4339 long old_value = args->os_oldval.number;
4340 char *errmsg = NULL;
4341
4342 // when 'updatecount' changes from zero to non-zero, open swap files
4343 if (p_uc < 0)
4344 {
4345 errmsg = e_argument_must_be_positive;
4346 p_uc = 100;
4347 }
4348 if (p_uc && !old_value)
4349 ml_open_files();
4350
4351 return errmsg;
4352}
4353
4354/*
4355 * Process the updated 'weirdinvert' option value.
4356 */
4357 char *
4358did_set_weirdinvert(optset_T *args)
4359{
4360 // When 'weirdinvert' changed, set/reset 't_xs'.
4361 // Then set 'weirdinvert' according to value of 't_xs'.
4362 if (p_wiv && !args->os_oldval.boolean)
4363 T_XS = (char_u *)"y";
4364 else if (!p_wiv && args->os_oldval.boolean)
4365 T_XS = empty_option;
4366 p_wiv = (*T_XS != NUL);
4367
4368 return NULL;
4369}
4370
4371/*
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02004372 * Process the new 'wildchar' / 'wildcharm' option value.
4373 */
4374 char *
4375did_set_wildchar(optset_T *args)
4376{
4377 long c = *(long *)args->os_varp;
4378
4379 // Don't allow key values that wouldn't work as wildchar.
4380 if (c == Ctrl_C || c == '\n' || c == '\r' || c == K_KENTER)
4381 return e_invalid_argument;
4382
4383 return NULL;
4384}
4385
4386/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004387 * Process the new 'window' option value.
4388 */
4389 char *
4390did_set_window(optset_T *args UNUSED)
4391{
4392 if (p_window < 1)
4393 p_window = 1;
4394 else if (p_window >= Rows)
4395 p_window = Rows - 1;
4396 return NULL;
4397}
4398
4399/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004400 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004402 char *
4403did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004405 long *pp = (long *)args->os_varp;
4406 char *errmsg = NULL;
4407
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004408 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004410 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004411 p_wh = 1;
4412 }
4413 if (p_wmh > p_wh)
4414 {
4415 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4416 p_wh = p_wmh;
4417 }
4418 if (p_hh < 0)
4419 {
4420 errmsg = e_argument_must_be_positive;
4421 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 }
4423
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004424 // Change window height NOW
4425 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004427 if (pp == &p_wh && curwin->w_height < p_wh)
4428 win_setheight((int)p_wh);
4429 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4430 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004433 return errmsg;
4434}
4435
4436/*
4437 * Process the new 'winminheight' option value.
4438 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004439 char *
4440did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004441{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004442 char *errmsg = NULL;
4443
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004444 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004446 errmsg = e_argument_must_be_positive;
4447 p_wmh = 0;
4448 }
4449 if (p_wmh > p_wh)
4450 {
4451 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4452 p_wmh = p_wh;
4453 }
4454 win_setminheight();
4455
4456 return errmsg;
4457}
4458
4459/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004460 * Process the new 'winminwidth' option value.
4461 */
4462 char *
4463did_set_winminwidth(optset_T *args UNUSED)
4464{
4465 char *errmsg = NULL;
4466
4467 if (p_wmw < 0)
4468 {
4469 errmsg = e_argument_must_be_positive;
4470 p_wmw = 0;
4471 }
4472 if (p_wmw > p_wiw)
4473 {
4474 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4475 p_wmw = p_wiw;
4476 }
4477 win_setminwidth();
4478
4479 return errmsg;
4480}
4481
4482/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004483 * Process the new 'winwidth' option value.
4484 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004485 char *
4486did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004487{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004488 char *errmsg = NULL;
4489
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004490 if (p_wiw < 1)
4491 {
4492 errmsg = e_argument_must_be_positive;
4493 p_wiw = 1;
4494 }
4495 if (p_wmw > p_wiw)
4496 {
4497 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4498 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004500
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004501 // Change window width NOW
4502 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4503 win_setwidth((int)p_wiw);
4504
4505 return errmsg;
4506}
4507
4508/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004509 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004510 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004511 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004512did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004513{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004514 // Set w_leftcol or w_skipcol to zero.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004515 if (curwin->w_p_wrap)
4516 curwin->w_leftcol = 0;
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004517 else
4518 curwin->w_skipcol = 0;
4519
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004520 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004521}
4522
4523/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004524 * Set the value of a boolean option, and take care of side effects.
4525 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004526 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004527 static char *
4528set_bool_option(
4529 int opt_idx, // index in options[] table
4530 char_u *varp, // pointer to the option variable
4531 int value, // new value
4532 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004533{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004534 int old_value = *(int *)varp;
4535#if defined(FEAT_EVAL)
4536 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004538 char *errmsg = NULL;
4539
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004540 // Disallow changing some options from secure mode
4541 if ((secure
4542#ifdef HAVE_SANDBOX
4543 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004544#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004545 ) && (options[opt_idx].flags & P_SECURE))
4546 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004547
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004548#if defined(FEAT_EVAL)
4549 // Save the global value before changing anything. This is needed as for
4550 // a global-only option setting the "local value" in fact sets the global
4551 // value (since there is only one value).
4552 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4553 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4554 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004556
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004557 *(int *)varp = value; // set the new value
4558#ifdef FEAT_EVAL
4559 // Remember where the option was set.
4560 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004561#endif
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004564 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004567 // May set global value for local option.
4568 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4569 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004570
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004571 // Handle side effects of changing a bool option.
4572 if (options[opt_idx].opt_did_set_cb != NULL)
4573 {
4574 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004575
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004576 CLEAR_FIELD(args);
4577 args.os_varp = varp;
4578 args.os_flags = opt_flags;
4579 args.os_oldval.boolean = old_value;
4580 args.os_newval.boolean = value;
4581 args.os_errbuf = NULL;
4582 errmsg = options[opt_idx].opt_did_set_cb(&args);
zeertzjqdeba02d2023-11-02 21:01:19 +01004583 if (errmsg != NULL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004584 return errmsg;
4585 }
4586
4587 // after handling side effects, call autocommand
4588
4589 options[opt_idx].flags |= P_WAS_SET;
4590
4591#if defined(FEAT_EVAL)
4592 apply_optionset_autocmd(opt_idx, opt_flags,
4593 (long)(old_value ? TRUE : FALSE),
4594 (long)(old_global_value ? TRUE : FALSE),
4595 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004596#endif
4597
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004598 comp_col(); // in case 'ruler' or 'showcmd' changed
4599 if (curwin->w_curswant != MAXCOL
4600 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
4601 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004602
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004603 if ((opt_flags & OPT_NO_REDRAW) == 0)
4604 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004605
4606 return errmsg;
4607}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004608
4609/*
4610 * Check the bounds of numeric options.
4611 */
4612 static char *
4613check_num_option_bounds(
4614 long *pp,
4615 long old_value,
4616 long old_Rows,
4617 long old_Columns,
4618 char *errbuf,
4619 size_t errbuflen,
4620 char *errmsg)
4621{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 if (Rows < min_rows() && full_screen)
4623 {
4624 if (errbuf != NULL)
4625 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004626 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004627 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 errmsg = errbuf;
4629 }
4630 Rows = min_rows();
4631 }
4632 if (Columns < MIN_COLUMNS && full_screen)
4633 {
4634 if (errbuf != NULL)
4635 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004636 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004637 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 errmsg = errbuf;
4639 }
4640 Columns = MIN_COLUMNS;
4641 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004642 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004644 // If the screen (shell) height has been changed, assume it is the
4645 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (old_Rows != Rows || old_Columns != Columns)
4647 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004648 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 if (updating_screen)
4650 *pp = old_value;
4651 else if (full_screen
4652#ifdef FEAT_GUI
4653 && !gui.starting
4654#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004655 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 set_shellsize((int)Columns, (int)Rows, TRUE);
4657 else
4658 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004659 // Postpone the resizing; check the size and cmdline position for
4660 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 check_shellsize();
4662 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4663 cmdline_row = Rows - p_ch;
4664 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004665 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004666 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 }
4668
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (curbuf->b_p_ts <= 0)
4670 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004671 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 curbuf->b_p_ts = 8;
4673 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004674 else if (curbuf->b_p_ts > TABSTOP_MAX)
4675 {
4676 errmsg = e_invalid_argument;
4677 curbuf->b_p_ts = 8;
4678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (p_tm < 0)
4680 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004681 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 p_tm = 0;
4683 }
4684 if ((curwin->w_p_scr <= 0
4685 || (curwin->w_p_scr > curwin->w_height
4686 && curwin->w_height > 0))
4687 && full_screen)
4688 {
4689 if (pp == &(curwin->w_p_scr))
4690 {
4691 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004692 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 win_comp_scroll(curwin);
4694 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004695 // If 'scroll' became invalid because of a side effect silently adjust
4696 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 else if (curwin->w_p_scr <= 0)
4698 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004699 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 curwin->w_p_scr = curwin->w_height;
4701 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004702 if (p_hi < 0)
4703 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004704 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004705 p_hi = 0;
4706 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004707 else if (p_hi > 10000)
4708 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004709 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004710 p_hi = 10000;
4711 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004712 if (p_re < 0 || p_re > 2)
4713 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004714 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004715 p_re = 0;
4716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 if (p_report < 0)
4718 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004719 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 p_report = 1;
4721 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004722 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004724 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 p_sj = Rows / 2;
4726 else
4727 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004728 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 p_sj = 1;
4730 }
4731 }
4732 if (p_so < 0 && full_screen)
4733 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004734 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 p_so = 0;
4736 }
4737 if (p_siso < 0 && full_screen)
4738 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004739 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 p_siso = 0;
4741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 if (p_cwh < 1)
4743 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004744 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 p_cwh = 1;
4746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (p_ut < 0)
4748 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004749 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 p_ut = 2000;
4751 }
4752 if (p_ss < 0)
4753 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004754 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 p_ss = 0;
4756 }
4757
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004758 return errmsg;
4759}
4760
4761/*
4762 * Set the value of a number option, and take care of side effects.
4763 * Returns NULL for success, or an error message for an error.
4764 */
4765 static char *
4766set_num_option(
4767 int opt_idx, // index in options[] table
4768 char_u *varp, // pointer to the option variable
4769 long value, // new value
4770 char *errbuf, // buffer for error messages
4771 size_t errbuflen, // length of "errbuf"
4772 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4773 // OPT_MODELINE, etc.
4774{
4775 char *errmsg = NULL;
4776 long old_value = *(long *)varp;
4777#if defined(FEAT_EVAL)
4778 long old_global_value = 0; // only used when setting a local and
4779 // global option
4780#endif
4781 long old_Rows = Rows; // remember old Rows
4782 long old_Columns = Columns; // remember old Columns
4783 long *pp = (long *)varp;
4784
4785 // Disallow changing some options from secure mode.
4786 if ((secure
4787#ifdef HAVE_SANDBOX
4788 || sandbox != 0
4789#endif
4790 ) && (options[opt_idx].flags & P_SECURE))
4791 return e_not_allowed_here;
4792
4793#if defined(FEAT_EVAL)
4794 // Save the global value before changing anything. This is needed as for
4795 // a global-only option setting the "local value" in fact sets the global
4796 // value (since there is only one value).
4797 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4798 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4799 OPT_GLOBAL);
4800#endif
4801
4802 *pp = value;
4803#ifdef FEAT_EVAL
4804 // Remember where the option was set.
4805 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4806#endif
4807#ifdef FEAT_GUI
4808 need_mouse_correct = TRUE;
4809#endif
4810
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004811 // Invoke the option specific callback function to validate and apply the
4812 // new value.
4813 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004814 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004815 optset_T args;
4816
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004817 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004818 args.os_varp = varp;
4819 args.os_flags = opt_flags;
4820 args.os_oldval.number = old_value;
4821 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004822 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004823 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004824 }
4825
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004826 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004827 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4828 errbuf, errbuflen, errmsg);
4829
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004830 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4832 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4833
4834 options[opt_idx].flags |= P_WAS_SET;
4835
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004836#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004837 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4838 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004839#endif
4840
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004841 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004842 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004843 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004844 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004845 if ((opt_flags & OPT_NO_REDRAW) == 0)
4846 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
4848 return errmsg;
4849}
4850
4851/*
4852 * Called after an option changed: check if something needs to be redrawn.
4853 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004854 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004855check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004857 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004858 int doclear = (flags & P_RCLR) == P_RCLR;
4859 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004861 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
4864 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4865 changed_window_setting();
4866 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004867 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004868 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004869 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004870 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004871 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004873 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874}
4875
4876/*
4877 * Find index for option 'arg'.
4878 * Return -1 if not found.
4879 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004880 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004881findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882{
4883 int opt_idx;
4884 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004885 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 int is_term_opt;
4887
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004888 // For first call: Initialize the quick-access table.
4889 // It contains the index for the first option that starts with a certain
4890 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 if (quick_tab[1] == 0)
4892 {
4893 p = options[0].fullname;
4894 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4895 {
4896 if (s[0] != p[0])
4897 {
4898 if (s[0] == 't' && s[1] == '_')
4899 quick_tab[26] = opt_idx;
4900 else
4901 quick_tab[CharOrdLow(s[0])] = opt_idx;
4902 }
4903 p = s;
4904 }
4905 }
4906
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004907 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 return -1;
4910
4911 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4912 if (is_term_opt)
4913 opt_idx = quick_tab[26];
4914 else
4915 opt_idx = quick_tab[CharOrdLow(arg[0])];
zeertzjqf48558e2023-12-08 21:34:31 +01004916 for (; (s = options[opt_idx].fullname) != NULL && s[0] == arg[0]; opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004918 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 break;
4920 }
zeertzjqf48558e2023-12-08 21:34:31 +01004921 if (s != NULL && s[0] != arg[0])
4922 s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 if (s == NULL && !is_term_opt)
4924 {
4925 opt_idx = quick_tab[CharOrdLow(arg[0])];
4926 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4927 {
4928 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004929 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 break;
4931 s = NULL;
4932 }
4933 }
4934 if (s == NULL)
4935 opt_idx = -1;
4936 return opt_idx;
4937}
4938
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004939#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4940 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941/*
4942 * Get the value for an option.
4943 *
4944 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004945 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004946 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004947 * String option: gov_string, *stringval gets allocated string.
4948 * Hidden Number option: gov_hidden_number.
4949 * Hidden Toggle option: gov_hidden_bool.
4950 * Hidden String option: gov_hidden_string.
4951 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004952 *
4953 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004955 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004956get_option_value(
4957 char_u *name,
4958 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004959 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004960 int *flagsp,
4961 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
4963 int opt_idx;
4964 char_u *varp;
4965
4966 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004967 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004968 {
4969 int key;
4970
4971 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004972 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004973 {
4974 char_u key_name[2];
4975 char_u *p;
4976
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004977 if (flagsp != NULL)
4978 *flagsp = 0; // terminal option has no flags
4979
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004980 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004981 if (key < 0)
4982 {
4983 key_name[0] = KEY2TERMCAP0(key);
4984 key_name[1] = KEY2TERMCAP1(key);
4985 }
4986 else
4987 {
4988 key_name[0] = KS_KEY;
4989 key_name[1] = (key & 0xff);
4990 }
4991 p = find_termcode(key_name);
4992 if (p != NULL)
4993 {
4994 if (stringval != NULL)
4995 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004996 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004997 }
4998 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004999 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01005000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005002 varp = get_varp_scope(&(options[opt_idx]), scope);
5003
5004 if (flagsp != NULL)
5005 // Return the P_xxxx option flags.
5006 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
5008 if (options[opt_idx].flags & P_STRING)
5009 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005010 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005011 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 if (stringval != NULL)
5013 {
zeertzjq51f0bc32022-05-09 13:33:39 +01005014 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005015 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5016 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005018 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005019 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005020 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005023 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 *stringval = vim_strsave(*(char_u **)(varp));
5025 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005026 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005029 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005030 return (options[opt_idx].flags & P_NUM)
5031 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if (options[opt_idx].flags & P_NUM)
5033 *numval = *(long *)varp;
5034 else
5035 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005036 // Special case: 'modified' is b_changed, but we also want to consider
5037 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 if ((int *)varp == &curbuf->b_changed)
5039 *numval = curbufIsChanged();
5040 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005041 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005043 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044}
5045#endif
5046
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005047#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005048/*
5049 * Returns the option attributes and its value. Unlike the above function it
5050 * will return either global value or local value of the option depending on
5051 * what was requested, but it will never return global value if it was
5052 * requested to return local one and vice versa. Neither it will return
5053 * buffer-local value if it was requested to return window-local one.
5054 *
5055 * Pretends that option is absent if it is not present in the requested scope
5056 * (i.e. has no global, window-local or buffer-local value depending on
5057 * opt_type). Uses
5058 *
5059 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005060 * 0 hidden or unknown option, also option that does not have requested
5061 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005062 * see SOPT_* in vim.h for other flags
5063 *
5064 * Possible opt_type values: see SREQ_* in vim.h
5065 */
5066 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005067get_option_value_strict(
5068 char_u *name,
5069 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005070 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005071 int opt_type,
5072 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005073{
5074 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005075 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005076 struct vimoption *p;
5077 int r = 0;
5078
5079 opt_idx = findoption(name);
5080 if (opt_idx < 0)
5081 return 0;
5082
5083 p = &(options[opt_idx]);
5084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005085 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005086 if (p->var == NULL)
5087 return 0;
5088
5089 if (p->flags & P_BOOL)
5090 r |= SOPT_BOOL;
5091 else if (p->flags & P_NUM)
5092 r |= SOPT_NUM;
5093 else if (p->flags & P_STRING)
5094 r |= SOPT_STRING;
5095
5096 if (p->indir == PV_NONE)
5097 {
5098 if (opt_type == SREQ_GLOBAL)
5099 r |= SOPT_GLOBAL;
5100 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005101 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005102 }
5103 else
5104 {
5105 if (p->indir & PV_BOTH)
5106 r |= SOPT_GLOBAL;
5107 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005108 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005109
5110 if (p->indir & PV_WIN)
5111 {
5112 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005113 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005114 else
5115 r |= SOPT_WIN;
5116 }
5117 else if (p->indir & PV_BUF)
5118 {
5119 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005120 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005121 else
5122 r |= SOPT_BUF;
5123 }
5124 }
5125
5126 if (stringval == NULL)
5127 return r;
5128
5129 if (opt_type == SREQ_GLOBAL)
5130 varp = p->var;
5131 else
5132 {
5133 if (opt_type == SREQ_BUF)
5134 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005135 // Special case: 'modified' is b_changed, but we also want to
5136 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005137 if (p->indir == PV_MOD)
5138 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005139 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005140 varp = NULL;
5141 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005142# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005143 else if (p->indir == PV_KEY)
5144 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005145 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005146 *stringval = NULL;
5147 varp = NULL;
5148 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005149# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005150 else
5151 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005152 buf_T *save_curbuf = curbuf;
5153
5154 // only getting a pointer, no need to use aucmd_prepbuf()
5155 curbuf = (buf_T *)from;
5156 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005157 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005158 curbuf = save_curbuf;
5159 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005160 }
5161 }
5162 else if (opt_type == SREQ_WIN)
5163 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005164 win_T *save_curwin = curwin;
5165
5166 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005167 curbuf = curwin->w_buffer;
5168 varp = get_varp(p);
5169 curwin = save_curwin;
5170 curbuf = curwin->w_buffer;
5171 }
5172 if (varp == p->var)
5173 return (r | SOPT_UNSET);
5174 }
5175
5176 if (varp != NULL)
5177 {
5178 if (p->flags & P_STRING)
5179 *stringval = vim_strsave(*(char_u **)(varp));
5180 else if (p->flags & P_NUM)
5181 *numval = *(long *) varp;
5182 else
5183 *numval = *(int *)varp;
5184 }
5185
5186 return r;
5187}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005188
5189/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005190 * Iterate over options. First argument is a pointer to a pointer to a
5191 * structure inside options[] array, second is option type like in the above
5192 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005193 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005194 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005195 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005196 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005197 * first argument to NULL.
5198 *
5199 * Returns full option name for current option on each call.
5200 */
5201 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005202option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005203{
5204 struct vimoption *ret = NULL;
5205 do
5206 {
5207 if (*option == NULL)
5208 *option = (void *) options;
5209 else if (((struct vimoption *) (*option))->fullname == NULL)
5210 {
5211 *option = NULL;
5212 return NULL;
5213 }
5214 else
5215 *option = (void *) (((struct vimoption *) (*option)) + 1);
5216
5217 ret = ((struct vimoption *) (*option));
5218
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005219 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005220 if (ret->var == NULL)
5221 {
5222 ret = NULL;
5223 continue;
5224 }
5225
5226 switch (opt_type)
5227 {
5228 case SREQ_GLOBAL:
5229 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5230 ret = NULL;
5231 break;
5232 case SREQ_BUF:
5233 if (!(ret->indir & PV_BUF))
5234 ret = NULL;
5235 break;
5236 case SREQ_WIN:
5237 if (!(ret->indir & PV_WIN))
5238 ret = NULL;
5239 break;
5240 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005241 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005242 return NULL;
5243 }
5244 }
5245 while (ret == NULL);
5246
5247 return (char_u *)ret->fullname;
5248}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005249#endif
5250
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005252 * Return the flags for the option at 'opt_idx'.
5253 */
5254 long_u
5255get_option_flags(int opt_idx)
5256{
5257 return options[opt_idx].flags;
5258}
5259
5260/*
5261 * Set a flag for the option at 'opt_idx'.
5262 */
5263 void
5264set_option_flag(int opt_idx, long_u flag)
5265{
5266 options[opt_idx].flags |= flag;
5267}
5268
5269/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005270 * Returns TRUE if the option at 'opt_idx' is a global option
5271 */
5272 int
5273is_global_option(int opt_idx)
5274{
5275 return options[opt_idx].indir == PV_NONE;
5276}
5277
5278/*
5279 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5280 * local value.
5281 */
5282 int
5283is_global_local_option(int opt_idx)
5284{
5285 return options[opt_idx].indir & PV_BOTH;
5286}
5287
5288/*
5289 * Returns TRUE if the option at 'opt_idx' is a window-local option
5290 */
5291 int
5292is_window_local_option(int opt_idx)
5293{
5294 return options[opt_idx].var == VAR_WIN;
5295}
5296
5297/*
5298 * Returns TRUE if the option at 'opt_idx' is a hidden option
5299 */
5300 int
5301is_hidden_option(int opt_idx)
5302{
5303 return options[opt_idx].var == NULL;
5304}
5305
5306#if defined(FEAT_CRYPT) || defined(PROTO)
5307/*
5308 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5309 */
5310 int
5311is_crypt_key_option(int opt_idx)
5312{
5313 return options[opt_idx].indir == PV_KEY;
5314}
5315#endif
5316
5317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 * Set the value of option "name".
5319 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005320 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005321 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005323 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005324set_option_value(
5325 char_u *name,
5326 long number,
5327 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005328 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
5330 int opt_idx;
5331 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005332 long_u flags;
Christian Brabandtb39b2402023-11-29 11:34:05 +01005333 static char errbuf[ERR_BUFLEN];
5334 int errbuflen = ERR_BUFLEN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335
5336 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005337 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005338 {
5339 int key;
5340
5341 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005342 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005343 {
5344 char_u key_name[2];
5345
5346 if (key < 0)
5347 {
5348 key_name[0] = KEY2TERMCAP0(key);
5349 key_name[1] = KEY2TERMCAP1(key);
5350 }
5351 else
5352 {
5353 key_name[0] = KS_KEY;
5354 key_name[1] = (key & 0xff);
5355 }
5356 add_termcode(key_name, string, FALSE);
5357 if (full_screen)
5358 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005359 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005360 return NULL;
5361 }
5362
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005363 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 else
5366 {
5367 flags = options[opt_idx].flags;
5368#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005369 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005371 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005372 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005373 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005376 if (flags & P_STRING)
Christian Brabandtb39b2402023-11-29 11:34:05 +01005377 return set_string_option(opt_idx, string, opt_flags, errbuf, errbuflen);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005378
5379 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5380 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005382 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005384 int idx;
5385
5386 // Either we are given a string or we are setting option
5387 // to zero.
5388 for (idx = 0; string[idx] == '0'; ++idx)
5389 ;
5390 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005391 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005392 // There's another character after zeros or the string
5393 // is empty. In both cases, we are trying to set a
5394 // num option using a string.
5395 semsg(_(e_number_required_after_str_equal_str),
5396 name, string);
5397 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005398
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005401 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005402 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005403 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005404 errbuf, sizeof(errbuf), opt_flags);
5405 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005406 else
5407 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005411 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412}
5413
5414/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005415 * Call set_option_value() and when an error is returned report it.
5416 */
5417 void
5418set_option_value_give_err(
5419 char_u *name,
5420 long number,
5421 char_u *string,
5422 int opt_flags) // OPT_LOCAL or 0 (both)
5423{
5424 char *errmsg = set_option_value(name, number, string, opt_flags);
5425
5426 if (errmsg != NULL)
5427 emsg(_(errmsg));
5428}
5429
5430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 * Get the terminal code for a terminal option.
5432 * Returns NULL when not found.
5433 */
5434 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005435get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
5437 int opt_idx;
5438 char_u *varp;
5439
5440 if (tname[0] != 't' || tname[1] != '_' ||
5441 tname[2] == NUL || tname[3] == NUL)
5442 return NULL;
5443 if ((opt_idx = findoption(tname)) >= 0)
5444 {
5445 varp = get_varp(&(options[opt_idx]));
5446 if (varp != NULL)
5447 varp = *(char_u **)(varp);
5448 return varp;
5449 }
5450 return find_termcode(tname + 2);
5451}
5452
5453 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005454get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 int i;
5457
5458 i = findoption((char_u *)"hl");
5459 if (i >= 0)
5460 return options[i].def_val[VI_DEFAULT];
5461 return (char_u *)NULL;
5462}
5463
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005464 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005465get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005466{
5467 int i;
5468
5469 i = findoption((char_u *)"enc");
5470 if (i >= 0)
5471 return options[i].def_val[VI_DEFAULT];
5472 return (char_u *)NULL;
5473}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005474
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005475#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005476 int
5477is_option_allocated(char *name)
5478{
5479 int idx = findoption((char_u *)name);
5480
5481 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5482}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005483#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005484
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485/*
5486 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005487 * When "has_lt" is true there is a '<' before "*arg_arg".
5488 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 */
5490 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005491find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005493 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005495 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005497 // Don't use get_special_key_code() for t_xx, we don't want it to call
5498 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5500 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005501 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005503 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005505 key = find_special_key(&arg, &modifiers,
5506 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005507 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 key = 0;
5509 }
5510 return key;
5511}
5512
5513/*
5514 * if 'all' == 0: show changed options
5515 * if 'all' == 1: show all normal options
5516 * if 'all' == 2: show all terminal options
5517 */
5518 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005519showoptions(
5520 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005521 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
5523 struct vimoption *p;
5524 int col;
5525 int isterm;
5526 char_u *varp;
5527 struct vimoption **items;
5528 int item_count;
5529 int run;
5530 int row, rows;
5531 int cols;
5532 int i;
5533 int len;
5534
5535#define INC 20
5536#define GAP 3
5537
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005538 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 if (items == NULL)
5540 return;
5541
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005542 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005544 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005546 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005548 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005550 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005552 // Do the loop two times:
5553 // 1. display the short items
5554 // 2. display the long items (only strings and numbers)
5555 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 for (run = 1; run <= 2 && !got_int; ++run)
5557 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005558 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 item_count = 0;
5560 for (p = &options[0]; p->fullname != NULL; p++)
5561 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005562 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005563 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005564 continue;
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 varp = NULL;
5567 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005568 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 {
5570 if (p->indir != PV_NONE && !isterm)
5571 varp = get_varp_scope(p, opt_flags);
5572 }
5573 else
5574 varp = get_varp(p);
5575 if (varp != NULL
5576 && ((all == 2 && isterm)
5577 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005578 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005580 if (opt_flags & OPT_ONECOLUMN)
5581 len = Columns;
5582 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005583 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 else
5585 {
5586 option_value2string(p, opt_flags);
5587 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5588 }
5589 if ((len <= INC - GAP && run == 1) ||
5590 (len > INC - GAP && run == 2))
5591 items[item_count++] = p;
5592 }
5593 }
5594
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005595 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 if (run == 1)
5597 {
5598 cols = (Columns + GAP - 3) / INC;
5599 if (cols == 0)
5600 cols = 1;
5601 rows = (item_count + cols - 1) / cols;
5602 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005603 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 rows = item_count;
5605 for (row = 0; row < rows && !got_int; ++row)
5606 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005607 msg_putchar('\n'); // go to next line
5608 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 break;
5610 col = 0;
5611 for (i = row; i < item_count; i += rows)
5612 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005613 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 showoneopt(items[i], opt_flags);
5615 col += INC;
5616 }
5617 out_flush();
5618 ui_breakcheck();
5619 }
5620 }
5621 vim_free(items);
5622}
5623
5624/*
5625 * Return TRUE if option "p" has its default value.
5626 */
5627 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005628optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629{
5630 int dvi;
5631
5632 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005633 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005634 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005636 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005638 // the cast to long is required for Manx C, long_i is
5639 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005640 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005641 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5643}
5644
5645/*
5646 * showoneopt: show the value of one option
5647 * must not be called with a hidden option!
5648 */
5649 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005650showoneopt(
5651 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005652 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005654 char_u *varp;
5655 int save_silent = silent_mode;
5656
5657 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005658 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 varp = get_varp_scope(p, opt_flags);
5661
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005662 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5664 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005665 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005667 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005669 msg_puts(" ");
5670 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 if (!(p->flags & P_BOOL))
5672 {
5673 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005674 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 option_value2string(p, opt_flags);
5676 msg_outtrans(NameBuff);
5677 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005678
5679 silent_mode = save_silent;
5680 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681}
5682
5683/*
5684 * Write modified options as ":set" commands to a file.
5685 *
5686 * There are three values for "opt_flags":
5687 * OPT_GLOBAL: Write global option values and fresh values of
5688 * buffer-local options (used for start of a session
5689 * file).
5690 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5691 * curwin (used for a vimrc file).
5692 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5693 * and local values for window-local options of
5694 * curwin. Local values are also written when at the
5695 * default value, because a modeline or autocommand
5696 * may have set them when doing ":edit file" and the
5697 * user has set them back at the default or fresh
5698 * value.
5699 * When "local_only" is TRUE, don't write fresh
5700 * values, only local values (for ":mkview").
5701 * (fresh value = value used for a new buffer or window for a local option).
5702 *
5703 * Return FAIL on error, OK otherwise.
5704 */
5705 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005706makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005709 char_u *varp; // currently used value
5710 char_u *varp_fresh; // local value
5711 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 char *cmd;
5713 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005714 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
5716 /*
5717 * The options that don't have a default (terminal name, columns, lines)
5718 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005719 * Do the loop over "options[]" twice: once for options with the
5720 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005722 for (pri = 1; pri >= 0; --pri)
5723 {
5724 for (p = &options[0]; !istermoption(p); p++)
5725 if (!(p->flags & P_NO_MKRC)
5726 && !istermoption(p)
5727 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005729 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5731 continue;
5732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005733 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5734 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5736 continue;
5737
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005738 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005740 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 continue;
5742
Bram Moolenaard23b7142021-04-17 21:04:34 +02005743 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5744 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005745 continue;
5746
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 round = 2;
5748 if (p->indir != PV_NONE)
5749 {
5750 if (p->var == VAR_WIN)
5751 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005752 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 if (!(opt_flags & OPT_LOCAL))
5754 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005755 // When fresh value of window-local option is not at the
5756 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5758 {
5759 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005760 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 {
5762 round = 1;
5763 varp_local = varp;
5764 varp = varp_fresh;
5765 }
5766 }
5767 }
5768 }
5769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005770 // Round 1: fresh value for window-local options.
5771 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 for ( ; round <= 2; varp = varp_local, ++round)
5773 {
5774 if (round == 1 || (opt_flags & OPT_GLOBAL))
5775 cmd = "set";
5776 else
5777 cmd = "setlocal";
5778
5779 if (p->flags & P_BOOL)
5780 {
5781 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5782 return FAIL;
5783 }
5784 else if (p->flags & P_NUM)
5785 {
5786 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5787 return FAIL;
5788 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005789 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005791 int do_endif = FALSE;
5792
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005793 // Don't set 'syntax' and 'filetype' again if the value is
5794 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005795 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005796#if defined(FEAT_SYN_HL)
5797 p->indir == PV_SYN ||
5798#endif
5799 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 {
5801 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5802 *(char_u **)(varp)) < 0
5803 || put_eol(fd) < 0)
5804 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005805 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
5807 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005808 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005810 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 {
5812 if (put_line(fd, "endif") == FAIL)
5813 return FAIL;
5814 }
5815 }
5816 }
5817 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 return OK;
5820}
5821
5822#if defined(FEAT_FOLDING) || defined(PROTO)
5823/*
5824 * Generate set commands for the local fold options only. Used when
5825 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5826 */
5827 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005828makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005830 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005832 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 == FAIL
5834# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005835 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005837 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 == FAIL
5839 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5840 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5841 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5842 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5843 )
5844 return FAIL;
5845
5846 return OK;
5847}
5848#endif
5849
5850 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005851put_setstring(
5852 FILE *fd,
5853 char *cmd,
5854 char *name,
5855 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005856 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
5858 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005859 char_u *buf = NULL;
5860 char_u *part = NULL;
5861 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
5863 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5864 return FAIL;
5865 if (*valuep != NULL)
5866 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005867 // Output 'pastetoggle' as key names. For other
5868 // options some characters have to be escaped with
5869 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 if (valuep == &p_pt)
5871 {
5872 s = *valuep;
5873 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005874 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 return FAIL;
5876 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005877 // expand the option value, replace $HOME by ~
5878 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005880 int size = (int)STRLEN(*valuep) + 1;
5881
5882 // replace home directory in the whole option value into "buf"
5883 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005884 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005885 goto fail;
5886 home_replace(NULL, *valuep, buf, size, FALSE);
5887
5888 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005889 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005890 // can be expanded when read back.
5891 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5892 && vim_strchr(*valuep, ',') != NULL)
5893 {
5894 part = alloc(size);
5895 if (part == NULL)
5896 goto fail;
5897
5898 // write line break to clear the option, e.g. ':set rtp='
5899 if (put_eol(fd) == FAIL)
5900 goto fail;
5901
5902 p = buf;
5903 while (*p != NUL)
5904 {
5905 // for each comma separated option part, append value to
5906 // the option, :set rtp+=value
5907 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5908 goto fail;
5909 (void)copy_option_part(&p, part, size, ",");
5910 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5911 goto fail;
5912 }
5913 vim_free(buf);
5914 vim_free(part);
5915 return OK;
5916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005918 {
5919 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005921 }
5922 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 }
5924 else if (put_escstr(fd, *valuep, 2) == FAIL)
5925 return FAIL;
5926 }
5927 if (put_eol(fd) < 0)
5928 return FAIL;
5929 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005930fail:
5931 vim_free(buf);
5932 vim_free(part);
5933 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934}
5935
5936 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005937put_setnum(
5938 FILE *fd,
5939 char *cmd,
5940 char *name,
5941 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942{
5943 long wc;
5944
5945 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5946 return FAIL;
5947 if (wc_use_keyname((char_u *)valuep, &wc))
5948 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005949 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5951 return FAIL;
5952 }
5953 else if (fprintf(fd, "%ld", *valuep) < 0)
5954 return FAIL;
5955 if (put_eol(fd) < 0)
5956 return FAIL;
5957 return OK;
5958}
5959
5960 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005961put_setbool(
5962 FILE *fd,
5963 char *cmd,
5964 char *name,
5965 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005967 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005968 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5970 || put_eol(fd) < 0)
5971 return FAIL;
5972 return OK;
5973}
5974
5975/*
5976 * Clear all the terminal options.
5977 * If the option has been allocated, free the memory.
5978 * Terminal options are never hidden or indirect.
5979 */
5980 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005981clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005983 // Reset a few things before clearing the old options. This may cause
5984 // outputting a few things that the terminal doesn't understand, but the
5985 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005986 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005987 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005989 // When starting the GUI close the display opened for the clipboard.
5990 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 if (gui.starting)
5992 clear_xterm_clip();
5993#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005994 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005996 free_termoptions();
5997}
5998
5999 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006000free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006001{
6002 struct vimoption *p;
6003
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006004 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 if (istermoption(p))
6006 {
6007 if (p->flags & P_ALLOCED)
6008 free_string_option(*(char_u **)(p->var));
6009 if (p->flags & P_DEF_ALLOCED)
6010 free_string_option(p->def_val[VI_DEFAULT]);
6011 *(char_u **)(p->var) = empty_option;
6012 p->def_val[VI_DEFAULT] = empty_option;
6013 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006014#ifdef FEAT_EVAL
6015 // remember where the option was cleared
6016 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 }
6019 clear_termcodes();
6020}
6021
6022/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006023 * Free the string for one term option, if it was allocated.
6024 * Set the string to empty_option and clear allocated flag.
6025 * "var" points to the option value.
6026 */
6027 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006028free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006029{
6030 struct vimoption *p;
6031
6032 for (p = &options[0]; p->fullname != NULL; p++)
6033 if (p->var == var)
6034 {
6035 if (p->flags & P_ALLOCED)
6036 free_string_option(*(char_u **)(p->var));
6037 *(char_u **)(p->var) = empty_option;
6038 p->flags &= ~P_ALLOCED;
6039 break;
6040 }
6041}
6042
6043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 * Set the terminal option defaults to the current value.
6045 * Used after setting the terminal name.
6046 */
6047 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006048set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049{
6050 struct vimoption *p;
6051
6052 for (p = &options[0]; p->fullname != NULL; p++)
6053 {
6054 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6055 {
6056 if (p->flags & P_DEF_ALLOCED)
6057 {
6058 free_string_option(p->def_val[VI_DEFAULT]);
6059 p->flags &= ~P_DEF_ALLOCED;
6060 }
6061 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6062 if (p->flags & P_ALLOCED)
6063 {
6064 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006065 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 }
6067 }
6068 }
6069}
6070
6071/*
6072 * return TRUE if 'p' starts with 't_'
6073 */
6074 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006075istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
6077 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6078}
6079
Bram Moolenaardac13472019-09-16 21:06:21 +02006080/*
6081 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6082 */
6083 int
6084istermoption_idx(int opt_idx)
6085{
6086 return istermoption(&options[opt_idx]);
6087}
6088
Bram Moolenaar113e1072019-01-20 15:30:40 +01006089#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006091 * Unset local option value, similar to ":set opt<".
6092 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006093 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006094unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006095{
6096 struct vimoption *p;
6097 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006098 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006099
6100 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006101 if (opt_idx < 0)
6102 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006103 p = &(options[opt_idx]);
6104
6105 switch ((int)p->indir)
6106 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006107 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006108 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006109 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006110 break;
6111 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006112 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006113 break;
6114 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006115 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006116 break;
6117 case PV_AR:
6118 buf->b_p_ar = -1;
6119 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006120 case PV_BKC:
6121 clear_string_option(&buf->b_p_bkc);
6122 buf->b_bkc_flags = 0;
6123 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006124 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006125 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006126 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006127 case PV_TC:
6128 clear_string_option(&buf->b_p_tc);
6129 buf->b_tc_flags = 0;
6130 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006131 case PV_SISO:
6132 curwin->w_p_siso = -1;
6133 break;
6134 case PV_SO:
6135 curwin->w_p_so = -1;
6136 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006137# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006138 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006139 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006140 break;
6141 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006142 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006143 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006144# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006145 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006146 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006147 break;
6148 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006149 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006150 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006151# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006152 case PV_TSRFU:
6153 clear_string_option(&buf->b_p_tsrfu);
6154 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006155# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006156 case PV_FP:
6157 clear_string_option(&buf->b_p_fp);
6158 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006159# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006160 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006161 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006162 break;
6163 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006164 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006165 break;
6166 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006167 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006168 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006169# endif
6170# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006171 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006172 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006173 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006174# endif
6175# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006176 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006177 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006178 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006179# endif
6180# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006181 case PV_SBR:
6182 clear_string_option(&((win_T *)from)->w_p_sbr);
6183 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006184# endif
6185# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006186 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006187 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006188 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006189# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006190 case PV_UL:
6191 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6192 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006193 case PV_LW:
6194 clear_string_option(&buf->b_p_lw);
6195 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006196 case PV_MENC:
6197 clear_string_option(&buf->b_p_menc);
6198 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006199 case PV_LCS:
6200 clear_string_option(&((win_T *)from)->w_p_lcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006201 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE,
6202 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006203 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006204 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006205 case PV_FCS:
6206 clear_string_option(&((win_T *)from)->w_p_fcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006207 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE,
6208 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006209 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006210 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006211 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006212 clear_string_option(&((win_T *)from)->w_p_ve);
6213 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006214 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006215 }
6216}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006217#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006218
6219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006221 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 */
6223 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006224get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006226 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 {
6228 if (p->var == VAR_WIN)
6229 return (char_u *)GLOBAL_WO(get_varp(p));
6230 return p->var;
6231 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006232 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
6234 switch ((int)p->indir)
6235 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006236 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006238 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6239 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6240 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006242 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6243 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6244 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006245 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006246 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006247 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006248 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6249 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006251 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6252 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006254 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6255 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006256#ifdef FEAT_COMPL_FUNC
6257 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6258#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006259#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6260 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6261#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006262#if defined(FEAT_CRYPT)
6263 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6264#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006265#ifdef FEAT_LINEBREAK
6266 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6267#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006268#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006269 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006270#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006271 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006272 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006273 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006274 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006275 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006276 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006277 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006278
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006280 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 }
6282 return get_varp(p);
6283}
6284
6285/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006286 * Get pointer to option variable at 'opt_idx', depending on local or global
6287 * scope.
6288 */
6289 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006290get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006291{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006292 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006293}
6294
6295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 * Get pointer to option variable.
6297 */
6298 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006299get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006301 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 if (p->var == NULL)
6303 return NULL;
6304
6305 switch ((int)p->indir)
6306 {
6307 case PV_NONE: return p->var;
6308
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006309 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006310 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006312 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006314 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006316 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006318 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006320 case PV_TC: return *curbuf->b_p_tc != NUL
6321 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006322 case PV_BKC: return *curbuf->b_p_bkc != NUL
6323 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006324 case PV_SISO: return curwin->w_p_siso >= 0
6325 ? (char_u *)&(curwin->w_p_siso) : p->var;
6326 case PV_SO: return curwin->w_p_so >= 0
6327 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006329 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006331 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6333#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006334 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006336 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006338#ifdef FEAT_COMPL_FUNC
6339 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6340 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6341#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006342 case PV_FP: return *curbuf->b_p_fp != NUL
6343 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006345 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006347 case PV_GP: return *curbuf->b_p_gp != NUL
6348 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6349 case PV_MP: return *curbuf->b_p_mp != NUL
6350 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006352#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6353 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6354 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6355#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006356#if defined(FEAT_CRYPT)
6357 case PV_CM: return *curbuf->b_p_cm != NUL
6358 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6359#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006360#ifdef FEAT_LINEBREAK
6361 case PV_SBR: return *curwin->w_p_sbr != NUL
6362 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6363#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006364#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006365 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006366 ? (char_u *)&(curwin->w_p_stl) : p->var;
6367#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006368 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6369 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006370 case PV_LW: return *curbuf->b_p_lw != NUL
6371 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006372 case PV_MENC: return *curbuf->b_p_menc != NUL
6373 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374#ifdef FEAT_ARABIC
6375 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6376#endif
6377 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006378 case PV_LCS: return *curwin->w_p_lcs != NUL
6379 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006380 case PV_FCS: return *curwin->w_p_fcs != NUL
6381 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006382 case PV_VE: return *curwin->w_p_ve != NUL
6383 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006384#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006385 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6386#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006387#ifdef FEAT_SYN_HL
6388 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6389 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006390 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006391 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393#ifdef FEAT_DIFF
6394 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6395#endif
6396#ifdef FEAT_FOLDING
6397 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6398 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6399 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6400 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6401 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6402 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6403 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6404# ifdef FEAT_EVAL
6405 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6406 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6407# endif
6408 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6409#endif
6410 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006411 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006412#ifdef FEAT_LINEBREAK
6413 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006416 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006417#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6419#endif
6420#ifdef FEAT_RIGHTLEFT
6421 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6422 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6423#endif
6424 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006425 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6427#ifdef FEAT_LINEBREAK
6428 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006429 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6430 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006432 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006434 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006435#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006436 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6437 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6438#endif
6439#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006440 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6441 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6442 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444
6445 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6446 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6449 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6451 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6453 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6454 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006455 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458#ifdef FEAT_FOLDING
6459 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006462#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006463 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006465#ifdef FEAT_COMPL_FUNC
6466 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006467 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006468#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006469#ifdef FEAT_EVAL
6470 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6471#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006472 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006474 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006480 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6482 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6483 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6484 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6485#ifdef FEAT_FIND_ID
6486# ifdef FEAT_EVAL
6487 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6488# endif
6489#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006490#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6492 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6493#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006494#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006495 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497#ifdef FEAT_CRYPT
6498 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006501 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6503 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6504 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6505 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6506 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006508 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6515#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006516 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006517 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6518#endif
6519#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006520 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6521 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6522 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006523 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524#endif
6525 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6526 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6527 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6528 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006529#ifdef FEAT_PERSISTENT_UNDO
6530 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6533#ifdef FEAT_KEYMAP
6534 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6535#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006536#ifdef FEAT_SIGNS
6537 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6538#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006539#ifdef FEAT_VARTABS
6540 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6541 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6542#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006543 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006545 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 return (char_u *)&(curbuf->b_p_wm);
6547}
6548
6549/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006550 * Return a pointer to the variable for option at 'opt_idx'
6551 */
6552 char_u *
6553get_option_var(int opt_idx)
6554{
6555 return options[opt_idx].var;
6556}
6557
Dominique Pelle748b3082022-01-08 12:41:16 +00006558#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006559/*
6560 * Return the full name of the option at 'opt_idx'
6561 */
6562 char_u *
6563get_option_fullname(int opt_idx)
6564{
6565 return (char_u *)options[opt_idx].fullname;
6566}
Dominique Pelle748b3082022-01-08 12:41:16 +00006567#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006568
6569/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006570 * Return the did_set callback function for the option at 'opt_idx'
6571 */
6572 opt_did_set_cb_T
6573get_option_did_set_cb(int opt_idx)
6574{
6575 return options[opt_idx].opt_did_set_cb;
6576}
6577
6578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 * Get the value of 'equalprg', either the buffer-local one or the global one.
6580 */
6581 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006582get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583{
6584 if (*curbuf->b_p_ep == NUL)
6585 return p_ep;
6586 return curbuf->b_p_ep;
6587}
6588
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589/*
6590 * Copy options from one window to another.
6591 * Used when splitting a window.
6592 */
6593 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006594win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
6596 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6597 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006598 after_copy_winopt(wp_to);
6599}
6600
6601/*
6602 * After copying window options: update variables depending on options.
6603 */
6604 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006605after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006606{
6607#ifdef FEAT_LINEBREAK
6608 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006609#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006610#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006611 fill_culopt_flags(NULL, wp);
6612 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006613#endif
zeertzjq6a8d2e12024-01-17 20:54:49 +01006614 set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
6615 set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006616}
6617
6618 static char_u *
6619copy_option_val(char_u *val)
6620{
6621 if (val == empty_option)
6622 return empty_option; // no need to allocate memory
6623 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625
6626/*
6627 * Copy the options from one winopt_T to another.
6628 * Doesn't free the old option values in "to", use clear_winopt() for that.
6629 * The 'scroll' option is not copied, because it depends on the window height.
6630 * The 'previewwindow' option is reset, there can be only one preview window.
6631 */
6632 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006633copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
6635#ifdef FEAT_ARABIC
6636 to->wo_arab = from->wo_arab;
6637#endif
6638 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006639 to->wo_lcs = copy_option_val(from->wo_lcs);
6640 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006642 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006643 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006644 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006645#ifdef FEAT_LINEBREAK
6646 to->wo_nuw = from->wo_nuw;
6647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648#ifdef FEAT_RIGHTLEFT
6649 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006650 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006652#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006653 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006654#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006655#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006656 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006659#ifdef FEAT_DIFF
6660 to->wo_wrap_save = from->wo_wrap_save;
6661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662#ifdef FEAT_LINEBREAK
6663 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006664 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006665 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006667 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006669 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006670 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006671 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006672 to->wo_crb_save = from->wo_crb_save;
Christian Brabandt4a8eb6e2023-08-13 19:43:42 +02006673 to->wo_siso = from->wo_siso;
6674 to->wo_so = from->wo_so;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006675#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006676 to->wo_spell = from->wo_spell;
6677#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006678#ifdef FEAT_SYN_HL
6679 to->wo_cuc = from->wo_cuc;
6680 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006681 to->wo_culopt = copy_option_val(from->wo_culopt);
6682 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684#ifdef FEAT_DIFF
6685 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006686 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006688#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006689 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006690 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006691#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006692#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006693 to->wo_twk = copy_option_val(from->wo_twk);
6694 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696#ifdef FEAT_FOLDING
6697 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006698 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006700 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006701 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 to->wo_fml = from->wo_fml;
6703 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006704 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006705 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006706 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006707 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 to->wo_fdn = from->wo_fdn;
6709# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006710 to->wo_fde = copy_option_val(from->wo_fde);
6711 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006713 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006715#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006716 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006717#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006718
6719#ifdef FEAT_EVAL
6720 // Copy the script context so that we know where the value was last set.
6721 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6722 sizeof(to->wo_script_ctx));
6723#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006724 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725}
6726
6727/*
6728 * Check string options in a window for a NULL value.
6729 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006730 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006731check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
6733 check_winopt(&win->w_onebuf_opt);
6734 check_winopt(&win->w_allbuf_opt);
6735}
6736
6737/*
6738 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6739 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006740 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006741check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742{
6743#ifdef FEAT_FOLDING
6744 check_string_option(&wop->wo_fdi);
6745 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006746 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747# ifdef FEAT_EVAL
6748 check_string_option(&wop->wo_fde);
6749 check_string_option(&wop->wo_fdt);
6750# endif
6751 check_string_option(&wop->wo_fmr);
6752#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006753#ifdef FEAT_SIGNS
6754 check_string_option(&wop->wo_scl);
6755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756#ifdef FEAT_RIGHTLEFT
6757 check_string_option(&wop->wo_rlc);
6758#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006759#ifdef FEAT_LINEBREAK
6760 check_string_option(&wop->wo_sbr);
6761#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006762#ifdef FEAT_STL_OPT
6763 check_string_option(&wop->wo_stl);
6764#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006765#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006766 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006767 check_string_option(&wop->wo_cc);
6768#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006769#ifdef FEAT_CONCEAL
6770 check_string_option(&wop->wo_cocu);
6771#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006772#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006773 check_string_option(&wop->wo_twk);
6774 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006775#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006776#ifdef FEAT_LINEBREAK
6777 check_string_option(&wop->wo_briopt);
6778#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006779 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006780 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006781 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006782 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783}
6784
6785/*
6786 * Free the allocated memory inside a winopt_T.
6787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006789clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790{
6791#ifdef FEAT_FOLDING
6792 clear_string_option(&wop->wo_fdi);
6793 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006794 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795# ifdef FEAT_EVAL
6796 clear_string_option(&wop->wo_fde);
6797 clear_string_option(&wop->wo_fdt);
6798# endif
6799 clear_string_option(&wop->wo_fmr);
6800#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006801#ifdef FEAT_SIGNS
6802 clear_string_option(&wop->wo_scl);
6803#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006804#ifdef FEAT_LINEBREAK
6805 clear_string_option(&wop->wo_briopt);
6806#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006807 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808#ifdef FEAT_RIGHTLEFT
6809 clear_string_option(&wop->wo_rlc);
6810#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006811#ifdef FEAT_LINEBREAK
6812 clear_string_option(&wop->wo_sbr);
6813#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006814#ifdef FEAT_STL_OPT
6815 clear_string_option(&wop->wo_stl);
6816#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006817#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006818 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006819 clear_string_option(&wop->wo_cc);
6820#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006821#ifdef FEAT_CONCEAL
6822 clear_string_option(&wop->wo_cocu);
6823#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006824#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006825 clear_string_option(&wop->wo_twk);
6826 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006827#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006828 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006829 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006830 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831}
6832
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006833#ifdef FEAT_EVAL
6834// Index into the options table for a buffer-local option enum.
6835static int buf_opt_idx[BV_COUNT];
6836# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6837
6838/*
6839 * Initialize buf_opt_idx[] if not done already.
6840 */
6841 static void
6842init_buf_opt_idx(void)
6843{
6844 static int did_init_buf_opt_idx = FALSE;
6845 int i;
6846
6847 if (did_init_buf_opt_idx)
6848 return;
6849 did_init_buf_opt_idx = TRUE;
6850 for (i = 0; !istermoption_idx(i); i++)
6851 if (options[i].indir & PV_BUF)
6852 buf_opt_idx[options[i].indir & PV_MASK] = i;
6853}
6854#else
6855# define COPY_OPT_SCTX(buf, bv)
6856#endif
6857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858/*
6859 * Copy global option values to local options for one buffer.
6860 * Used when creating a new buffer and sometimes when entering a buffer.
6861 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006862 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6864 * appropriate.
6865 * BCO_NOHELP Don't copy the values to a help buffer.
6866 */
6867 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006868buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006871 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 int dont_do_help;
6873 int did_isk = FALSE;
6874
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006875 // Skip this when the option defaults have not been set yet. Happens when
6876 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 if (p_cpo != NULL)
6878 {
6879 /*
6880 * Always copy when entering and 'cpo' contains 'S'.
6881 * Don't copy when already initialized.
6882 * Don't copy when 'cpo' contains 's' and not entering.
6883 * 'S' BCO_ENTER initialized 's' should_copy
6884 * yes yes X X TRUE
6885 * yes no yes X FALSE
6886 * no X yes X FALSE
6887 * X no no yes FALSE
6888 * X no no no TRUE
6889 * no yes no X TRUE
6890 */
6891 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6892 && (buf->b_p_initialized
6893 || (!(flags & BCO_ENTER)
6894 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6895 should_copy = FALSE;
6896
6897 if (should_copy || (flags & BCO_ALWAYS))
6898 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006899#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006900 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006901 init_buf_opt_idx();
6902#endif
6903 // Don't copy the options specific to a help buffer when
6904 // BCO_NOHELP is given or the options were initialized already
6905 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6907 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006908 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 {
6910 save_p_isk = buf->b_p_isk;
6911 buf->b_p_isk = NULL;
6912 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006913 // Always free the allocated strings. If not already initialized,
6914 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 if (!buf->b_p_initialized)
6916 {
6917 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006918 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006921 switch (*p_ffs)
6922 {
6923 case 'm':
6924 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6925 case 'd':
6926 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6927 case 'u':
6928 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6929 default:
6930 buf->b_p_ff = vim_strsave(p_ff);
6931 }
6932 if (buf->b_p_ff != NULL)
6933 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 buf->b_p_bh = empty_option;
6935 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 }
6937 else
6938 free_buf_options(buf, FALSE);
6939
6940 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006941 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 buf->b_p_ai_nopaste = p_ai_nopaste;
6943 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006944 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006946 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 buf->b_p_tw_nopaste = p_tw_nopaste;
6948 buf->b_p_tw_nobin = p_tw_nobin;
6949 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006950 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 buf->b_p_wm_nopaste = p_wm_nopaste;
6952 buf->b_p_wm_nobin = p_wm_nobin;
6953 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006954 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006955 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006956 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006957 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006958 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006960 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006962 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006964 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 buf->b_p_ml_nobin = p_ml_nobin;
6966 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006967 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006968 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006969 buf->b_p_swf = FALSE;
6970 else
6971 {
6972 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006973 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006976 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006977#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006978 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006979 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006981#ifdef FEAT_COMPL_FUNC
6982 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006983 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006984 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006985 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006986 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006987 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006988#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006989#ifdef FEAT_EVAL
6990 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006991 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006992 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006995 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006997#ifdef FEAT_VARTABS
6998 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006999 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007000 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007001 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007002 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007003 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007004 buf->b_p_vsts_nopaste = p_vsts_nopaste
7005 ? vim_strsave(p_vsts_nopaste) : NULL;
7006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007008 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007010 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011#ifdef FEAT_FOLDING
7012 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007013 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014#endif
7015 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007016 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007017 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007018 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007019 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7020 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007022 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007024 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007026 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007028 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007029
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007031 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007033 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007035 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007036 buf->b_p_cinsd = vim_strsave(p_cinsd);
7037 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007038 buf->b_p_lop = vim_strsave(p_lop);
7039 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007040
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007041 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007044 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007046 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007048 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007050 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007052 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007053 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007054 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007055#endif
7056#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007057 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007058 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007059 (void)compile_cap_prog(&buf->b_s);
7060 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007061 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007062 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007063 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007064 buf->b_s.b_p_spo = vim_strsave(p_spo);
7065 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007067#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007069 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007071 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007073 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007074#if defined(FEAT_EVAL)
7075 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007076 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078#ifdef FEAT_CRYPT
7079 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007080 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007083 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084#ifdef FEAT_KEYMAP
7085 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007086 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 buf->b_kmap_state |= KEYMAP_INIT;
7088#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007089#ifdef FEAT_TERMINAL
7090 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007091 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007092#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007093 // This isn't really an option, but copying the langmap and IME
7094 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007096 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007098 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007100 // options that are normally global but also have a local value
7101 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007103 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007104 buf->b_p_bkc = empty_option;
7105 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106#ifdef FEAT_QUICKFIX
7107 buf->b_p_gp = empty_option;
7108 buf->b_p_mp = empty_option;
7109 buf->b_p_efm = empty_option;
7110#endif
7111 buf->b_p_ep = empty_option;
7112 buf->b_p_kp = empty_option;
7113 buf->b_p_path = empty_option;
7114 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007115 buf->b_p_tc = empty_option;
7116 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117#ifdef FEAT_FIND_ID
7118 buf->b_p_def = empty_option;
7119 buf->b_p_inc = empty_option;
7120# ifdef FEAT_EVAL
7121 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007122 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123# endif
7124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 buf->b_p_dict = empty_option;
7126 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007127#ifdef FEAT_COMPL_FUNC
7128 buf->b_p_tsrfu = empty_option;
7129#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007130 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007131 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007132#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7133 buf->b_p_bexpr = empty_option;
7134#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007135#if defined(FEAT_CRYPT)
7136 buf->b_p_cm = empty_option;
7137#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007138#ifdef FEAT_PERSISTENT_UNDO
7139 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007140 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007141#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007142 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007143 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007145 // Don't copy the options set by ex_help(), use the saved values,
7146 // when going from a help buffer to a non-help buffer.
7147 // Don't touch these at all when BCO_NOHELP is used and going from
7148 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007150 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007152#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007153 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007154 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007155 else
7156 buf->b_p_vts_array = NULL;
7157#endif
7158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 else
7160 {
7161 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007162 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 did_isk = TRUE;
7164 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007165 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007166#ifdef FEAT_VARTABS
7167 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007168 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007169 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007170 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007171 else
7172 buf->b_p_vts_array = NULL;
7173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 if (buf->b_p_bt[0] == 'h')
7176 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007178 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 }
7180 }
7181
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007182 // When the options should be copied (ignoring BCO_ALWAYS), set the
7183 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (should_copy)
7185 buf->b_p_initialized = TRUE;
7186 }
7187
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007188 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 if (did_isk)
7190 (void)buf_init_chartab(buf, FALSE);
7191}
7192
7193/*
7194 * Reset the 'modifiable' option and its default value.
7195 */
7196 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007197reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
7199 int opt_idx;
7200
7201 curbuf->b_p_ma = FALSE;
7202 p_ma = FALSE;
7203 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007204 if (opt_idx >= 0)
7205 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206}
7207
7208/*
7209 * Set the global value for 'iminsert' to the local value.
7210 */
7211 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007212set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213{
7214 p_iminsert = curbuf->b_p_iminsert;
7215}
7216
7217/*
7218 * Set the global value for 'imsearch' to the local value.
7219 */
7220 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007221set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223 p_imsearch = curbuf->b_p_imsearch;
7224}
7225
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226static int expand_option_idx = -1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007227static int expand_option_start_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7229static int expand_option_flags = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007230static int expand_option_append = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231
7232 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007233set_context_in_set_cmd(
7234 expand_T *xp,
7235 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007236 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
7238 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007239 long_u flags = 0; // init for GCC
7240 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 char_u *p;
7242 char_u *s;
7243 int is_term_option = FALSE;
7244 int key;
7245
7246 expand_option_flags = opt_flags;
7247
7248 xp->xp_context = EXPAND_SETTINGS;
7249 if (*arg == NUL)
7250 {
7251 xp->xp_pattern = arg;
7252 return;
7253 }
7254 p = arg + STRLEN(arg) - 1;
7255 if (*p == ' ' && *(p - 1) != '\\')
7256 {
7257 xp->xp_pattern = p + 1;
7258 return;
7259 }
7260 while (p > arg)
7261 {
7262 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007263 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 if (*p == ' ' || *p == ',')
7265 {
7266 while (s > arg && *(s - 1) == '\\')
7267 --s;
7268 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007269 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 if (*p == ' ' && ((p - s) & 1) == 0)
7271 {
7272 ++p;
7273 break;
7274 }
7275 --p;
7276 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007277 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 {
7279 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007280 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 p += 2;
7282 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007283 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {
7285 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007286 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 p += 3;
7288 }
7289 xp->xp_pattern = arg = p;
7290 if (*arg == '<')
7291 {
7292 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007293 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 return;
7295 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007296 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 {
7298 xp->xp_context = EXPAND_NOTHING;
7299 return;
7300 }
7301 nextchar = *++p;
7302 is_term_option = TRUE;
7303 expand_option_name[2] = KEY2TERMCAP0(key);
7304 expand_option_name[3] = KEY2TERMCAP1(key);
7305 }
7306 else
7307 {
7308 if (p[0] == 't' && p[1] == '_')
7309 {
7310 p += 2;
7311 if (*p != NUL)
7312 ++p;
7313 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007314 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 nextchar = *++p;
7316 is_term_option = TRUE;
7317 expand_option_name[2] = p[-2];
7318 expand_option_name[3] = p[-1];
7319 }
7320 else
7321 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007322 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7324 p++;
7325 if (*p == NUL)
7326 return;
7327 nextchar = *p;
7328 *p = NUL;
7329 opt_idx = findoption(arg);
7330 *p = nextchar;
7331 if (opt_idx == -1 || options[opt_idx].var == NULL)
7332 {
7333 xp->xp_context = EXPAND_NOTHING;
7334 return;
7335 }
7336 flags = options[opt_idx].flags;
7337 if (flags & P_BOOL)
7338 {
7339 xp->xp_context = EXPAND_NOTHING;
7340 return;
7341 }
7342 }
7343 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007344 // handle "-=" and "+="
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007345 expand_option_append = FALSE;
7346 int expand_option_subtract = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7348 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007349 if (nextchar == '-')
7350 expand_option_subtract = TRUE;
7351 if (nextchar == '+' || nextchar == '^')
7352 expand_option_append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 ++p;
7354 nextchar = '=';
7355 }
7356 if ((nextchar != '=' && nextchar != ':')
7357 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7358 {
7359 xp->xp_context = EXPAND_UNSUCCESSFUL;
7360 return;
7361 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007362
7363 // Below are for handling expanding a specific option's value after the '='
7364 // or ':'
7365
7366 if (is_term_option)
7367 expand_option_idx = -1;
7368 else
7369 expand_option_idx = opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02007371 if (!is_term_option)
7372 {
7373 if (options[opt_idx].flags & P_NO_CMD_EXPAND)
7374 {
7375 xp->xp_context=EXPAND_UNSUCCESSFUL;
7376 return;
7377 }
7378 }
7379
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 xp->xp_pattern = p + 1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007381 expand_option_start_col = (int)(p + 1 - xp->xp_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007383 // Certain options currently have special case handling to reuse the
7384 // expansion logic with other commands.
Doug Kearns6dfdff32023-08-27 18:48:51 +02007385#ifdef FEAT_SYN_HL
7386 if (options[opt_idx].var == (char_u *)&p_syn)
7387 {
7388 xp->xp_context = EXPAND_OWNSYNTAX;
7389 return;
7390 }
7391#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007392 if (options[opt_idx].var == (char_u *)&p_ft)
7393 {
7394 xp->xp_context = EXPAND_FILETYPE;
7395 return;
7396 }
Doug Kearns81642d92024-01-04 22:37:44 +01007397#ifdef FEAT_KEYMAP
7398 if (options[opt_idx].var == (char_u *)&p_keymap)
7399 {
7400 xp->xp_context = EXPAND_KEYMAP;
7401 return;
7402 }
7403#endif
Doug Kearns6dfdff32023-08-27 18:48:51 +02007404
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007405 // Now pick. If the option has a custom expander, use that. Otherwise, just
7406 // fill with the existing option value.
7407 if (expand_option_subtract)
7408 {
7409 xp->xp_context = EXPAND_SETTING_SUBTRACT;
7410 return;
7411 }
7412 else if (expand_option_idx >= 0 &&
7413 options[expand_option_idx].opt_expand_cb != NULL)
7414 {
7415 xp->xp_context = EXPAND_STRING_SETTING;
7416 }
7417 else if (*xp->xp_pattern == NUL)
7418 {
7419 xp->xp_context = EXPAND_OLD_SETTING;
7420 return;
7421 }
7422 else
7423 xp->xp_context = EXPAND_NOTHING;
7424
7425 if (is_term_option || (flags & P_NUM))
7426 return;
7427
7428 // Only string options below
7429
7430 // Options that have P_EXPAND are considered to all use file/dir expansion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 if (flags & P_EXPAND)
7432 {
7433 p = options[opt_idx].var;
7434 if (p == (char_u *)&p_bdir
7435 || p == (char_u *)&p_dir
7436 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007437 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440#ifdef FEAT_SESSION
7441 || p == (char_u *)&p_vdir
7442#endif
7443 )
7444 {
7445 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007446 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 xp->xp_backslash = XP_BS_THREE;
7448 else
7449 xp->xp_backslash = XP_BS_ONE;
7450 }
7451 else
7452 {
7453 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007454 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 if (p == (char_u *)&p_tags)
7456 xp->xp_backslash = XP_BS_THREE;
7457 else
7458 xp->xp_backslash = XP_BS_ONE;
7459 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02007460 if (flags & P_COMMA)
7461 xp->xp_backslash |= XP_BS_COMMA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 }
7463
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007464 // For an option that is a list of file names, or comma/colon-separated
7465 // values, split it by the delimiter and find the start of the current
7466 // pattern, while accounting for backslash-escaped space/commas/colons.
7467 // Triple-backslashed escaped file names (e.g. 'path') can also be
7468 // delimited by space.
7469 if ((flags & P_EXPAND) || (flags & P_COMMA) || (flags & P_COLON))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007471 for (p = arg + STRLEN(arg) - 1; p >= xp->xp_pattern; --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007473 // count number of backslashes before ' ' or ',' or ':'
7474 if (*p == ' ' || *p == ',' ||
7475 (*p == ':' && (flags & P_COLON)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007477 s = p;
7478 while (s > xp->xp_pattern && *(s - 1) == '\\')
7479 --s;
Yee Cheng Chin54844852023-10-09 18:12:31 +02007480 if ((*p == ' ' && ((xp->xp_backslash & XP_BS_THREE) && (p - s) < 3))
7481#if defined(BACKSLASH_IN_FILENAME)
7482 || (*p == ',' && (flags & P_COMMA) && (p - s) < 1)
7483#else
7484 || (*p == ',' && (flags & P_COMMA) && (p - s) < 2)
7485#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007486 || (*p == ':' && (flags & P_COLON)))
7487 {
7488 xp->xp_pattern = p + 1;
7489 break;
7490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 }
7492 }
7493 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007494
7495 // An option that is a list of single-character flags should always start
7496 // at the end as we don't complete words.
7497 if (flags & P_FLAGLIST)
7498 xp->xp_pattern = arg + STRLEN(arg);
7499
7500 // Some options can either be using file/dir expansions, or custom value
7501 // expansion depending on what the user typed. Unfortunately we have to
7502 // manually handle it here to make sure we have the correct xp_context set.
7503#ifdef FEAT_SPELL
7504 if (options[opt_idx].var == (char_u *)&p_sps)
7505 {
7506 if (STRNCMP(xp->xp_pattern, "file:", 5) == 0)
7507 {
7508 xp->xp_pattern += 5;
7509 return;
7510 }
7511 else if (options[expand_option_idx].opt_expand_cb != NULL)
7512 {
7513 xp->xp_context = EXPAND_STRING_SETTING;
7514 }
7515 }
7516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517}
7518
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007519/*
7520 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7521 *
7522 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7523 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7524 *
7525 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7526 * regular expression 'regmatch', then stores the match in matches[idx] and
7527 * returns TRUE.
7528 *
7529 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7530 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7531 *
7532 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7533 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7534 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02007535 static int
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007536match_str(
7537 char_u *str,
7538 regmatch_T *regmatch,
7539 char_u **matches,
7540 int idx,
7541 int test_only,
7542 int fuzzy,
7543 char_u *fuzzystr,
7544 fuzmatch_str_T *fuzmatch)
7545{
7546 if (!fuzzy)
7547 {
7548 if (vim_regexec(regmatch, str, (colnr_T)0))
7549 {
7550 if (!test_only)
7551 matches[idx] = vim_strsave(str);
7552 return TRUE;
7553 }
7554 }
7555 else
7556 {
7557 int score;
7558
7559 score = fuzzy_match_str(str, fuzzystr);
7560 if (score != 0)
7561 {
7562 if (!test_only)
7563 {
7564 fuzmatch[idx].idx = idx;
7565 fuzmatch[idx].str = vim_strsave(str);
7566 fuzmatch[idx].score = score;
7567 }
7568
7569 return TRUE;
7570 }
7571 }
7572
7573 return FALSE;
7574}
7575
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007577ExpandSettings(
7578 expand_T *xp,
7579 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007580 char_u *fuzzystr,
7581 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007582 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007583 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007585 int num_normal = 0; // Nr of matching non-term-code settings
7586 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 int opt_idx;
7588 int match;
7589 int count = 0;
7590 char_u *str;
7591 int loop;
7592 int is_term_opt;
7593 char_u name_buf[MAX_KEY_NAME_LEN];
7594 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007595 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007596 int fuzzy;
7597 fuzmatch_str_T *fuzmatch = NULL;
7598
Christian Brabandtcb747892022-05-08 21:10:56 +01007599 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007601 // do this loop twice:
7602 // loop == 0: count the number of matching options
7603 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 for (loop = 0; loop <= 1; ++loop)
7605 {
7606 regmatch->rm_ic = ic;
7607 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7608 {
K.Takataeeec2542021-06-02 13:28:16 +02007609 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007610 {
7611 if (match_str((char_u *)names[match], regmatch, *matches,
7612 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 {
7614 if (loop == 0)
7615 num_normal++;
7616 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007617 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 }
7621 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7622 opt_idx++)
7623 {
7624 if (options[opt_idx].var == NULL)
7625 continue;
7626 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007627 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007629 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 if (is_term_opt && num_normal > 0)
7631 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007632
7633 if (match_str(str, regmatch, *matches, count, (loop == 0),
7634 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 {
7636 if (loop == 0)
7637 {
7638 if (is_term_opt)
7639 num_term++;
7640 else
7641 num_normal++;
7642 }
7643 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007644 count++;
7645 }
7646 else if (!fuzzy && options[opt_idx].shortname != NULL
7647 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007648 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007649 {
7650 // Compare against the abbreviated option name (for regular
7651 // expression match). Fuzzy matching (previous if) already
7652 // matches against both the expanded and abbreviated names.
7653 if (loop == 0)
7654 {
7655 if (is_term_opt)
7656 num_term++;
7657 else
7658 num_normal++;
7659 }
7660 else
7661 (*matches)[count++] = vim_strsave(str);
7662 }
7663 else if (is_term_opt)
7664 {
7665 name_buf[0] = '<';
7666 name_buf[1] = 't';
7667 name_buf[2] = '_';
7668 name_buf[3] = str[2];
7669 name_buf[4] = str[3];
7670 name_buf[5] = '>';
7671 name_buf[6] = NUL;
7672
7673 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7674 fuzzy, fuzzystr, fuzmatch))
7675 {
7676 if (loop == 0)
7677 num_term++;
7678 else
7679 count++;
7680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 }
7682 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007683
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007684 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7686 {
7687 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7688 {
Keith Thompson184f71c2024-01-04 21:19:04 +01007689 if (!SAFE_isprint(str[0]) || !SAFE_isprint(str[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 continue;
7691
7692 name_buf[0] = 't';
7693 name_buf[1] = '_';
7694 name_buf[2] = str[0];
7695 name_buf[3] = str[1];
7696 name_buf[4] = NUL;
7697
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007698 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007699 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007700 {
7701 if (loop == 0)
7702 num_term++;
7703 else
7704 count++;
7705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 else
7707 {
7708 name_buf[0] = '<';
7709 name_buf[1] = 't';
7710 name_buf[2] = '_';
7711 name_buf[3] = str[0];
7712 name_buf[4] = str[1];
7713 name_buf[5] = '>';
7714 name_buf[6] = NUL;
7715
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007716 if (match_str(name_buf, regmatch, *matches, count,
7717 (loop == 0), fuzzy, fuzzystr,
7718 fuzmatch))
7719 {
7720 if (loop == 0)
7721 num_term++;
7722 else
7723 count++;
7724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 }
7726 }
7727
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007728 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007729 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7731 {
7732 name_buf[0] = '<';
7733 STRCPY(name_buf + 1, str);
7734 STRCAT(name_buf, ">");
7735
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007736 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7737 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 {
7739 if (loop == 0)
7740 num_term++;
7741 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007742 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 }
7744 }
7745 }
7746 if (loop == 0)
7747 {
7748 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007749 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007751 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 else
7753 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007754 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007756 *matches = ALLOC_MULT(char_u *, *numMatches);
7757 if (*matches == NULL)
7758 {
7759 *matches = (char_u **)"";
7760 return FAIL;
7761 }
7762 }
7763 else
7764 {
7765 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7766 if (fuzmatch == NULL)
7767 {
7768 *matches = (char_u **)"";
7769 return FAIL;
7770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 }
7772 }
7773 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007774
7775 if (fuzzy &&
7776 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7777 return FAIL;
7778
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 return OK;
7780}
7781
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007782// Escape an option value that can be used on the command-line with :set.
7783// Caller needs to free the returned string, unless NULL is returned.
7784 static char_u*
7785escape_option_str_cmdline(char_u *var)
7786{
7787 char_u *buf;
7788
7789 // A backslash is required before some characters. This is the reverse of
7790 // what happens in do_set().
7791 buf = vim_strsave_escaped(var, escape_chars);
7792 if (buf == NULL)
7793 return NULL;
7794
7795#ifdef BACKSLASH_IN_FILENAME
7796 // For MS-Windows et al. we don't double backslashes at the start and
7797 // before a file name character.
7798 // The reverse is found at stropt_copy_value().
7799 for (var = buf; *var != NUL; MB_PTR_ADV(var))
7800 if (var[0] == '\\' && var[1] == '\\'
7801 && expand_option_idx >= 0
7802 && (options[expand_option_idx].flags & P_EXPAND)
7803 && vim_isfilec(var[2])
7804 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
7805 STRMOVE(var, var + 1);
7806#endif
7807 return buf;
7808}
7809
7810/*
7811 * Expansion handler for :set= when we just want to fill in with the existing value.
7812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007814ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007816 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 char_u *buf;
7818
zeertzjqb0d45ec2023-01-25 15:04:22 +00007819 *numMatches = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007820 *matches = ALLOC_MULT(char_u *, 1);
zeertzjqb0d45ec2023-01-25 15:04:22 +00007821 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 return FAIL;
7823
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007824 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 if (expand_option_idx < 0)
7826 {
7827 var = find_termcode(expand_option_name + 2);
7828 if (var == NULL)
7829 expand_option_idx = findoption(expand_option_name);
7830 }
7831
7832 if (expand_option_idx >= 0)
7833 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007834 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 option_value2string(&options[expand_option_idx], expand_option_flags);
7836 var = NameBuff;
7837 }
7838 else if (var == NULL)
7839 var = (char_u *)"";
7840
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007841 buf = escape_option_str_cmdline(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 if (buf == NULL)
7843 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007844 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 return FAIL;
7846 }
7847
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007848 (*matches)[0] = buf;
zeertzjqb0d45ec2023-01-25 15:04:22 +00007849 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 return OK;
7851}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852
7853/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007854 * Expansion handler for :set=/:set+= when the option has a custom expansion handler.
7855 */
7856 int
7857ExpandStringSetting(
7858 expand_T *xp,
7859 regmatch_T *regmatch,
7860 int *numMatches,
7861 char_u ***matches)
7862{
7863 char_u *var = NULL; // init for GCC
7864 char_u *buf;
7865
7866 if (expand_option_idx < 0 ||
7867 options[expand_option_idx].opt_expand_cb == NULL)
7868 {
7869 // Not supposed to reach this. This function is only for options with
7870 // custom expansion callbacks.
7871 return FAIL;
7872 }
7873
7874 optexpand_T args;
7875 args.oe_varp = get_varp_scope(&options[expand_option_idx], expand_option_flags);
7876 args.oe_append = expand_option_append;
7877 args.oe_regmatch = regmatch;
7878 args.oe_xp = xp;
7879 args.oe_set_arg = xp->xp_line + expand_option_start_col;
7880 args.oe_include_orig_val =
7881 !expand_option_append &&
7882 (*args.oe_set_arg == NUL);
7883
7884 // Retrieve the existing value, but escape it as a reverse of setting it.
7885 // We technically only need to do this when oe_append or
7886 // oe_include_orig_val is true.
7887 option_value2string(&options[expand_option_idx], expand_option_flags);
7888 var = NameBuff;
7889 buf = escape_option_str_cmdline(var);
7890 if (buf == NULL)
7891 return FAIL;
7892
7893 args.oe_opt_value = buf;
7894
7895 int num_ret = options[expand_option_idx].opt_expand_cb(&args, numMatches, matches);
7896
7897 vim_free(buf);
7898 return num_ret;
7899}
7900
7901/*
7902 * Expansion handler for :set-=
7903 */
7904 int
7905ExpandSettingSubtract(
7906 expand_T *xp,
7907 regmatch_T *regmatch,
7908 int *numMatches,
7909 char_u ***matches)
7910{
7911 if (expand_option_idx < 0)
7912 // term option
7913 return ExpandOldSetting(numMatches, matches);
7914
7915 char_u *option_val = *(char_u**)get_option_varp_scope(
7916 expand_option_idx, expand_option_flags);
7917
7918 long_u option_flags = options[expand_option_idx].flags;
7919
7920 if (option_flags & P_NUM)
7921 return ExpandOldSetting(numMatches, matches);
7922 else if (option_flags & P_COMMA)
7923 {
7924 // Split the option by comma, then present each option to the user if
7925 // it matches the pattern.
7926 // This condition needs to go first, because 'whichwrap' has both
7927 // P_COMMA and P_FLAGLIST.
7928 garray_T ga;
7929
7930 char_u *item;
7931 char_u *option_copy;
7932 char_u *next_val;
7933 char_u *comma;
7934
7935 if (*option_val == NUL)
7936 return FAIL;
7937
7938 // Make a copy as we need to inject null characters destructively.
7939 option_copy = vim_strsave(option_val);
7940 if (option_copy == NULL)
7941 return FAIL;
7942 next_val = option_copy;
7943
7944 ga_init2(&ga, sizeof(char_u *), 10);
7945
7946 do
7947 {
7948 item = next_val;
7949 comma = vim_strchr(next_val, ',');
7950 while (comma != NULL && comma != next_val && *(comma - 1) == '\\')
7951 {
7952 // "\," is interpreted as a literal comma rather than option
7953 // separator when reading options in copy_option_part(). Skip
7954 // it.
7955 comma = vim_strchr(comma + 1, ',');
7956 }
7957 if (comma != NULL)
7958 {
7959 *comma = NUL; // null-terminate this value, required by later functions
7960 next_val = comma + 1;
7961 }
7962 else
7963 next_val = NULL;
7964
7965 if (*item == NUL)
7966 // empty value, don't add to list
7967 continue;
7968
7969 if (!vim_regexec(regmatch, item, (colnr_T)0))
7970 continue;
7971
7972 char_u *buf = escape_option_str_cmdline(item);
7973 if (buf == NULL)
7974 {
7975 vim_free(option_copy);
7976 ga_clear_strings(&ga);
7977 return FAIL;
7978 }
7979 if (ga_add_string(&ga, buf) != OK)
7980 {
7981 vim_free(buf);
7982 break;
7983 }
7984 } while (next_val != NULL);
7985
7986 vim_free(option_copy);
7987
7988 *matches = ga.ga_data;
7989 *numMatches = ga.ga_len;
7990 return OK;
7991 }
7992 else if (option_flags & P_FLAGLIST)
7993 {
7994 // Only present the flags that are set on the option as the other flags
7995 // are not meaningful to do set-= on.
7996
7997 if (*xp->xp_pattern != NUL)
7998 {
7999 // Don't suggest anything if cmdline is non-empty. Vim's set-=
8000 // behavior requires consecutive strings and it's usually
Viktor Szépedbf749b2023-10-16 09:53:37 +02008001 // unintuitive to users if they try to subtract multiple flags at
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008002 // once.
8003 return FAIL;
8004 }
8005
Yee Cheng Chin6d113472023-10-02 21:38:39 +02008006 size_t num_flags = STRLEN(option_val);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008007 if (num_flags == 0)
8008 return FAIL;
8009
8010 *matches = ALLOC_MULT(char_u *, num_flags + 1);
8011 if (*matches == NULL)
8012 return FAIL;
8013
8014 int count = 0;
8015 char_u *p;
8016
8017 p = vim_strsave(option_val);
8018 if (p == NULL)
8019 {
8020 VIM_CLEAR(*matches);
8021 return FAIL;
8022 }
8023 (*matches)[count++] = p;
8024
8025 if (num_flags > 1)
8026 {
8027 // If more than one flags, split the flags up and expose each
8028 // character as individual choice.
8029 for (char_u *flag = option_val; *flag != NUL; flag++)
8030 {
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02008031 p = vim_strnsave(flag, 1);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008032 if (p == NULL)
8033 break;
8034 (*matches)[count++] = p;
8035 }
8036 }
8037
8038 *numMatches = count;
8039 return OK;
8040 }
8041
8042 return ExpandOldSetting(numMatches, matches);
8043}
8044
8045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 * Get the value for the numeric or string option *opp in a nice format into
8047 * NameBuff[]. Must not be called with a hidden option!
8048 */
8049 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008050option_value2string(
8051 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008052 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
8054 char_u *varp;
8055
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008056 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057
8058 if (opp->flags & P_NUM)
8059 {
8060 long wc = 0;
8061
8062 if (wc_use_keyname(varp, &wc))
8063 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
8064 else if (wc != 0)
8065 STRCPY(NameBuff, transchar((int)wc));
8066 else
8067 sprintf((char *)NameBuff, "%ld", *(long *)varp);
8068 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008069 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {
8071 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008072 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 NameBuff[0] = NUL;
8074#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008075 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008076 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 STRCPY(NameBuff, "*****");
8078#endif
8079 else if (opp->flags & P_EXPAND)
8080 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008081 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 else if ((char_u **)opp->var == &p_pt)
8083 str2specialbuf(p_pt, NameBuff, MAXPATHL);
8084 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008085 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 }
8087}
8088
8089/*
8090 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
8091 * printed as a keyname.
8092 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
8093 */
8094 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008095wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096{
8097 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
8098 {
8099 *wcp = *(long *)varp;
8100 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
8101 return TRUE;
8102 }
8103 return FALSE;
8104}
8105
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 * Return TRUE if "x" is present in 'shortmess' option, or
8108 * 'shortmess' contains 'a' and "x" is present in SHM_A.
8109 */
8110 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008111shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01008113 return p_shm != NULL &&
8114 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 || (vim_strchr(p_shm, 'a') != NULL
8116 && vim_strchr((char_u *)SHM_A, x) != NULL));
8117}
8118
8119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
8121 *
8122 * Reset 'compatible' and set the values for options that didn't get set yet
8123 * to the Vim defaults.
8124 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008125 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 */
8127 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008128vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008130 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00008131 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008132 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133
8134 if (!option_was_set((char_u *)"cp"))
8135 {
8136 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02008137 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
8139 set_option_default(opt_idx, OPT_FREE, FALSE);
8140 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008141 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008143
8144 if (fname != NULL)
8145 {
8146 p = vim_getenv(envname, &dofree);
8147 if (p == NULL)
8148 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008149 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008150 p = FullName_save(fname, FALSE);
8151 if (p != NULL)
8152 {
8153 vim_setenv(envname, p);
8154 vim_free(p);
8155 }
8156 }
8157 else if (dofree)
8158 vim_free(p);
8159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160}
8161
8162/*
8163 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
8164 */
8165 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008166change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008168 int opt_idx;
8169
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 if (p_cp != on)
8171 {
8172 p_cp = on;
8173 compatible_set();
8174 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008175 opt_idx = findoption((char_u *)"cp");
8176 if (opt_idx >= 0)
8177 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178}
8179
8180/*
8181 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008182 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 */
8184 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008185option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186{
8187 int idx;
8188
8189 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008190 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 return FALSE;
8192 if (options[idx].flags & P_WAS_SET)
8193 return TRUE;
8194 return FALSE;
8195}
8196
8197/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008198 * Reset the flag indicating option "name" was set.
8199 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008200 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008201reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008202{
8203 int idx = findoption(name);
8204
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008205 if (idx < 0)
8206 return FAIL;
8207
8208 options[idx].flags &= ~P_WAS_SET;
8209 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008210}
8211
8212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 * compatible_set() - Called when 'compatible' has been set or unset.
8214 *
8215 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8216 * flag) to a Vi compatible value.
8217 * When 'compatible' is unset: Set all options that have a different default
8218 * for Vim (without the P_VI_DEF flag) to that default.
8219 */
8220 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008221compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222{
8223 int opt_idx;
8224
Bram Moolenaardac13472019-09-16 21:06:21 +02008225 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8227 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8228 set_option_default(opt_idx, OPT_FREE, p_cp);
8229 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008230 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231}
8232
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 * Check if backspacing over something is allowed.
8235 */
8236 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008237can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008238 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008240#ifdef FEAT_JOB_CHANNEL
8241 if (what == BS_START && bt_prompt(curbuf))
8242 return FALSE;
8243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 switch (*p_bs)
8245 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008246 case '3': return TRUE;
8247 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 case '1': return (what != BS_START);
8249 case '0': return FALSE;
8250 }
8251 return vim_strchr(p_bs, what) != NULL;
8252}
8253
8254/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008255 * Return the effective 'scrolloff' value for the current window, using the
8256 * global value when appropriate.
8257 */
8258 long
8259get_scrolloff_value(void)
8260{
8261 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8262}
8263
8264/*
8265 * Return the effective 'sidescrolloff' value for the current window, using the
8266 * global value when appropriate.
8267 */
8268 long
8269get_sidescrolloff_value(void)
8270{
8271 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8272}
8273
8274/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008275 * Get the local or global value of 'backupcopy'.
8276 */
8277 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008278get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008279{
8280 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8281}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008282
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008283#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008284/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008285 * Get the local or global value of 'formatlistpat'.
8286 */
8287 char_u *
8288get_flp_value(buf_T *buf)
8289{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008290 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8291 return p_flp;
8292 return buf->b_p_flp;
8293}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008294#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008295
8296/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008297 * Get the local or global value of the 'virtualedit' flags.
8298 */
8299 unsigned int
8300get_ve_flags(void)
8301{
Gary Johnson51ad8502021-08-03 18:33:08 +02008302 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8303 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008304}
8305
Bram Moolenaaree857022019-11-09 23:26:40 +01008306#if defined(FEAT_LINEBREAK) || defined(PROTO)
8307/*
8308 * Get the local or global value of 'showbreak'.
8309 */
8310 char_u *
8311get_showbreak_value(win_T *win)
8312{
8313 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8314 return p_sbr;
8315 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8316 return empty_option;
8317 return win->w_p_sbr;
8318}
8319#endif
8320
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008321#if defined(FEAT_EVAL) || defined(PROTO)
8322/*
8323 * Get window or buffer local options.
8324 */
8325 dict_T *
8326get_winbuf_options(int bufopt)
8327{
8328 dict_T *d;
8329 int opt_idx;
8330
8331 d = dict_alloc();
8332 if (d == NULL)
8333 return NULL;
8334
Bram Moolenaardac13472019-09-16 21:06:21 +02008335 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008336 {
8337 struct vimoption *opt = &options[opt_idx];
8338
8339 if ((bufopt && (opt->indir & PV_BUF))
8340 || (!bufopt && (opt->indir & PV_WIN)))
8341 {
8342 char_u *varp = get_varp(opt);
8343
8344 if (varp != NULL)
8345 {
8346 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008347 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008348 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008349 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008350 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008351 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008352 }
8353 }
8354 }
8355
8356 return d;
8357}
8358#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008359
Bram Moolenaardac13472019-09-16 21:06:21 +02008360#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008361/*
8362 * This is called when 'culopt' is changed
8363 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008364 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008365fill_culopt_flags(char_u *val, win_T *wp)
8366{
8367 char_u *p;
8368 char_u culopt_flags_new = 0;
8369
8370 if (val == NULL)
8371 p = wp->w_p_culopt;
8372 else
8373 p = val;
8374 while (*p != NUL)
8375 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008376 // Note: Keep this in sync with p_culopt_values.
Bram Moolenaar017ba072019-09-14 21:01:23 +02008377 if (STRNCMP(p, "line", 4) == 0)
8378 {
8379 p += 4;
8380 culopt_flags_new |= CULOPT_LINE;
8381 }
8382 else if (STRNCMP(p, "both", 4) == 0)
8383 {
8384 p += 4;
8385 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8386 }
8387 else if (STRNCMP(p, "number", 6) == 0)
8388 {
8389 p += 6;
8390 culopt_flags_new |= CULOPT_NBR;
8391 }
8392 else if (STRNCMP(p, "screenline", 10) == 0)
8393 {
8394 p += 10;
8395 culopt_flags_new |= CULOPT_SCRLINE;
8396 }
8397
8398 if (*p != ',' && *p != NUL)
8399 return FAIL;
8400 if (*p == ',')
8401 ++p;
8402 }
8403
8404 // Can't have both "line" and "screenline".
8405 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8406 return FAIL;
8407 wp->w_p_culopt_flags = culopt_flags_new;
8408
8409 return OK;
8410}
8411#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008412
8413/*
8414 * Get the value of 'magic' adjusted for Vim9 script.
8415 */
8416 int
8417magic_isset(void)
8418{
8419 switch (magic_overruled)
8420 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008421 case OPTION_MAGIC_ON: return TRUE;
8422 case OPTION_MAGIC_OFF: return FALSE;
8423 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008424 }
8425#ifdef FEAT_EVAL
8426 if (in_vim9script())
8427 return TRUE;
8428#endif
8429 return p_magic;
8430}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008431
8432/*
8433 * Set the callback function value for an option that accepts a function name,
8434 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8435 * Returns OK if the option is successfully set to a function, otherwise
8436 * returns FAIL.
8437 */
8438 int
8439option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8440{
8441#ifdef FEAT_EVAL
8442 typval_T *tv;
8443 callback_T cb;
8444
8445 if (optval == NULL || *optval == NUL)
8446 {
8447 free_callback(optcb);
8448 return OK;
8449 }
8450
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008451 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008452 || (STRNCMP(optval, "function(", 9) == 0)
8453 || (STRNCMP(optval, "funcref(", 8) == 0))
8454 // Lambda expression or a funcref
8455 tv = eval_expr(optval, NULL);
8456 else
8457 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008458 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008459 if (tv == NULL)
8460 return FAIL;
8461
8462 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008463 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008464 {
8465 free_tv(tv);
8466 return FAIL;
8467 }
8468
8469 free_callback(optcb);
8470 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008471 if (cb.cb_free_name)
8472 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008473 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008474
8475 // when using Vim9 style "import.funcname" it needs to be expanded to
8476 // "import#funcname".
8477 expand_autload_callback(optcb);
8478
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008479 return OK;
8480#else
8481 return FAIL;
8482#endif
8483}
Christian Brabandt757593c2023-08-22 21:44:10 +02008484
8485#if defined(FEAT_EVAL) || defined(PROTO)
8486 static void
8487didset_options_sctx(int opt_flags, char **buf)
8488{
8489 for (int i = 0; ; ++i)
8490 {
8491 if (buf[i] == NULL)
8492 break;
8493
8494 int idx = findoption((char_u *)buf[i]);
8495 if (idx >= 0)
8496 set_option_sctx_idx(idx, opt_flags, current_sctx);
8497 }
8498}
8499#endif