blob: f0fca6f45c5b2b6e8da7fe9a58728a1af4c9f1f8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000013 * - Put it in the options array in optiondefs.h (copy an existing entry).
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000016 * - Add a PV_XX macro definition to the optiondefs.h file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000019 * - For a window string option, add code to check_win_options() and
20 * clear_winopt().
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 * - For a buffer option, add some code to buf_copy_options().
22 * - For a buffer string option, add code to check_buf_options().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000023 * - If it's a numeric option, add any necessary bounds checks to
24 * set_num_option().
25 * - If it's a list of flags, add some code in did_set_string_option(), search
26 * for WW_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 * - When adding an option with expansion (P_EXPAND), but with a different
28 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020029 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * options.txt, and any other related places.
31 * - Add an entry in runtime/optwin.vim.
32 * When making changes:
33 * - Adjust the help for the option in doc/option.txt.
34 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
35 * comment at the help for the 'compatible' option.
36 */
37
38#define IN_OPTION_C
39#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020040#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010043static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static char_u *option_expand(int opt_idx, char_u *val);
46static void didset_options(void);
47static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000050#else
51# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
52#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010053static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
54static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020055static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020057static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010059static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
61static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020062static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000063static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000066static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void check_winopt(winopt_T *wop);
68static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Christian Brabandt757593c2023-08-22 21:44:10 +020071#if defined(FEAT_EVAL) || defined(PROTO)
72static char *(p_bin_dep_opts[]) = {"textwidth", "wrapmargin", "modeline", "expandtab", NULL};
73static char *(p_paste_dep_opts[]) = {"autoindent", "expandtab", "ruler", "showmatch", "smarttab",
74 "softtabstop", "textwidth", "wrapmargin",
75#ifdef FEAT_RIGHTLEFT
76 "hkmap", "revins",
77#endif
78#ifdef FEAT_VARTABS
79 "varsofttabstop",
80#endif
81 NULL};
82static void didset_options_sctx(int opt_flags, char **buf);
83#endif
84
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086/*
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000087 * Initialize the 'shell' option to a default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000089 static void
90set_init_default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000091{
92 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000094 // Find default value for 'shell' option.
95 // Don't use it if it is empty.
Bram Moolenaar7c626922005-02-07 22:01:03 +000096 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010097#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +000098 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010099 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200102#if defined(MSWIN)
103 {
104 // For MS-Windows put the path in quotes instead of escaping spaces.
105 char_u *cmd;
106 size_t len;
107
108 if (vim_strchr(p, ' ') != NULL)
109 {
110 len = STRLEN(p) + 3; // two quotes and a trailing NUL
111 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200112 if (cmd != NULL)
113 {
114 vim_snprintf((char *)cmd, len, "\"%s\"", p);
115 set_string_default("sh", cmd);
116 vim_free(cmd);
117 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200118 }
119 else
120 set_string_default("sh", p);
121 }
122#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100123 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200124#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000125}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000127/*
128 * Set the default for 'backupskip' to include environment variables for
129 * temp files.
130 */
131 static void
132set_init_default_backupskip(void)
133{
134 int opt_idx;
135 long_u n;
136 char_u *p;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100137#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000138 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100139#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000140 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100141#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000142 int len;
143 garray_T ga;
Bram Moolenaar14338022023-03-15 22:05:44 +0000144 char_u *item;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200145
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000146 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000148 ga_init2(&ga, 1, 100);
149 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
150 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000151 int mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100152#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000153 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100154# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000155 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100156# else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000157 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000159 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100160#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000161 p = vim_getenv((char_u *)names[n], &mustfree);
162 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000164 // First time count the NUL, otherwise count the ','.
165 len = (int)STRLEN(p) + 3;
166 item = alloc(len);
Bram Moolenaar14338022023-03-15 22:05:44 +0000167 if (item != NULL)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000168 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000169 STRCPY(item, p);
170 add_pathsep(item);
171 STRCAT(item, "*");
172 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
173 == NULL
174 && ga_grow(&ga, len) == OK)
175 {
176 if (ga.ga_len > 0)
177 STRCAT(ga.ga_data, ",");
178 STRCAT(ga.ga_data, item);
179 ga.ga_len += len;
180 }
181 vim_free(item);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000184 if (mustfree)
185 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000187 if (ga.ga_data != NULL)
188 {
189 set_string_default("bsk", ga.ga_data);
190 vim_free(ga.ga_data);
191 }
192}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000194/*
195 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
196 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
197 */
198 static void
199set_init_default_maxmemtot(void)
200{
201 int opt_idx;
202 long_u n;
203
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000205 if (opt_idx < 0)
206 return;
207
208#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
209 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
ichizok02560422022-04-05 14:18:44 +0100212#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000213 // Use amount of memory available at this moment.
214 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100215#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000216 // Use amount of memory available to Vim.
217 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100218#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000219 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000221 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
222 opt_idx = findoption((char_u *)"maxmem");
223 if (opt_idx >= 0)
224 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000225#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000226 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
227 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000228#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000229 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000232}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000234/*
235 * Initialize the 'cdpath' option to a default value.
236 */
237 static void
238set_init_default_cdpath(void)
239{
240 int opt_idx;
241 char_u *cdpath;
242 char_u *buf;
243 int i;
244 int j;
245 int mustfree = FALSE;
246
247 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
248 if (cdpath == NULL)
249 return;
250
251 buf = alloc((STRLEN(cdpath) << 1) + 2);
252 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000254 buf[0] = ','; // start with ",", current dir first
255 j = 1;
256 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000258 if (vim_ispathlistsep(cdpath[i]))
259 buf[j++] = ',';
260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000262 if (cdpath[i] == ' ' || cdpath[i] == ',')
263 buf[j++] = '\\';
264 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000267 buf[j] = NUL;
268 opt_idx = findoption((char_u *)"cdpath");
269 if (opt_idx >= 0)
270 {
271 options[opt_idx].def_val[VI_DEFAULT] = buf;
272 options[opt_idx].flags |= P_DEF_ALLOCED;
273 }
274 else
275 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000277 if (mustfree)
278 vim_free(cdpath);
279}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000281/*
282 * Initialize the 'printencoding' option to a default value.
283 */
284 static void
285set_init_default_printencoding(void)
286{
ichizok02560422022-04-05 14:18:44 +0100287#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000288 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100289 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100291# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000292 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100293# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000294 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100295# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000296 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100297# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000298 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000300 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000302}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
304#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000305/*
306 * Initialize the 'printexpr' option to a default value.
307 */
308 static void
309set_init_default_printexpr(void)
310{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100311 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100313# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000314 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100315# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
317
ichizok02560422022-04-05 14:18:44 +0100318# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# endif
321 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000322}
323#endif
324
325#ifdef UNIX
326/*
327 * Force restricted-mode on for "nologin" or "false" $SHELL
328 */
329 static void
330set_init_restricted_mode(void)
331{
332 char_u *p;
333
334 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000335 if (p == NULL)
336 return;
337 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
338 restricted = TRUE;
339 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000340}
341#endif
342
343#ifdef CLEAN_RUNTIMEPATH
344/*
345 * When Vim is started with the "--clean" argument, set the default value
346 * for the 'runtimepath' and 'packpath' options.
347 */
348 static void
349set_init_clean_rtp(void)
350{
351 int opt_idx;
352
353 opt_idx = findoption((char_u *)"runtimepath");
354 if (opt_idx >= 0)
355 {
356 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
357 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
358 }
359 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000360 if (opt_idx < 0)
361 return;
362 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
363 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000364}
365#endif
366
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200367#ifdef UNIX
368/*
369 * Change 'runtimepath' and 'packdir' to '$XDG_CONFIG_HOME/vim' if the only
370 * vimrc found is located in '$XDG_CONFIG_HOME/vim/vimrc'.
371 * In case the '$XDG_CONFIG_HOME' variable is not set, '$HOME/.config' is used
372 * as a fallback as is defined in the XDG base dir specification:
373 * <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html>
374 */
375 static void
376set_init_xdg_rtp(void)
377{
378 int opt_idx;
379 int has_xdg_env = TRUE;
380 int should_free_xdg_dir = FALSE;
381 char_u *vimrc1 = NULL;
382 char_u *vimrc2 = NULL;
383 char_u *xdg_dir = NULL;
384 char_u *xdg_rtp = NULL;
385 char_u *vimrc_xdg = NULL;
386
387 vimrc1 = expand_env_save((char_u *)USR_VIMRC_FILE);
388 vimrc2 = expand_env_save((char_u *)USR_VIMRC_FILE2);
389
390 xdg_dir = mch_getenv("XDG_CONFIG_HOME");
391 if (!xdg_dir)
392 {
393 xdg_dir = expand_env_save((char_u *)"~/.config");
394 should_free_xdg_dir = TRUE;
395 has_xdg_env = FALSE;
396 }
397 vimrc_xdg = concat_fnames(xdg_dir, (char_u *)"vim/vimrc", TRUE);
398
399 if (file_is_readable(vimrc1) || file_is_readable(vimrc2) ||
400 !file_is_readable(vimrc_xdg))
401 goto theend;
402
403 xdg_rtp = has_xdg_env ? (char_u *)XDG_RUNTIMEPATH
404 : (char_u *)XDG_RUNTIMEPATH_FB;
405
406 if ((opt_idx = findoption((char_u *)"runtimepath")) < 0)
407 goto theend;
408
409 options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
410 p_rtp = xdg_rtp;
411
412 if ((opt_idx = findoption((char_u *)"packpath")) < 0)
413 goto theend;
414
415 options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
416 p_pp = xdg_rtp;
417
418theend:
419 vim_free(vimrc1);
420 vim_free(vimrc2);
421 vim_free(vimrc_xdg);
422 if (should_free_xdg_dir)
423 vim_free(xdg_dir);
424}
425#endif
426
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000427/*
428 * Expand environment variables and things like "~" for the defaults.
429 * If option_expand() returns non-NULL the variable is expanded. This can
430 * only happen for non-indirect options.
431 * Also set the default to the expanded value, so ":set" does not list
432 * them.
433 * Don't set the P_ALLOCED flag, because we don't want to free the
434 * default.
435 */
436 static void
437set_init_expand_env(void)
438{
439 int opt_idx;
440 char_u *p;
441
442 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
443 {
444 if ((options[opt_idx].flags & P_GETTEXT)
445 && options[opt_idx].var != NULL)
446 p = (char_u *)_(*(char **)options[opt_idx].var);
447 else
448 p = option_expand(opt_idx, NULL);
449 if (p != NULL && (p = vim_strsave(p)) != NULL)
450 {
451 *(char_u **)options[opt_idx].var = p;
452 // VIMEXP
453 // Defaults for all expanded options are currently the same for Vi
454 // and Vim. When this changes, add some code here! Also need to
455 // split P_DEF_ALLOCED in two.
456 if (options[opt_idx].flags & P_DEF_ALLOCED)
457 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
458 options[opt_idx].def_val[VI_DEFAULT] = p;
459 options[opt_idx].flags |= P_DEF_ALLOCED;
460 }
461 }
462}
463
464/*
465 * Initialize the 'LANG' environment variable to a default value.
466 */
467 static void
468set_init_lang_env(void)
469{
470#if defined(MSWIN) && defined(FEAT_GETTEXT)
471 // If $LANG isn't set, try to get a good value for it. This makes the
472 // right language be used automatically. Don't do this for English.
473 if (mch_getenv((char_u *)"LANG") == NULL)
474 {
475 char buf[20];
476 long_u n;
477
478 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
479 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
480 // only the first two.
481 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
482 (LPTSTR)buf, 20);
483 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
484 {
485 // There are a few exceptions (probably more)
486 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
487 STRCPY(buf, "zh_TW");
488 else if (STRNICMP(buf, "chs", 3) == 0
489 || STRNICMP(buf, "zhc", 3) == 0)
490 STRCPY(buf, "zh_CN");
491 else if (STRNICMP(buf, "jp", 2) == 0)
492 STRCPY(buf, "ja");
493 else
494 buf[2] = NUL; // truncate to two-letter code
495 vim_setenv((char_u *)"LANG", (char_u *)buf);
496 }
497 }
498#elif defined(MACOS_CONVERT)
499 // Moved to os_mac_conv.c to avoid dependency problems.
500 mac_lang_init();
501#endif
502}
503
504/*
505 * Initialize the 'encoding' option to a default value.
506 */
507 static void
508set_init_default_encoding(void)
509{
510 char_u *p;
511 int opt_idx;
512
Igor Todorovski497e5282024-01-12 17:59:18 +0100513# if defined(MSWIN) || defined(__MVS__)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000514 // MS-Windows has builtin support for conversion to and from Unicode, using
515 // "utf-8" for 'encoding' should work best for most users.
Igor Todorovski497e5282024-01-12 17:59:18 +0100516 // 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 +0000517 p = vim_strsave((char_u *)ENC_DFLT);
518# else
519 // enc_locale() will try to find the encoding of the current locale.
520 // This works best for properly configured systems, old and new.
521 p = enc_locale();
522# endif
523 if (p == NULL)
524 return;
525
526 // Try setting 'encoding' and check if the value is valid.
527 // If not, go back to the default encoding.
528 char_u *save_enc = p_enc;
529 p_enc = p;
530 if (STRCMP(p_enc, "gb18030") == 0)
531 {
532 // We don't support "gb18030", but "cp936" is a good substitute
533 // for practical purposes, thus use that. It's not an alias to
534 // still support conversion between gb18030 and utf-8.
535 p_enc = vim_strsave((char_u *)"cp936");
536 vim_free(p);
537 }
538 if (mb_init() == NULL)
539 {
540 opt_idx = findoption((char_u *)"encoding");
541 if (opt_idx >= 0)
542 {
543 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
544 options[opt_idx].flags |= P_DEF_ALLOCED;
545 }
546
547#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
548 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
549 {
550 // Adjust the default for 'isprint' and 'iskeyword' to match
551 // latin1. Also set the defaults for when 'nocompatible' is
552 // set.
553 set_string_option_direct((char_u *)"isp", -1,
554 ISP_LATIN1, OPT_FREE, SID_NONE);
555 set_string_option_direct((char_u *)"isk", -1,
556 ISK_LATIN1, OPT_FREE, SID_NONE);
557 opt_idx = findoption((char_u *)"isp");
558 if (opt_idx >= 0)
559 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
560 opt_idx = findoption((char_u *)"isk");
561 if (opt_idx >= 0)
562 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
563 (void)init_chartab();
564 }
565#endif
566
567#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
568 // Win32 console: When GetACP() returns a different value from
569 // GetConsoleCP() set 'termencoding'.
570 if (
571# ifdef VIMDLL
572 (!gui.in_use && !gui.starting) &&
573# endif
574 GetACP() != GetConsoleCP())
575 {
576 char buf[50];
577
578 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
579 // Use an alternative value.
580 if (GetConsoleCP() == 0)
581 sprintf(buf, "cp%ld", (long)GetACP());
582 else
583 sprintf(buf, "cp%ld", (long)GetConsoleCP());
584 p_tenc = vim_strsave((char_u *)buf);
585 if (p_tenc != NULL)
586 {
587 opt_idx = findoption((char_u *)"termencoding");
588 if (opt_idx >= 0)
589 {
590 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
591 options[opt_idx].flags |= P_DEF_ALLOCED;
592 }
593 convert_setup(&input_conv, p_tenc, p_enc);
594 convert_setup(&output_conv, p_enc, p_tenc);
595 }
596 else
597 p_tenc = empty_option;
598 }
599#endif
600#if defined(MSWIN)
601 // $HOME may have characters in active code page.
602 init_homedir();
603#endif
604 }
605 else
606 {
607 vim_free(p_enc);
608 p_enc = save_enc;
609 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000610}
611
612/*
613 * Initialize the options, first part.
614 *
615 * Called only once from main(), just after creating the first buffer.
616 * If "clean_arg" is TRUE Vim was started with --clean.
617 */
618 void
619set_init_1(int clean_arg)
620{
621#ifdef FEAT_LANGMAP
622 langmap_init();
623#endif
624
625 // Be Vi compatible by default
626 p_cp = TRUE;
627
628 // Use POSIX compatibility when $VIM_POSIX is set.
629 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
630 {
631 set_string_default("cpo", (char_u *)CPO_ALL);
632 set_string_default("shm", (char_u *)SHM_POSIX);
633 }
634
635 set_init_default_shell();
636 set_init_default_backupskip();
637 set_init_default_maxmemtot();
638 set_init_default_cdpath();
639 set_init_default_printencoding();
640#ifdef FEAT_POSTSCRIPT
641 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#endif
643
644 /*
645 * Set all the options (except the terminal options) to their default
646 * value. Also set the global value for local options.
647 */
648 set_options_default(0);
649
matveytadbb1bf2022-02-01 17:26:12 +0000650#ifdef UNIX
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200651 set_init_xdg_rtp();
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000652 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000653#endif
654
Bram Moolenaar07268702018-03-01 21:57:32 +0100655#ifdef CLEAN_RUNTIMEPATH
656 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000657 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100658#endif
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660#ifdef FEAT_GUI
661 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100662 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663#endif
664
665 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100666 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100667 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 check_buf_options(curbuf);
669 check_win_options(curwin);
670 check_options();
671
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100672 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 didset_options();
674
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000675#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100676 // Use the current chartab for the generic chartab. This is not in
677 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000678 init_spell_chartab();
679#endif
680
K.Takatace3189d2023-02-15 19:13:43 +0000681 set_init_default_encoding();
682
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000683 // Expand environment variables and things like "~" for the defaults.
684 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100686 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100689 // Detect use of mlterm.
690 // Mlterm is a terminal emulator akin to xterm that has some special
691 // abilities (bidi namely).
692 // NOTE: mlterm's author is being asked to 'set' a variable
693 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100695 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696#endif
697
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200698 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000700 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
702#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100703 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 set_helplang_default(get_mess_lang());
705#endif
706}
707
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200708static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
709
710/*
711 * Set the "fileencodings" option to the default value for when 'encoding' is
712 * utf-8.
713 */
714 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000715set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200716{
717 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
718 OPT_FREE, 0);
719}
720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721/*
722 * Set an option to its default value.
723 * This does not take care of side effects!
724 */
725 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100726set_option_default(
727 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100728 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
729 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100731 char_u *varp; // pointer to variable for current option
732 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000734 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
736
737 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
738 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100739 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 {
741 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
742 if (flags & P_STRING)
743 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200744 // 'fencs' default value depends on 'encoding'
745 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
746 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100747 // Use set_string_option_direct() for local options to handle
748 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200749 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200750 set_string_option_direct(NULL, opt_idx,
751 options[opt_idx].def_val[dvi], opt_flags, 0);
752 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200754 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
755 free_string_option(*(char_u **)(varp));
756 *(char_u **)varp = options[opt_idx].def_val[dvi];
757 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 }
759 }
760 else if (flags & P_NUM)
761 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000762 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 win_comp_scroll(curwin);
764 else
765 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100766 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
767
768 if ((long *)varp == &curwin->w_p_so
769 || (long *)varp == &curwin->w_p_siso)
770 // 'scrolloff' and 'sidescrolloff' local values have a
771 // different default value than the global default.
772 *(long *)varp = -1;
773 else
774 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100775 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 if (both)
777 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100778 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
780 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100781 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100783 // the cast to long is required for Manx C, long_i is needed for
784 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000785 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000786#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100787 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000788 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
789 *(int *)varp = FALSE;
790#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100791 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 if (both)
793 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
794 *(int *)varp;
795 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000796
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100797 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000798 flagsp = insecure_flag(opt_idx, opt_flags);
799 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
801
802#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200803 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804#endif
805}
806
807/*
808 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200809 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 */
811 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100812set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100813 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
815 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000817 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
Bram Moolenaardac13472019-09-16 21:06:21 +0200819 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200820 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200821 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100822 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200823# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200824 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200825 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200826# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100827 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 set_option_default(i, opt_flags, p_cp);
829
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100830 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000831 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200833 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834}
835
836/*
837 * Set the Vi-default value of a string option.
838 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100839 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100841 static void
842set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
844 char_u *p;
845 int opt_idx;
846
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100847 if (escape && vim_strchr(val, ' ') != NULL)
848 p = vim_strsave_escaped(val, (char_u *)" ");
849 else
850 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000851 if (p == NULL) // we don't want a NULL
852 return;
853
854 opt_idx = findoption((char_u *)name);
855 if (opt_idx < 0)
856 return;
857
858 if (options[opt_idx].flags & P_DEF_ALLOCED)
859 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
860 options[opt_idx].def_val[VI_DEFAULT] = p;
861 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862}
863
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100864 void
865set_string_default(char *name, char_u *val)
866{
867 set_string_default_esc(name, val, FALSE);
868}
869
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200871 * For an option value that contains comma separated items, find "newval" in
872 * "origval". Return NULL if not found.
873 */
874 static char_u *
875find_dup_item(char_u *origval, char_u *newval, long_u flags)
876{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200877 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200878 size_t newlen;
879 char_u *s;
880
881 if (origval == NULL)
882 return NULL;
883
884 newlen = STRLEN(newval);
885 for (s = origval; *s != NUL; ++s)
886 {
887 if ((!(flags & P_COMMA)
888 || s == origval
889 || (s[-1] == ',' && !(bs & 1)))
890 && STRNCMP(s, newval, newlen) == 0
891 && (!(flags & P_COMMA)
892 || s[newlen] == ','
893 || s[newlen] == NUL))
894 return s;
895 // Count backslashes. Only a comma with an even number of backslashes
896 // or a single backslash preceded by a comma before it is recognized as
897 // a separator.
898 if ((s > origval + 1
899 && s[-1] == '\\'
900 && s[-2] != ',')
901 || (s == origval + 1
902 && s[-1] == '\\'))
903 ++bs;
904 else
905 bs = 0;
906 }
907 return NULL;
908}
909
910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 * Set the Vi-default value of a number option.
912 * Used for 'lines' and 'columns'.
913 */
914 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100915set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000917 int opt_idx;
918
919 opt_idx = findoption((char_u *)name);
920 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000921 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922}
923
Dominique Pelle748b3082022-01-08 12:41:16 +0000924#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200925/*
926 * Set all window-local and buffer-local options to the Vim default.
927 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200928 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200929 */
930 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200931set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200932{
933 win_T *save_curwin = curwin;
934 int i;
935
936 curwin = wp;
937 curbuf = curwin->w_buffer;
938 block_autocmds();
939
Bram Moolenaardac13472019-09-16 21:06:21 +0200940 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200941 {
942 struct vimoption *p = &(options[i]);
943 char_u *varp = get_varp_scope(p, OPT_LOCAL);
944
945 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200946 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200947 && !(options[i].flags & P_NODEFAULT)
948 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200949 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200950 }
951
952 unblock_autocmds();
953 curwin = save_curwin;
954 curbuf = curwin->w_buffer;
955}
Dominique Pelle748b3082022-01-08 12:41:16 +0000956#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200957
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000958#if defined(EXITFREE) || defined(PROTO)
959/*
960 * Free all options.
961 */
962 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100963free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000964{
965 int i;
966
Bram Moolenaardac13472019-09-16 21:06:21 +0200967 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000968 {
969 if (options[i].indir == PV_NONE)
970 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100971 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100972 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000973 free_string_option(*(char_u **)options[i].var);
974 if (options[i].flags & P_DEF_ALLOCED)
975 free_string_option(options[i].def_val[VI_DEFAULT]);
976 }
977 else if (options[i].var != VAR_WIN
978 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100979 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200980 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000981 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000982 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000983 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000984}
985#endif
986
987
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988/*
989 * Initialize the options, part two: After getting Rows and Columns and
990 * setting 'term'.
991 */
992 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100993set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000995 int idx;
996
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000997 // 'scroll' defaults to half the window height. The stored default is zero,
998 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000999 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001000 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001001 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 comp_col();
1003
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001004 // 'window' is only for backwards compatibility with Vi.
1005 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +00001006 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001007 p_window = Rows - 1;
1008 set_number_default("window", Rows - 1);
1009
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001010 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +01001011#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001012 // If 'background' wasn't set by the user, try guessing the value,
1013 // depending on the terminal name. Only need to check for terminals
1014 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001015 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001016 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
1017 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001019 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001020 // don't mark it as set, when starting the GUI it may be
1021 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +00001022 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001025
1026#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001027 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +00001028#endif
1029#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001030 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +00001031#endif
1032#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001033 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +00001034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035}
1036
1037/*
1038 * Initialize the options, part three: After reading the .vimrc
1039 */
1040 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001041set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
Bram Moolenaar4f974752019-02-17 17:44:42 +01001043#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044/*
1045 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
1046 * This is done after other initializations, where 'shell' might have been
1047 * set, but only if they have not been set before.
1048 */
1049 char_u *p;
1050 int idx_srr;
1051 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001052# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 int idx_sp;
1054 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
1057 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001058 if (idx_srr < 0)
1059 do_srr = FALSE;
1060 else
1061 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001062# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001064 if (idx_sp < 0)
1065 do_sp = FALSE;
1066 else
1067 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001068# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001069 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (p != NULL)
1071 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001072 // Default for p_sp is "| tee", for p_srr is ">".
1073 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if ( fnamecmp(p, "csh") == 0
1075 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001076# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 || fnamecmp(p, "csh.exe") == 0
1078 || fnamecmp(p, "tcsh.exe") == 0
1079# endif
1080 )
1081 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001082# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (do_sp)
1084 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001085# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001087# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001089# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1091 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (do_srr)
1094 {
1095 p_srr = (char_u *)">&";
1096 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1097 }
1098 }
Mike Williams12795022021-06-28 20:53:58 +02001099# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001100 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1101 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001102 else if ( fnamecmp(p, "powershell") == 0
1103 || fnamecmp(p, "powershell.exe") == 0
1104 )
1105 {
1106# if defined(FEAT_QUICKFIX)
1107 if (do_sp)
1108 {
1109 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1110 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1111 }
1112# endif
1113 if (do_srr)
1114 {
1115 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1116 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1117 }
1118 }
1119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 else
Natanael Copa56318362021-05-06 18:46:35 +02001121 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 if ( fnamecmp(p, "sh") == 0
1123 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001124 || fnamecmp(p, "mksh") == 0
1125 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001127 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001129 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001130 || fnamecmp(p, "ash") == 0
1131 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001132 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001133# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 || fnamecmp(p, "cmd") == 0
1135 || fnamecmp(p, "sh.exe") == 0
1136 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001137 || fnamecmp(p, "mksh.exe") == 0
1138 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001140 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 || fnamecmp(p, "bash.exe") == 0
1142 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001143 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001144 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001146 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001148# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (do_sp)
1150 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001151# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001153# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001154 if (fnamecmp(p, "pwsh") == 0)
1155 p_sp = (char_u *)">%s 2>&1";
1156 else
1157 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1160 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 if (do_srr)
1163 {
1164 p_srr = (char_u *)">%s 2>&1";
1165 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1166 }
1167 }
1168 vim_free(p);
1169 }
1170#endif
1171
Bram Moolenaar4f974752019-02-17 17:44:42 +01001172#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001174 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1175 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001177 * set, but only if they have not been set before.
1178 * Default values depend on shell (cmd.exe is default shell):
1179 *
1180 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001181 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001182 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001183 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001184 * "sh" like shells - "-c" "\""
1185 *
1186 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 */
Mike Williams12795022021-06-28 20:53:58 +02001188 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1189 {
1190 int idx_opt;
1191
1192 idx_opt = findoption((char_u *)"shcf");
1193 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1194 {
1195 p_shcf = (char_u*)"-Command";
1196 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1197 }
1198
1199 idx_opt = findoption((char_u *)"sxq");
1200 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1201 {
1202 p_sxq = (char_u*)"\"";
1203 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1204 }
1205 }
1206 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
1208 int idx3;
1209
1210 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001211 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
1213 p_shcf = (char_u *)"-c";
1214 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1215 }
1216
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001217 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001219 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 {
1221 p_sxq = (char_u *)"\"";
1222 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001225 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1226 {
1227 int idx3;
1228
1229 /*
1230 * cmd.exe on Windows will strip the first and last double quote given
1231 * on the command line, e.g. most of the time things like:
1232 * cmd /c "my path/to/echo" "my args to echo"
1233 * become:
1234 * my path/to/echo" "my args to echo
1235 * when executed.
1236 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001237 * To avoid this, set shellxquote to surround the command in
1238 * parenthesis. This appears to make most commands work, without
1239 * breaking commands that worked previously, such as
1240 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001241 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001242 idx3 = findoption((char_u *)"sxq");
1243 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1244 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001245 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001246 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1247 }
1248
Bram Moolenaara64ba222012-02-12 23:23:31 +01001249 idx3 = findoption((char_u *)"shcf");
1250 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1251 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001252 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001253 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1254 }
1255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256#endif
1257
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001258 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001259 {
1260 int idx_ffs = findoption((char_u *)"ffs");
1261
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001262 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001263 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1264 set_fileformat(default_fileformat(), OPT_LOCAL);
1265 }
1266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268}
1269
1270#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1271/*
1272 * When 'helplang' is still at its default value, set it to "lang".
1273 * Only the first two characters of "lang" are used.
1274 */
1275 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001276set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277{
1278 int idx;
1279
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001280 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 return;
1282 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001283 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1284 return;
1285
1286 if (options[idx].flags & P_ALLOCED)
1287 free_string_option(p_hlg);
1288 p_hlg = vim_strsave(lang);
1289 if (p_hlg == NULL)
1290 p_hlg = empty_option;
1291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001293 // zh_CN becomes "cn", zh_TW becomes "tw"
1294 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001295 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001296 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1297 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001298 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001299 // any C like setting, such as C.UTF-8, becomes "en"
1300 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1301 {
1302 p_hlg[0] = 'e';
1303 p_hlg[1] = 'n';
1304 }
1305 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001307 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308}
1309#endif
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311/*
1312 * 'title' and 'icon' only default to true if they have not been set or reset
1313 * in .vimrc and we can read the old value.
1314 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1315 * they can be reset. This reduces startup time when using X on a remote
1316 * machine.
1317 */
1318 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001319set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320{
1321 int idx1;
1322 long val;
1323
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001324 // If GUI is (going to be) used, we can always set the window title and
1325 // icon name. Saves a bit of time, because the X11 display server does
1326 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001328 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
1330#ifdef FEAT_GUI
1331 if (gui.starting || gui.in_use)
1332 val = TRUE;
1333 else
1334#endif
1335 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001336 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 p_title = val;
1338 }
1339 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001340 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001342 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001344
1345#ifdef FEAT_GUI
1346 if (gui.starting || gui.in_use)
1347 val = TRUE;
1348 else
1349#endif
1350 val = mch_can_restore_icon();
1351 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1352 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001355 void
1356ex_set(exarg_T *eap)
1357{
1358 int flags = 0;
1359
1360 if (eap->cmdidx == CMD_setlocal)
1361 flags = OPT_LOCAL;
1362 else if (eap->cmdidx == CMD_setglobal)
1363 flags = OPT_GLOBAL;
1364#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001365 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001366 ex_options(eap);
1367 else
1368#endif
1369 {
1370 if (eap->forceit)
1371 flags |= OPT_ONECOLUMN;
1372 (void)do_set(eap->arg, flags);
1373 }
1374}
1375
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001376/*
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001377 * :set boolean option prefix
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001378 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001379typedef enum {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001380 PREFIX_NO = 0, // "no" prefix
1381 PREFIX_NONE, // no prefix
1382 PREFIX_INV, // "inv" prefix
1383} set_prefix_T;
1384
1385/*
1386 * Return the prefix type for the option name in *argp.
1387 */
1388 static set_prefix_T
1389get_option_prefix(char_u **argp)
1390{
1391 int prefix = PREFIX_NONE;
1392 char_u *arg = *argp;
1393
1394 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1395 {
1396 prefix = PREFIX_NO;
1397 arg += 2;
1398 }
1399 else if (STRNCMP(arg, "inv", 3) == 0)
1400 {
1401 prefix = PREFIX_INV;
1402 arg += 3;
1403 }
1404
1405 *argp = arg;
1406 return prefix;
1407}
1408
1409/*
1410 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1411 * and the option name length in "*lenp". For a <t_xx> option, return the key
1412 * number in "*keyp".
1413 *
1414 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1415 * otherwise returns OK.
1416 */
1417 static int
1418parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1419{
1420 int key = 0;
1421 int len;
1422 int opt_idx;
1423
1424 if (*arg == '<')
1425 {
1426 opt_idx = -1;
1427 // look out for <t_>;>
1428 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1429 len = 5;
1430 else
1431 {
1432 len = 1;
1433 while (arg[len] != NUL && arg[len] != '>')
1434 ++len;
1435 }
1436 if (arg[len] != '>')
1437 return FAIL;
1438
1439 arg[len] = NUL; // put NUL after name
1440 if (arg[1] == 't' && arg[2] == '_') // could be term code
1441 opt_idx = findoption(arg + 1);
1442 arg[len++] = '>'; // restore '>'
1443 if (opt_idx == -1)
1444 key = find_key_option(arg + 1, TRUE);
1445 }
1446 else
1447 {
1448 int nextchar; // next non-white char after option name
1449
1450 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001451 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001452 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1453 len = 4;
1454 else
1455 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1456 ++len;
1457 nextchar = arg[len];
1458 arg[len] = NUL; // put NUL after name
1459 opt_idx = findoption(arg);
1460 arg[len] = nextchar; // restore nextchar
1461 if (opt_idx == -1)
1462 key = find_key_option(arg, FALSE);
1463 }
1464
1465 *keyp = key;
1466 *lenp = len;
1467 *opt_idxp = opt_idx;
1468
1469 return OK;
1470}
1471
1472/*
1473 * Get the option operator (+=, ^=, -=).
1474 */
1475 static set_op_T
1476get_opt_op(char_u *arg)
1477{
1478 set_op_T op = OP_NONE;
1479
1480 if (*arg != NUL && *(arg + 1) == '=')
1481 {
1482 if (*arg == '+')
1483 op = OP_ADDING; // "+="
1484 else if (*arg == '^')
1485 op = OP_PREPENDING; // "^="
1486 else if (*arg == '-')
1487 op = OP_REMOVING; // "-="
1488 }
1489
1490 return op;
1491}
1492
1493/*
1494 * Validate whether the value of the option in "opt_idx" can be changed.
1495 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1496 * if it can be changed.
1497 */
1498 static int
1499validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1500{
1501 // Skip all options that are not window-local (used when showing
1502 // an already loaded buffer in a window).
1503 if ((opt_flags & OPT_WINONLY)
1504 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1505 return FAIL;
1506
1507 // Skip all options that are window-local (used for :vimgrep).
1508 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1509 && options[opt_idx].var == VAR_WIN)
1510 return FAIL;
1511
1512 // Disallow changing some options from modelines.
1513 if (opt_flags & OPT_MODELINE)
1514 {
1515 if (flags & (P_SECURE | P_NO_ML))
1516 {
1517 *errmsg = e_not_allowed_in_modeline;
1518 return FAIL;
1519 }
1520 if ((flags & P_MLE) && !p_mle)
1521 {
1522 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1523 return FAIL;
1524 }
1525#ifdef FEAT_DIFF
1526 // In diff mode some options are overruled. This avoids that
1527 // 'foldmethod' becomes "marker" instead of "diff" and that
1528 // "wrap" gets set.
1529 if (curwin->w_p_diff
1530 && opt_idx >= 0 // shut up coverity warning
1531 && (
1532# ifdef FEAT_FOLDING
1533 options[opt_idx].indir == PV_FDM ||
1534# endif
1535 options[opt_idx].indir == PV_WRAP))
1536 return FAIL;
1537#endif
1538 }
1539
1540#ifdef HAVE_SANDBOX
1541 // Disallow changing some options in the sandbox
1542 if (sandbox != 0 && (flags & P_SECURE))
1543 {
1544 *errmsg = e_not_allowed_in_sandbox;
1545 return FAIL;
1546 }
1547#endif
1548
1549 return OK;
1550}
1551
Bram Moolenaar47403942022-09-21 21:12:53 +01001552/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001553 * Get the Vim/Vi default value for a string option.
1554 */
1555 static char_u *
1556stropt_get_default_val(
1557 int opt_idx,
1558 char_u *varp,
1559 int flags,
1560 int cp_val)
1561{
1562 char_u *newval;
1563
1564 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1565 ? VI_DEFAULT : VIM_DEFAULT];
1566 if ((char_u **)varp == &p_bg)
1567 {
1568 // guess the value of 'background'
1569#ifdef FEAT_GUI
1570 if (gui.in_use)
1571 newval = gui_bg_default();
1572 else
1573#endif
1574 newval = term_bg_default();
1575 }
1576 else if ((char_u **)varp == &p_fencs && enc_utf8)
1577 newval = fencs_utf8_default;
1578
1579 // expand environment variables and ~ since the default value was
1580 // already expanded, only required when an environment variable was set
1581 // later
1582 if (newval == NULL)
1583 newval = empty_option;
1584 else
1585 {
1586 char_u *s = option_expand(opt_idx, newval);
1587 if (s == NULL)
1588 s = newval;
1589 newval = vim_strsave(s);
1590 }
1591
1592 return newval;
1593}
1594
1595/*
1596 * Convert the 'backspace' option number value to a string: for adding,
1597 * prepending and removing string.
1598 */
1599 static void
1600opt_backspace_nr2str(
1601 char_u *varp,
1602 char_u **origval_p,
1603 char_u **origval_l_p,
1604 char_u **origval_g_p,
1605 char_u **oldval_p)
1606{
1607 int i = getdigits((char_u **)varp);
1608
1609 switch (i)
1610 {
1611 case 0:
1612 *(char_u **)varp = empty_option;
1613 break;
1614 case 1:
1615 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1616 break;
1617 case 2:
1618 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1619 break;
1620 case 3:
1621 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1622 break;
1623 }
1624 vim_free(*oldval_p);
1625 if (*origval_p == *oldval_p)
1626 *origval_p = *(char_u **)varp;
1627 if (*origval_l_p == *oldval_p)
1628 *origval_l_p = *(char_u **)varp;
1629 if (*origval_g_p == *oldval_p)
1630 *origval_g_p = *(char_u **)varp;
1631 *oldval_p = *(char_u **)varp;
1632}
1633
1634/*
1635 * Convert the 'whichwrap' option number value to a string, for backwards
1636 * compatibility with Vim 3.0.
1637 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1638 */
1639 static char_u *
1640opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1641{
1642 *whichwrap = NUL;
1643 int i = getdigits(argp);
1644 if (i & 1)
1645 STRCAT(whichwrap, "b,");
1646 if (i & 2)
1647 STRCAT(whichwrap, "s,");
1648 if (i & 4)
1649 STRCAT(whichwrap, "h,l,");
1650 if (i & 8)
1651 STRCAT(whichwrap, "<,>,");
1652 if (i & 16)
1653 STRCAT(whichwrap, "[,],");
1654 if (*whichwrap != NUL) // remove trailing ,
1655 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1656
1657 return whichwrap;
1658}
1659
1660/*
1661 * Copy the new string value into allocated memory for the option.
1662 * Can't use set_string_option_direct(), because we need to remove the
1663 * backslashes.
1664 */
1665 static char_u *
1666stropt_copy_value(
1667 char_u *origval,
1668 char_u **argp,
1669 set_op_T op,
1670 int flags UNUSED)
1671{
1672 char_u *arg = *argp;
1673 unsigned newlen;
1674 char_u *newval;
1675 char_u *s = NULL;
1676
1677 // get a bit too much
1678 newlen = (unsigned)STRLEN(arg) + 1;
1679 if (op != OP_NONE)
1680 newlen += (unsigned)STRLEN(origval) + 1;
1681 newval = alloc(newlen);
1682 if (newval == NULL) // out of mem, don't change
1683 return NULL;
1684 s = newval;
1685
1686 // Copy the string, skip over escaped chars.
1687 // For MS-DOS and WIN32 backslashes before normal file name characters
1688 // are not removed, and keep backslash at start, for "\\machine\path",
1689 // but do remove it for "\\\\machine\\path".
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001690 // The reverse is found in escape_option_str_cmdline().
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001691 while (*arg != NUL && !VIM_ISWHITE(*arg))
1692 {
1693 int i;
1694
1695 if (*arg == '\\' && arg[1] != NUL
1696#ifdef BACKSLASH_IN_FILENAME
1697 && !((flags & P_EXPAND)
1698 && vim_isfilec(arg[1])
1699 && !VIM_ISWHITE(arg[1])
1700 && (arg[1] != '\\'
1701 || (s == newval && arg[2] != '\\')))
1702#endif
1703 )
1704 ++arg; // remove backslash
1705 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1706 {
1707 // copy multibyte char
1708 mch_memmove(s, arg, (size_t)i);
1709 arg += i;
1710 s += i;
1711 }
1712 else
1713 *s++ = *arg++;
1714 }
1715 *s = NUL;
1716
1717 *argp = arg;
1718 return newval;
1719}
1720
1721/*
1722 * Expand environment variables and ~ in string option value 'newval'.
1723 */
1724 static char_u *
1725stropt_expand_envvar(
1726 int opt_idx,
1727 char_u *origval,
1728 char_u *newval,
1729 set_op_T op)
1730{
1731 char_u *s = option_expand(opt_idx, newval);
1732 if (s == NULL)
1733 return newval;
1734
1735 vim_free(newval);
1736 unsigned newlen = (unsigned)STRLEN(s) + 1;
1737 if (op != OP_NONE)
1738 newlen += (unsigned)STRLEN(origval) + 1;
1739
1740 newval = alloc(newlen);
1741 if (newval == NULL)
1742 return NULL;
1743
1744 STRCPY(newval, s);
1745
1746 return newval;
1747}
1748
1749/*
1750 * Concatenate the original and new values of a string option, adding a "," if
1751 * needed.
1752 */
1753 static void
1754stropt_concat_with_comma(
1755 char_u *origval,
1756 char_u *newval,
1757 set_op_T op,
1758 int flags)
1759{
1760 int len = 0;
1761
1762 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1763 if (op == OP_ADDING)
1764 {
1765 len = (int)STRLEN(origval);
1766 // strip a trailing comma, would get 2
1767 if (comma && len > 1
1768 && (flags & P_ONECOMMA) == P_ONECOMMA
1769 && origval[len - 1] == ','
1770 && origval[len - 2] != '\\')
1771 len--;
1772 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1773 mch_memmove(newval, origval, (size_t)len);
1774 }
1775 else
1776 {
1777 len = (int)STRLEN(newval);
1778 STRMOVE(newval + len + comma, origval);
1779 }
1780 if (comma)
1781 newval[len] = ',';
1782}
1783
1784/*
1785 * Remove a value from a string option. Copy string option value in "origval"
1786 * to "newval" and then remove the string "strval" of length "len".
1787 */
1788 static void
1789stropt_remove_val(
1790 char_u *origval,
1791 char_u *newval,
1792 int flags,
1793 char_u *strval,
1794 int len)
1795{
1796 // Remove newval[] from origval[]. (Note: "len" has been set above
1797 // and is used here).
1798 STRCPY(newval, origval);
1799 if (*strval)
1800 {
1801 // may need to remove a comma
1802 if (flags & P_COMMA)
1803 {
1804 if (strval == origval)
1805 {
1806 // include comma after string
1807 if (strval[len] == ',')
1808 ++len;
1809 }
1810 else
1811 {
1812 // include comma before string
1813 --strval;
1814 ++len;
1815 }
1816 }
1817 STRMOVE(newval + (strval - origval), strval + len);
1818 }
1819}
1820
1821/*
1822 * Remove flags that appear twice in the string option value 'newval'.
1823 */
1824 static void
1825stropt_remove_dupflags(char_u *newval, int flags)
1826{
1827 char_u *s = newval;
1828
1829 // Remove flags that appear twice.
1830 while (*s)
1831 {
1832 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1833 if (flags & P_ONECOMMA)
1834 {
1835 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1836 {
1837 // Remove the duplicated value and the next comma.
1838 STRMOVE(s, s + 2);
1839 continue;
1840 }
1841 }
1842 else
1843 {
1844 if ((!(flags & P_COMMA) || *s != ',')
1845 && vim_strchr(s + 1, *s) != NULL)
1846 {
1847 STRMOVE(s, s + 1);
1848 continue;
1849 }
1850 }
1851 ++s;
1852 }
1853}
1854
1855/*
1856 * Get the string value specified for a ":set" command. The following set
1857 * options are supported:
1858 * set {opt}&
1859 * set {opt}<
1860 * set {opt}={val}
1861 * set {opt}:{val}
1862 */
1863 static char_u *
1864stropt_get_newval(
1865 int nextchar,
1866 int opt_idx,
1867 char_u **argp,
1868 char_u *varp,
1869 char_u **origval_arg,
1870 char_u **origval_l_arg,
1871 char_u **origval_g_arg,
1872 char_u **oldval_arg,
1873 set_op_T *op_arg,
1874 int flags,
1875 int cp_val)
1876{
1877 char_u *arg = *argp;
1878 char_u *origval = *origval_arg;
1879 char_u *origval_l = *origval_l_arg;
1880 char_u *origval_g = *origval_g_arg;
1881 char_u *oldval = *oldval_arg;
1882 set_op_T op = *op_arg;
1883 char_u *save_arg = NULL;
1884 char_u *newval;
1885 char_u *s = NULL;
1886 char_u whichwrap[80];
1887
1888 if (nextchar == '&') // set to default val
1889 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1890 else if (nextchar == '<') // set to global val
1891 newval = vim_strsave(*(char_u **)get_varp_scope(
1892 &(options[opt_idx]), OPT_GLOBAL));
1893 else
1894 {
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001895 ++arg; // jump to after the '=' or ':'
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001896
1897 // Set 'keywordprg' to ":help" if an empty
1898 // value was passed to :set by the user.
1899 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1900 {
1901 save_arg = arg;
1902 arg = (char_u *)":help";
1903 }
1904 // Convert 'backspace' number to string
1905 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1906 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1907 &oldval);
1908 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1909 {
1910 // Convert 'whichwrap' number to string, for backwards
1911 // compatibility with Vim 3.0.
1912 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1913 save_arg = arg;
1914 arg = t;
1915 }
1916 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1917 // version 3.0
1918 else if (*arg == '>' && (varp == (char_u *)&p_dir
1919 || varp == (char_u *)&p_bdir))
1920 ++arg;
1921
1922 // Copy the new string into allocated memory.
1923 newval = stropt_copy_value(origval, &arg, op, flags);
1924 if (newval == NULL)
1925 goto done;
1926
1927 // Expand environment variables and ~.
1928 // Don't do it when adding without inserting a comma.
1929 if (op == OP_NONE || (flags & P_COMMA))
1930 {
1931 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1932 if (newval == NULL)
1933 goto done;
1934 }
1935
1936 // locate newval[] in origval[] when removing it and when adding to
1937 // avoid duplicates
1938 int len = 0;
1939 if (op == OP_REMOVING || (flags & P_NODUP))
1940 {
1941 len = (int)STRLEN(newval);
1942 s = find_dup_item(origval, newval, flags);
1943
1944 // do not add if already there
1945 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1946 {
1947 op = OP_NONE;
1948 STRCPY(newval, origval);
1949 }
1950
1951 // if no duplicate, move pointer to end of original value
1952 if (s == NULL)
1953 s = origval + (int)STRLEN(origval);
1954 }
1955
1956 // concatenate the two strings; add a ',' if needed
1957 if (op == OP_ADDING || op == OP_PREPENDING)
1958 stropt_concat_with_comma(origval, newval, op, flags);
1959 else if (op == OP_REMOVING)
1960 // Remove newval[] from origval[]. (Note: "len" has been set above
1961 // and is used here).
1962 stropt_remove_val(origval, newval, flags, s, len);
1963
1964 if (flags & P_FLAGLIST)
1965 // Remove flags that appear twice.
1966 stropt_remove_dupflags(newval, flags);
1967 }
1968
1969done:
1970 if (save_arg != NULL)
1971 arg = save_arg; // arg was temporarily changed, restore it
1972 *argp = arg;
1973 *origval_arg = origval;
1974 *origval_l_arg = origval_l;
1975 *origval_g_arg = origval_g;
1976 *oldval_arg = oldval;
1977 *op_arg = op;
1978
1979 return newval;
1980}
1981
1982/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001983 * Part of do_set() for string options.
1984 * Returns FAIL on failure, do not process further options.
1985 */
1986 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001987do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001988 int opt_idx,
1989 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001990 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001991 int nextchar,
1992 set_op_T op_arg,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001993 long_u flags,
Bram Moolenaar47403942022-09-21 21:12:53 +01001994 int cp_val,
1995 char_u *varp_arg,
1996 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +01001997 size_t errbuflen,
Bram Moolenaar47403942022-09-21 21:12:53 +01001998 int *value_checked,
1999 char **errmsg)
2000{
Bram Moolenaar6f981142022-09-22 12:48:58 +01002001 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01002002 set_op_T op = op_arg;
2003 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002004 char_u *oldval = NULL; // previous value if *varp
2005 char_u *newval;
2006 char_u *origval = NULL;
2007 char_u *origval_l = NULL;
2008 char_u *origval_g = NULL;
2009#if defined(FEAT_EVAL)
2010 char_u *saved_origval = NULL;
2011 char_u *saved_origval_l = NULL;
2012 char_u *saved_origval_g = NULL;
2013 char_u *saved_newval = NULL;
2014#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01002015
2016 // When using ":set opt=val" for a global option
2017 // with a local value the local value will be
2018 // reset, use the global value here.
2019 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2020 && ((int)options[opt_idx].indir & PV_BOTH))
2021 varp = options[opt_idx].var;
2022
2023 // The old value is kept until we are sure that the new value is valid.
2024 oldval = *(char_u **)varp;
2025
2026 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2027 {
2028 origval_l = *(char_u **)get_varp_scope(
2029 &(options[opt_idx]), OPT_LOCAL);
2030 origval_g = *(char_u **)get_varp_scope(
2031 &(options[opt_idx]), OPT_GLOBAL);
2032
2033 // A global-local string option might have an empty option as value to
2034 // indicate that the global value should be used.
2035 if (((int)options[opt_idx].indir & PV_BOTH)
2036 && origval_l == empty_option)
2037 origval_l = origval_g;
2038 }
2039
2040 // When setting the local value of a global option, the old value may be
2041 // the global value.
2042 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
2043 origval = *(char_u **)get_varp(&options[opt_idx]);
2044 else
2045 origval = oldval;
2046
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002047 // Get the new value for the option
2048 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
2049 &origval_l, &origval_g, &oldval, &op, flags,
2050 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01002051
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002052 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01002053 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00002054 if (newval == NULL)
2055 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01002056
2057#if defined(FEAT_EVAL)
2058 if (!starting
2059# ifdef FEAT_CRYPT
2060 && options[opt_idx].indir != PV_KEY
2061# endif
2062 && origval != NULL && newval != NULL)
2063 {
2064 // origval may be freed by did_set_string_option(), make a copy.
2065 saved_origval = vim_strsave(origval);
2066 // newval (and varp) may become invalid if the buffer is closed by
2067 // autocommands.
2068 saved_newval = vim_strsave(newval);
2069 if (origval_l != NULL)
2070 saved_origval_l = vim_strsave(origval_l);
2071 if (origval_g != NULL)
2072 saved_origval_g = vim_strsave(origval_g);
2073 }
2074#endif
2075
2076 {
2077 long_u *p = insecure_flag(opt_idx, opt_flags);
2078 int secure_saved = secure;
2079
2080 // When an option is set in the sandbox, from a modeline or in secure
2081 // mode, then deal with side effects in secure mode. Also when the
2082 // value was set with the P_INSECURE flag and is not completely
2083 // replaced.
2084 if ((opt_flags & OPT_MODELINE)
2085#ifdef HAVE_SANDBOX
2086 || sandbox != 0
2087#endif
2088 || (op != OP_NONE && (*p & P_INSECURE)))
2089 secure = 1;
2090
2091 // Handle side effects, and set the global value for ":set" on local
2092 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2093 // be triggered that can cause havoc.
2094 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002095 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002096 errbuflen, opt_flags, op, value_checked);
Bram Moolenaar47403942022-09-21 21:12:53 +01002097
2098 secure = secure_saved;
2099 }
2100
2101#if defined(FEAT_EVAL)
2102 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002103 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002104 saved_origval_l, saved_origval_g, saved_newval);
2105 vim_free(saved_origval);
2106 vim_free(saved_origval_l);
2107 vim_free(saved_origval_g);
2108 vim_free(saved_newval);
2109#endif
2110
Bram Moolenaar6f981142022-09-22 12:48:58 +01002111 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002112 return *errmsg == NULL ? OK : FAIL;
2113}
2114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002116 * Set a boolean option.
2117 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002118 */
2119 static char *
2120do_set_option_bool(
2121 int opt_idx,
2122 int opt_flags,
2123 set_prefix_T prefix,
2124 long_u flags,
2125 char_u *varp,
2126 int nextchar,
2127 int afterchar,
2128 int cp_val)
2129
2130{
2131 varnumber_T value;
2132
2133 if (nextchar == '=' || nextchar == ':')
2134 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002135 if (opt_idx < 0 || varp == NULL)
2136 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002137
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002138 // ":set opt!": invert
2139 // ":set opt&": reset to default value
2140 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002141 if (nextchar == '!')
2142 value = *(int *)(varp) ^ 1;
2143 else if (nextchar == '&')
2144 value = (int)(long)(long_i)options[opt_idx].def_val[
2145 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2146 else if (nextchar == '<')
2147 {
2148 // For 'autoread' -1 means to use global value.
2149 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2150 value = -1;
2151 else
2152 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2153 }
2154 else
2155 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002156 // ":set invopt": invert
2157 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002158 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2159 return e_trailing_characters;
2160 if (prefix == PREFIX_INV)
2161 value = *(int *)(varp) ^ 1;
2162 else
2163 value = prefix == PREFIX_NO ? 0 : 1;
2164 }
2165
2166 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2167}
2168
2169/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002170 * Set a numeric option.
2171 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002172 */
2173 static char *
2174do_set_option_numeric(
2175 int opt_idx,
2176 int opt_flags,
2177 char_u **argp,
2178 int nextchar,
2179 set_op_T op,
2180 long_u flags,
2181 int cp_val,
2182 char_u *varp,
2183 char *errbuf,
2184 size_t errbuflen)
2185{
2186 char_u *arg = *argp;
2187 varnumber_T value;
2188 int i;
2189 char *errmsg = NULL;
2190
Bram Moolenaar546933f2023-02-06 16:40:49 +00002191 if (opt_idx < 0 || varp == NULL)
2192 return NULL; // "cannot happen"
2193 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002194 /*
2195 * Different ways to set a number option:
2196 * & set to default value
2197 * < set to global value
2198 * <xx> accept special key codes for 'wildchar'
2199 * c accept any non-digit for 'wildchar'
2200 * [-]0-9 set number
2201 * other error
2202 */
2203 ++arg;
2204 if (nextchar == '&')
2205 value = (long)(long_i)options[opt_idx].def_val[
2206 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2207 else if (nextchar == '<')
2208 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002209 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002210 // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002211 value = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002212 else if (opt_flags == OPT_LOCAL
2213 && ((long *)varp == &curwin->w_p_siso
2214 || (long *)varp == &curwin->w_p_so))
2215 // for 'scrolloff'/'sidescrolloff' -1 means using the global value
2216 value = -1;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002217 else
2218 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2219 }
2220 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2221 && (*arg == '<'
2222 || *arg == '^'
2223 || (*arg != NUL
2224 && (!arg[1] || VIM_ISWHITE(arg[1]))
2225 && !VIM_ISDIGIT(*arg))))
2226 {
2227 value = string_to_key(arg, FALSE);
2228 if (value == 0 && (long *)varp != &p_wcm)
2229 {
2230 errmsg = e_invalid_argument;
2231 goto skip;
2232 }
2233 }
2234 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2235 {
2236 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002237 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002238 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2239 {
2240 errmsg = e_number_required_after_equal;
2241 goto skip;
2242 }
2243 }
2244 else
2245 {
2246 errmsg = e_number_required_after_equal;
2247 goto skip;
2248 }
2249
2250 if (op == OP_ADDING)
2251 value = *(long *)varp + value;
2252 else if (op == OP_PREPENDING)
2253 value = *(long *)varp * value;
2254 else if (op == OP_REMOVING)
2255 value = *(long *)varp - value;
2256
2257 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2258 opt_flags);
2259
2260skip:
2261 *argp = arg;
2262 return errmsg;
2263}
2264
2265/*
2266 * Set a key code (t_xx) option
2267 */
2268 static char *
2269do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2270{
2271 char_u *arg = *argp;
2272 char_u *p;
2273
2274 if (nextchar == '&')
2275 {
2276 if (add_termcap_entry(key_name, TRUE) == FAIL)
2277 return e_not_found_in_termcap;
2278 }
2279 else
2280 {
2281 ++arg; // jump to after the '=' or ':'
2282 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2283 if (*p == '\\' && p[1] != NUL)
2284 ++p;
2285 nextchar = *p;
2286 *p = NUL;
2287 add_termcode(key_name, arg, FALSE);
2288 *p = nextchar;
2289 }
2290 if (full_screen)
2291 ttest(FALSE);
2292 redraw_all_later(UPD_CLEAR);
2293
2294 *argp = arg;
2295 return NULL;
2296}
2297
2298/*
2299 * Set an option to a new value.
2300 */
2301 static char *
2302do_set_option_value(
2303 int opt_idx,
2304 int opt_flags,
2305 char_u **argp,
2306 set_prefix_T prefix,
2307 set_op_T op,
2308 long_u flags,
2309 char_u *varp,
2310 char_u *key_name,
2311 int nextchar,
2312 int afterchar,
2313 int cp_val,
2314 int *stopopteval,
2315 char *errbuf,
2316 size_t errbuflen)
2317{
2318 int value_checked = FALSE;
2319 char *errmsg = NULL;
2320 char_u *arg = *argp;
2321
2322 if (flags & P_BOOL)
2323 {
2324 // boolean option
2325 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2326 nextchar, afterchar, cp_val);
2327 if (errmsg != NULL)
2328 goto skip;
2329 }
2330 else
2331 {
2332 // numeric or string option
2333 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2334 || prefix != PREFIX_NONE)
2335 {
2336 errmsg = e_invalid_argument;
2337 goto skip;
2338 }
2339
2340 if (flags & P_NUM)
2341 {
2342 // numeric option
2343 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2344 op, flags, cp_val, varp,
2345 errbuf, errbuflen);
2346 if (errmsg != NULL)
2347 goto skip;
2348 }
2349 else if (opt_idx >= 0)
2350 {
2351 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002352 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002353 flags, cp_val, varp, errbuf, errbuflen,
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002354 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002355 {
2356 if (errmsg != NULL)
2357 goto skip;
2358 *stopopteval = TRUE;
2359 goto skip;
2360 }
2361 }
2362 else
2363 {
2364 // key code option
2365 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2366 if (errmsg != NULL)
2367 goto skip;
2368 }
2369 }
2370
2371 if (opt_idx >= 0)
2372 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2373
2374skip:
2375 *argp = arg;
2376 return errmsg;
2377}
2378
2379/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002380 * Set an option to a new value.
2381 * Return NULL if OK, return an untranslated error message when something is
2382 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2383 */
2384 static char *
2385do_set_option(
2386 int opt_flags,
2387 char_u **argp,
2388 char_u *arg_start,
2389 char_u **startarg,
2390 int *did_show,
2391 int *stopopteval,
2392 char *errbuf,
2393 size_t errbuflen)
2394{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002395 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002396 char_u *arg;
2397 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2398 set_op_T op;
2399 long_u flags; // flags for current option
2400 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002401 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002402 int nextchar; // next non-white char after option name
2403 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002404 char *errmsg = NULL;
2405 int key;
2406 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002407
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002408 prefix = get_option_prefix(argp);
2409 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002410
2411 // find end of name
2412 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002413 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2414 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002415
2416 // remember character after option name
2417 afterchar = arg[len];
2418
2419 if (in_vim9script())
2420 {
2421 char_u *p = skipwhite(arg + len);
2422
2423 // disallow white space before =val, +=val, -=val, ^=val
2424 if (p > arg + len && (p[0] == '='
2425 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2426 && p[1] == '=')))
2427 {
2428 errmsg = e_no_white_space_allowed_between_option_and;
2429 arg = p;
2430 *startarg = p;
2431 goto skip;
2432 }
2433 }
2434 else
2435 // skip white space, allow ":set ai ?", ":set hlsearch !"
2436 while (VIM_ISWHITE(arg[len]))
2437 ++len;
2438
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002439 op = get_opt_op(arg + len);
2440 if (op != OP_NONE)
2441 len++;
2442
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002443 nextchar = arg[len];
2444
2445 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2446 {
2447 if (in_vim9script() && arg > arg_start
2448 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2449 errmsg = e_no_white_space_allowed_between_option_and;
2450 else
2451 errmsg = e_unknown_option;
2452 goto skip;
2453 }
2454
2455 if (opt_idx >= 0)
2456 {
2457 if (options[opt_idx].var == NULL) // hidden option: skip
2458 {
2459 // Only give an error message when requesting the value of
2460 // a hidden option, ignore setting it.
2461 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2462 && (!(options[opt_idx].flags & P_BOOL)
2463 || nextchar == '?'))
2464 errmsg = e_option_not_supported;
2465 goto skip;
2466 }
2467
2468 flags = options[opt_idx].flags;
2469 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2470 }
2471 else
2472 {
2473 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002474 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002475 if (key < 0)
2476 {
2477 key_name[0] = KEY2TERMCAP0(key);
2478 key_name[1] = KEY2TERMCAP1(key);
2479 }
2480 else
2481 {
2482 key_name[0] = KS_KEY;
2483 key_name[1] = (key & 0xff);
2484 }
2485 }
2486
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002487 // Make sure the option value can be changed.
2488 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002489 goto skip;
2490
Bram Moolenaar40b48722023-02-05 17:04:50 +00002491 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002492 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2493 {
2494 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002495 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2496 {
2497 if (arg[3] == 'm') // "opt&vim": set to Vim default
2498 {
2499 cp_val = FALSE;
2500 arg += 3;
2501 }
2502 else // "opt&vi": set to Vi default
2503 {
2504 cp_val = TRUE;
2505 arg += 2;
2506 }
2507 }
2508 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2509 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2510 {
2511 errmsg = e_trailing_characters;
2512 goto skip;
2513 }
2514 }
2515
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002516 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2517 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002518 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002519 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002520 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2521 && !(flags & P_BOOL)))
2522 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002523 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002524 if (*did_show)
2525 msg_putchar('\n'); // cursor below last one
2526 else
2527 {
2528 gotocmdline(TRUE); // cursor at status line
2529 *did_show = TRUE; // remember that we did a line
2530 }
2531 if (opt_idx >= 0)
2532 {
2533 showoneopt(&options[opt_idx], opt_flags);
2534#ifdef FEAT_EVAL
2535 if (p_verbose > 0)
2536 {
2537 // Mention where the option was last set.
2538 if (varp == options[opt_idx].var)
2539 last_set_msg(options[opt_idx].script_ctx);
2540 else if ((int)options[opt_idx].indir & PV_WIN)
2541 last_set_msg(curwin->w_p_script_ctx[
2542 (int)options[opt_idx].indir & PV_MASK]);
2543 else if ((int)options[opt_idx].indir & PV_BUF)
2544 last_set_msg(curbuf->b_p_script_ctx[
2545 (int)options[opt_idx].indir & PV_MASK]);
2546 }
2547#endif
2548 }
2549 else
2550 {
2551 char_u *p;
2552
2553 p = find_termcode(key_name);
2554 if (p == NULL)
2555 {
2556 errmsg = e_key_code_not_set;
2557 goto skip;
2558 }
2559 else
2560 (void)show_one_termcode(key_name, p, TRUE);
2561 }
2562 if (nextchar != '?'
2563 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2564 errmsg = e_trailing_characters;
2565 }
2566 else
2567 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002568 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2569 flags, varp, key_name, nextchar,
2570 afterchar, cp_val, stopopteval, errbuf,
2571 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002572 }
2573
2574skip:
2575 *argp = arg;
2576 return errmsg;
2577}
2578
2579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 * Parse 'arg' for option settings.
2581 *
2582 * 'arg' may be IObuff, but only when no errors can be present and option
2583 * does not need to be expanded with option_expand().
2584 * "opt_flags":
2585 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002586 * OPT_GLOBAL for ":setglobal"
2587 * OPT_LOCAL for ":setlocal" and a modeline
2588 * OPT_MODELINE for a modeline
2589 * OPT_WINONLY to only set window-local options
2590 * OPT_NOWIN to skip setting window-local options
2591 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002593 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 */
2595 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002596do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002597 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002598 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002600 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002602 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603
2604 if (*arg == NUL)
2605 {
2606 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002607 did_show = TRUE;
2608 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 }
2610
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002611 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002613 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002614 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002616 // ":set all" show all options.
2617 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 arg += 3;
2619 if (*arg == '&')
2620 {
2621 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002622 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002624 didset_options();
2625 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002626 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
2628 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002629 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002631 did_show = TRUE;
2632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002634 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
2636 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002637 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002638 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 arg += 7;
2640 }
2641 else
2642 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002643 int stopopteval = FALSE;
2644 char *errmsg = NULL;
Christian Brabandtb39b2402023-11-29 11:34:05 +01002645 char errbuf[ERR_BUFLEN];
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002646 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002648 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2649 &did_show, &stopopteval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002650 ERR_BUFLEN);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002651 if (stopopteval)
2652 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002654 // Advance to next argument.
2655 // - skip until a blank found, taking care of backslashes
2656 // - skip blanks
2657 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 for (i = 0; i < 2 ; ++i)
2659 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002660 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (*arg++ == '\\' && *arg != NUL)
2662 ++arg;
2663 arg = skipwhite(arg);
2664 if (*arg != '=')
2665 break;
2666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002668 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002670 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2671 i = (int)STRLEN(IObuff) + 2;
2672 if (i + (arg - startarg) < IOSIZE)
2673 {
2674 // append the argument with the error
2675 STRCAT(IObuff, ": ");
2676 mch_memmove(IObuff + i, startarg, (arg - startarg));
2677 IObuff[i + (arg - startarg)] = NUL;
2678 }
2679 // make sure all characters are printable
2680 trans_characters(IObuff, IOSIZE);
2681
2682 ++no_wait_return; // wait_return() done later
2683 emsg((char *)IObuff); // show error highlighted
2684 --no_wait_return;
2685
2686 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689
2690 arg = skipwhite(arg);
2691 }
2692
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002693theend:
2694 if (silent_mode && did_show)
2695 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002696 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002697 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002698 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002699 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002700 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002701 out_flush();
2702 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002703 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002704 }
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return OK;
2707}
2708
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002709/*
2710 * Call this when an option has been given a new value through a user command.
2711 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2712 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002713 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002714did_set_option(
2715 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002716 int opt_flags, // possibly with OPT_MODELINE
2717 int new_value, // value was replaced completely
2718 int value_checked) // value was checked to be safe, no need to set the
2719 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002720{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002721 long_u *p;
2722
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002723 options[opt_idx].flags |= P_WAS_SET;
2724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 // When an option is set in the sandbox, from a modeline or in secure mode
2726 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2727 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002728 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002729 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002730#ifdef HAVE_SANDBOX
2731 || sandbox != 0
2732#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002733 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002734 *p = *p | P_INSECURE;
2735 else if (new_value)
2736 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002737}
2738
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739/*
2740 * Convert a key name or string into a key value.
2741 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002742 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002744 int
2745string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
2747 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002748 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (*arg == '^')
2750 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002751 if (multi_byte)
2752 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 return *arg;
2754}
2755
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756/*
2757 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2758 * maketitle() to create and display it.
2759 * When switching the title or icon off, call mch_restore_title() to get
2760 * the old value back.
2761 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002762 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002763did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
2765 if (starting != NO_SCREEN
2766#ifdef FEAT_GUI
2767 && !gui.starting
2768#endif
2769 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
2773/*
2774 * set_options_bin - called when 'bin' changes value.
2775 */
2776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002777set_options_bin(
2778 int oldval,
2779 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002782 // The option values that are changed when 'bin' changes are
2783 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 if (newval)
2785 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002786 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 {
2788 if (!(opt_flags & OPT_GLOBAL))
2789 {
2790 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2791 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2792 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2793 curbuf->b_p_et_nobin = curbuf->b_p_et;
2794 }
2795 if (!(opt_flags & OPT_LOCAL))
2796 {
2797 p_tw_nobin = p_tw;
2798 p_wm_nobin = p_wm;
2799 p_ml_nobin = p_ml;
2800 p_et_nobin = p_et;
2801 }
2802 }
2803
2804 if (!(opt_flags & OPT_GLOBAL))
2805 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002806 curbuf->b_p_tw = 0; // no automatic line wrap
2807 curbuf->b_p_wm = 0; // no automatic line wrap
2808 curbuf->b_p_ml = 0; // no modelines
2809 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
2811 if (!(opt_flags & OPT_LOCAL))
2812 {
2813 p_tw = 0;
2814 p_wm = 0;
2815 p_ml = FALSE;
2816 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002817 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
2819 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 {
2822 if (!(opt_flags & OPT_GLOBAL))
2823 {
2824 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2825 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2826 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2827 curbuf->b_p_et = curbuf->b_p_et_nobin;
2828 }
2829 if (!(opt_flags & OPT_LOCAL))
2830 {
2831 p_tw = p_tw_nobin;
2832 p_wm = p_wm_nobin;
2833 p_ml = p_ml_nobin;
2834 p_et = p_et_nobin;
2835 }
2836 }
Christian Brabandt757593c2023-08-22 21:44:10 +02002837#if defined(FEAT_EVAL) || defined(PROTO)
2838 // Remember where the dependent option were reset
2839 didset_options_sctx(opt_flags, p_bin_dep_opts);
2840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841}
2842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843/*
2844 * Expand environment variables for some string options.
2845 * These string options cannot be indirect!
2846 * If "val" is NULL expand the current value of the option.
2847 * Return pointer to NameBuff, or NULL when not expanded.
2848 */
2849 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002850option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002852 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2854 return NULL;
2855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002856 // If val is longer than MAXPATHL no meaningful expansion can be done,
2857 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 if (val != NULL && STRLEN(val) > MAXPATHL)
2859 return NULL;
2860
2861 if (val == NULL)
2862 val = *(char_u **)options[opt_idx].var;
2863
2864 /*
2865 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2866 * Escape spaces when expanding 'tags', they are used to separate file
2867 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002868 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 */
2870 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002871 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002872#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002873 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2874#endif
2875 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002876 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 return NULL;
2878
2879 return NameBuff;
2880}
2881
2882/*
2883 * After setting various option values: recompute variables that depend on
2884 * option values.
2885 */
2886 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002887didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 (void)init_chartab();
2891
Bram Moolenaardac13472019-09-16 21:06:21 +02002892 didset_string_options();
2893
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002894#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002895 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002896 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002897 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002898 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002899#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002900 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002901 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002902#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002903 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002904 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002905#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002906 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002907}
2908
2909/*
2910 * More side effects of setting options.
2911 */
2912 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002913didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002914{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002916 (void)highlight_changed();
2917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002918 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002919 check_opt_wim();
2920
Bram Moolenaareed9d462021-02-15 20:38:25 +01002921 // Parse default for 'listchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002922 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE, NULL, 0);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002923
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002924 // Parse default for 'fillchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002925 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002926
2927#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002929 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002930#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002931#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002932 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002933 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002934 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002935 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937}
2938
2939/*
2940 * Check for string options that are NULL (normally only termcap options).
2941 */
2942 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002943check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944{
2945 int opt_idx;
2946
2947 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2948 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2949 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2950}
2951
2952/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002953 * Return the option index found by a pointer into term_strings[].
2954 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002956 int
2957get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002959 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960
2961 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2962 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002963 return opt_idx;
2964 return -1; // cannot happen: didn't find it!
2965}
2966
2967/*
2968 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2969 * Return the option index or -1 if not found.
2970 */
2971 int
2972set_term_option_alloced(char_u **p)
2973{
2974 int opt_idx = get_term_opt_idx(p);
2975
2976 if (opt_idx >= 0)
2977 options[opt_idx].flags |= P_ALLOCED;
2978 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979}
2980
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002981#if defined(FEAT_EVAL) || defined(PROTO)
2982/*
2983 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2984 * Return FALSE when it wasn't.
2985 * Return -1 for an unknown option.
2986 */
2987 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002988was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002989{
2990 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002991 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002992
2993 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002994 {
2995 flagp = insecure_flag(idx, opt_flags);
2996 return (*flagp & P_INSECURE) != 0;
2997 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002998 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002999 return -1;
3000}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003001
3002/*
3003 * Get a pointer to the flags used for the P_INSECURE flag of option
3004 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01003005 * NOTE: Caller must make sure that "curwin" is set to the window from which
3006 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003007 */
3008 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003009insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003010{
3011 if (opt_flags & OPT_LOCAL)
3012 switch ((int)options[opt_idx].indir)
3013 {
3014#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003015 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003016#endif
3017#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00003018# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003019 case PV_FDE: return &curwin->w_p_fde_flags;
3020 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00003021# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00003022# ifdef FEAT_BEVAL
3023 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
3024# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003025 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003026 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003027# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003028 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003029# endif
3030#endif
3031 }
3032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003033 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003034 return &options[opt_idx].flags;
3035}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003036#endif
3037
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003038/*
3039 * Redraw the window title and/or tab page text later.
3040 */
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02003041 void
3042redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003043{
3044 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003045 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003046}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003047
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003049 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
3050 * characters or characters in "allowed".
3051 */
Bram Moolenaare677df82019-09-02 22:31:11 +02003052 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003053valid_name(char_u *val, char *allowed)
3054{
3055 char_u *s;
3056
3057 for (s = val; *s != NUL; ++s)
3058 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
3059 return FALSE;
3060 return TRUE;
3061}
3062
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003063#if defined(FEAT_EVAL) || defined(PROTO)
3064/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003065 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003066 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003067 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003068 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003069set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003070{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003071 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3072 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003073 sctx_T new_script_ctx = script_ctx;
3074
Bram Moolenaar51258742020-05-03 17:19:33 +02003075 // Modeline already has the line number set.
3076 if (!(opt_flags & OPT_MODELINE))
3077 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003078
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003079 // Remember where the option was set. For local options need to do that
3080 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003081 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003082 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003083 if (both || (opt_flags & OPT_LOCAL))
3084 {
3085 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003086 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003087 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003088 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003089 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003090 if (both)
3091 // also setting the "all buffers" value
3092 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3093 new_script_ctx;
3094 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003095 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003096}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003097
3098/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003099 * Get the script context of global option "name".
3100 *
3101 */
3102 sctx_T *
3103get_option_sctx(char *name)
3104{
3105 int idx = findoption((char_u *)name);
3106
3107 if (idx >= 0)
3108 return &options[idx].script_ctx;
3109 siemsg("no such option: %s", name);
3110 return NULL;
3111}
3112
3113/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003114 * Set the script_ctx for a termcap option.
3115 * "name" must be the two character code, e.g. "RV".
3116 * When "name" is NULL use "opt_idx".
3117 */
3118 void
3119set_term_option_sctx_idx(char *name, int opt_idx)
3120{
3121 char_u buf[5];
3122 int idx;
3123
3124 if (name == NULL)
3125 idx = opt_idx;
3126 else
3127 {
3128 buf[0] = 't';
3129 buf[1] = '_';
3130 buf[2] = name[0];
3131 buf[3] = name[1];
3132 buf[4] = 0;
3133 idx = findoption(buf);
3134 }
3135 if (idx >= 0)
3136 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3137}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003138#endif
3139
Bram Moolenaarf4140482020-02-15 23:06:45 +01003140#if defined(FEAT_EVAL)
3141/*
3142 * Apply the OptionSet autocommand.
3143 */
3144 static void
3145apply_optionset_autocmd(
3146 int opt_idx,
3147 long opt_flags,
3148 long oldval,
3149 long oldval_g,
3150 long newval,
3151 char *errmsg)
3152{
3153 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3154
3155 // Don't do this while starting up, failure or recursively.
3156 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3157 return;
3158
3159 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3160 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3161 oldval_g);
3162 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3163 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3164 (opt_flags & OPT_LOCAL) ? "local" : "global");
3165 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3166 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3167 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3168 if (opt_flags & OPT_LOCAL)
3169 {
3170 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3171 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3172 }
3173 if (opt_flags & OPT_GLOBAL)
3174 {
3175 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3176 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3177 }
3178 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3179 {
3180 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3181 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3182 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3183 }
3184 if (opt_flags & OPT_MODELINE)
3185 {
3186 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3187 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3188 }
3189 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3190 NULL, FALSE, NULL);
3191 reset_v_option_vars();
3192}
3193#endif
3194
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003195#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003196/*
3197 * Process the updated 'arabic' option value.
3198 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003199 char *
3200did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003201{
3202 char *errmsg = NULL;
3203
3204 if (curwin->w_p_arab)
3205 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003206 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003207 if (!p_tbidi)
3208 {
3209 // set rightleft mode
3210 if (!curwin->w_p_rl)
3211 {
3212 curwin->w_p_rl = TRUE;
3213 changed_window_setting();
3214 }
3215
3216 // Enable Arabic shaping (major part of what Arabic requires)
3217 if (!p_arshape)
3218 {
3219 p_arshape = TRUE;
3220 redraw_later_clear();
3221 }
3222 }
3223
3224 // Arabic requires a utf-8 encoding, inform the user if it's not
3225 // set.
3226 if (STRCMP(p_enc, "utf-8") != 0)
3227 {
3228 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3229
3230 msg_source(HL_ATTR(HLF_W));
3231 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3232#ifdef FEAT_EVAL
3233 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3234#endif
3235 }
3236
3237 // set 'delcombine'
3238 p_deco = TRUE;
3239
3240# ifdef FEAT_KEYMAP
3241 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003242 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3243 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003244# endif
3245 }
3246 else
3247 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003248 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003249 if (!p_tbidi)
3250 {
3251 // reset rightleft mode
3252 if (curwin->w_p_rl)
3253 {
3254 curwin->w_p_rl = FALSE;
3255 changed_window_setting();
3256 }
3257
3258 // 'arabicshape' isn't reset, it is a global option and
3259 // another window may still need it "on".
3260 }
3261
3262 // 'delcombine' isn't reset, it is a global option and another
3263 // window may still want it "on".
3264
3265# ifdef FEAT_KEYMAP
3266 // Revert to the default keymap
3267 curbuf->b_p_iminsert = B_IMODE_NONE;
3268 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3269# endif
3270 }
3271
3272 return errmsg;
3273}
3274#endif
3275
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003276#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3277/*
3278 * Process the updated 'autochdir' option value.
3279 */
3280 char *
3281did_set_autochdir(optset_T *args UNUSED)
3282{
3283 // Change directories when the 'acd' option is set now.
3284 DO_AUTOCHDIR;
3285 return NULL;
3286}
3287#endif
3288
3289#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3290/*
3291 * Process the updated 'ballooneval' option value.
3292 */
3293 char *
3294did_set_ballooneval(optset_T *args)
3295{
3296 if (balloonEvalForTerm)
3297 return NULL;
3298
3299 if (p_beval && !args->os_oldval.boolean)
3300 gui_mch_enable_beval_area(balloonEval);
3301 else if (!p_beval && args->os_oldval.boolean)
3302 gui_mch_disable_beval_area(balloonEval);
3303
3304 return NULL;
3305}
3306#endif
3307
3308#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3309/*
3310 * Process the updated 'balloonevalterm' option value.
3311 */
3312 char *
3313did_set_balloonevalterm(optset_T *args UNUSED)
3314{
3315 mch_bevalterm_changed();
3316 return NULL;
3317}
3318#endif
3319
3320/*
3321 * Process the updated 'binary' option value.
3322 */
3323 char *
3324did_set_binary(optset_T *args)
3325{
3326 // when 'bin' is set also set some other options
3327 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3328 redraw_titles();
3329
3330 return NULL;
3331}
3332
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003333/*
3334 * Process the updated 'buflisted' option value.
3335 */
3336 char *
3337did_set_buflisted(optset_T *args)
3338{
3339 // when 'buflisted' changes, trigger autocommands
3340 if (args->os_oldval.boolean != curbuf->b_p_bl)
3341 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3342 NULL, NULL, TRUE, curbuf);
3343 return NULL;
3344}
3345
3346/*
3347 * Process the new 'cmdheight' option value.
3348 */
3349 char *
3350did_set_cmdheight(optset_T *args)
3351{
3352 long old_value = args->os_oldval.number;
3353 char *errmsg = NULL;
3354
3355 // if p_ch changed value, change the command line height
3356 if (p_ch < 1)
3357 {
3358 errmsg = e_argument_must_be_positive;
3359 p_ch = 1;
3360 }
3361 if (p_ch > Rows - min_rows() + 1)
3362 p_ch = Rows - min_rows() + 1;
3363
3364 // Only compute the new window layout when startup has been
3365 // completed. Otherwise the frame sizes may be wrong.
3366 if ((p_ch != old_value
3367 || tabline_height() + topframe->fr_height != Rows - p_ch)
3368 && full_screen
3369#ifdef FEAT_GUI
3370 && !gui.starting
3371#endif
3372 )
3373 command_height();
3374
3375 return errmsg;
3376}
3377
3378/*
3379 * Process the updated 'compatible' option value.
3380 */
3381 char *
3382did_set_compatible(optset_T *args UNUSED)
3383{
3384 compatible_set();
3385 return NULL;
3386}
3387
3388#if defined(FEAT_CONCEAL) || defined(PROTO)
3389/*
3390 * Process the new 'conceallevel' option value.
3391 */
3392 char *
3393did_set_conceallevel(optset_T *args UNUSED)
3394{
3395 char *errmsg = NULL;
3396
3397 if (curwin->w_p_cole < 0)
3398 {
3399 errmsg = e_argument_must_be_positive;
3400 curwin->w_p_cole = 0;
3401 }
3402 else if (curwin->w_p_cole > 3)
3403 {
3404 errmsg = e_invalid_argument;
3405 curwin->w_p_cole = 3;
3406 }
3407
3408 return errmsg;
3409}
3410#endif
3411
3412#if defined(FEAT_DIFF) || defined(PROTO)
3413/*
3414 * Process the updated 'diff' option value.
3415 */
3416 char *
3417did_set_diff(optset_T *args UNUSED)
3418{
3419 // May add or remove the buffer from the list of diff buffers.
3420 diff_buf_adjust(curwin);
3421# ifdef FEAT_FOLDING
3422 if (foldmethodIsDiff(curwin))
3423 foldUpdateAll(curwin);
3424# endif
3425 return NULL;
3426}
3427#endif
3428
3429/*
3430 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3431 * option value.
3432 */
3433 char *
3434did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3435{
3436 // redraw the window title and tab page text
3437 redraw_titles();
3438 return NULL;
3439}
3440
3441/*
3442 * Process the updated 'equalalways' option value.
3443 */
3444 char *
3445did_set_equalalways(optset_T *args)
3446{
3447 if (p_ea && !args->os_oldval.boolean)
3448 win_equal(curwin, FALSE, 0);
3449
3450 return NULL;
3451}
3452
3453#if defined(FEAT_FOLDING) || defined(PROTO)
3454/*
3455 * Process the new 'foldcolumn' option value.
3456 */
3457 char *
3458did_set_foldcolumn(optset_T *args UNUSED)
3459{
3460 char *errmsg = NULL;
3461
3462 if (curwin->w_p_fdc < 0)
3463 {
3464 errmsg = e_argument_must_be_positive;
3465 curwin->w_p_fdc = 0;
3466 }
3467 else if (curwin->w_p_fdc > 12)
3468 {
3469 errmsg = e_invalid_argument;
3470 curwin->w_p_fdc = 12;
3471 }
3472
3473 return errmsg;
3474}
3475
3476/*
3477 * Process the new 'foldlevel' option value.
3478 */
3479 char *
3480did_set_foldlevel(optset_T *args UNUSED)
3481{
3482 if (curwin->w_p_fdl < 0)
3483 curwin->w_p_fdl = 0;
3484 newFoldLevel();
3485 return NULL;
3486}
3487
3488/*
3489 * Process the new 'foldminlines' option value.
3490 */
3491 char *
3492did_set_foldminlines(optset_T *args UNUSED)
3493{
3494 foldUpdateAll(curwin);
3495 return NULL;
3496}
3497
3498/*
3499 * Process the new 'foldnestmax' option value.
3500 */
3501 char *
3502did_set_foldnestmax(optset_T *args UNUSED)
3503{
3504 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3505 foldUpdateAll(curwin);
3506 return NULL;
3507}
3508#endif
3509
3510#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3511/*
3512 * Process the updated 'hlsearch' option value.
3513 */
3514 char *
3515did_set_hlsearch(optset_T *args UNUSED)
3516{
3517 // when 'hlsearch' is set or reset: reset no_hlsearch
3518 set_no_hlsearch(FALSE);
3519 return NULL;
3520}
3521#endif
3522
3523/*
3524 * Process the updated 'ignorecase' option value.
3525 */
3526 char *
3527did_set_ignorecase(optset_T *args UNUSED)
3528{
3529 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3530 if (p_hls)
3531 redraw_all_later(UPD_SOME_VALID);
3532 return NULL;
3533}
3534
3535#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3536/*
3537 * Process the updated 'imdisable' option value.
3538 */
3539 char *
3540did_set_imdisable(optset_T *args UNUSED)
3541{
3542 // Only de-activate it here, it will be enabled when changing mode.
3543 if (p_imdisable)
3544 im_set_active(FALSE);
3545 else if (State & MODE_INSERT)
3546 // When the option is set from an autocommand, it may need to take
3547 // effect right away.
3548 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3549 return NULL;
3550}
3551#endif
3552
3553/*
3554 * Process the new 'iminsert' option value.
3555 */
3556 char *
3557did_set_iminsert(optset_T *args UNUSED)
3558{
3559 char *errmsg = NULL;
3560
3561 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3562 {
3563 errmsg = e_invalid_argument;
3564 curbuf->b_p_iminsert = B_IMODE_NONE;
3565 }
3566 p_iminsert = curbuf->b_p_iminsert;
3567 if (termcap_active) // don't do this in the alternate screen
3568 showmode();
3569#if defined(FEAT_KEYMAP)
3570 // Show/unshow value of 'keymap' in status lines.
3571 status_redraw_curbuf();
3572#endif
3573
3574 return errmsg;
3575}
3576
3577/*
3578 * Process the new 'imsearch' option value.
3579 */
3580 char *
3581did_set_imsearch(optset_T *args UNUSED)
3582{
3583 char *errmsg = NULL;
3584
3585 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3586 {
3587 errmsg = e_invalid_argument;
3588 curbuf->b_p_imsearch = B_IMODE_NONE;
3589 }
3590 p_imsearch = curbuf->b_p_imsearch;
3591
3592 return errmsg;
3593}
3594
3595#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3596/*
3597 * Process the new 'imstyle' option value.
3598 */
3599 char *
3600did_set_imstyle(optset_T *args UNUSED)
3601{
3602 char *errmsg = NULL;
3603
3604 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3605 errmsg = e_invalid_argument;
3606
3607 return errmsg;
3608}
3609#endif
3610
3611/*
3612 * Process the updated 'insertmode' option value.
3613 */
3614 char *
3615did_set_insertmode(optset_T *args)
3616{
3617 // when 'insertmode' is set from an autocommand need to do work here
3618 if (p_im)
3619 {
3620 if ((State & MODE_INSERT) == 0)
3621 need_start_insertmode = TRUE;
3622 stop_insert_mode = FALSE;
3623 }
3624 // only reset if it was set previously
3625 else if (args->os_oldval.boolean)
3626 {
3627 need_start_insertmode = FALSE;
3628 stop_insert_mode = TRUE;
3629 if (restart_edit != 0 && mode_displayed)
3630 clear_cmdline = TRUE; // remove "(insert)"
3631 restart_edit = 0;
3632 }
3633
3634 return NULL;
3635}
3636
3637#if defined(FEAT_LANGMAP) || defined(PROTO)
3638/*
3639 * Process the updated 'langnoremap' option value.
3640 */
3641 char *
3642did_set_langnoremap(optset_T *args UNUSED)
3643{
3644 // 'langnoremap' -> !'langremap'
3645 p_lrm = !p_lnr;
3646 return NULL;
3647}
3648
3649/*
3650 * Process the updated 'langremap' option value.
3651 */
3652 char *
3653did_set_langremap(optset_T *args UNUSED)
3654{
3655 // 'langremap' -> !'langnoremap'
3656 p_lnr = !p_lrm;
3657 return NULL;
3658}
3659#endif
3660
3661/*
3662 * Process the new 'laststatus' option value.
3663 */
3664 char *
3665did_set_laststatus(optset_T *args UNUSED)
3666{
3667 last_status(FALSE); // (re)set last window status line
3668 return NULL;
3669}
3670
3671#if defined(FEAT_GUI) || defined(PROTO)
3672/*
3673 * Process the new 'linespace' option value.
3674 */
3675 char *
3676did_set_linespace(optset_T *args UNUSED)
3677{
3678 // Recompute gui.char_height and resize the Vim window to keep the
3679 // same number of lines.
3680 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3681 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3682 return NULL;
3683}
3684#endif
3685
3686/*
3687 * Process the updated 'lisp' option value.
3688 */
3689 char *
3690did_set_lisp(optset_T *args UNUSED)
3691{
3692 // When 'lisp' option changes include/exclude '-' in keyword characters.
3693 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3694 return NULL;
3695}
3696
3697/*
3698 * Process the new 'maxcombine' option value.
3699 */
3700 char *
3701did_set_maxcombine(optset_T *args UNUSED)
3702{
3703 if (p_mco > MAX_MCO)
3704 p_mco = MAX_MCO;
3705 else if (p_mco < 0)
3706 p_mco = 0;
3707 screenclear(); // will re-allocate the screen
3708 return NULL;
3709}
3710
3711/*
3712 * Process the updated 'modifiable' option value.
3713 */
3714 char *
3715did_set_modifiable(optset_T *args UNUSED)
3716{
3717 // when 'modifiable' is changed, redraw the window title
3718
3719# ifdef FEAT_TERMINAL
3720 // Cannot set 'modifiable' when in Terminal mode.
3721 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3722 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3723 {
3724 curbuf->b_p_ma = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003725 return e_cannot_make_terminal_with_running_job_modifiable;
3726 }
3727# endif
3728 redraw_titles();
3729
3730 return NULL;
3731}
3732
3733/*
3734 * Process the updated 'modified' option value.
3735 */
3736 char *
3737did_set_modified(optset_T *args)
3738{
3739 if (!args->os_newval.boolean)
3740 save_file_ff(curbuf); // Buffer is unchanged
3741 redraw_titles();
zeertzjq5bf6c212024-03-31 18:41:27 +02003742 curbuf->b_modified_was_set = args->os_newval.boolean;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003743 return NULL;
3744}
3745
3746#if defined(FEAT_GUI) || defined(PROTO)
3747/*
3748 * Process the updated 'mousehide' option value.
3749 */
3750 char *
3751did_set_mousehide(optset_T *args UNUSED)
3752{
3753 if (!p_mh)
3754 gui_mch_mousehide(FALSE);
3755 return NULL;
3756}
3757#endif
3758
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003759/*
3760 * Process the updated 'number' or 'relativenumber' option value.
3761 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003762 char *
3763did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003764{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003765#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003766 if (gui.in_use
3767 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3768 && curbuf->b_signlist != NULL)
3769 {
3770 // If the 'number' or 'relativenumber' options are modified and
3771 // 'signcolumn' is set to 'number', then clear the screen for a full
3772 // refresh. Otherwise the sign icons are not displayed properly in the
3773 // number column. If the 'number' option is set and only the
3774 // 'relativenumber' option is toggled, then don't refresh the screen
3775 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003776 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003777 redraw_all_later(UPD_CLEAR);
3778 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003779#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003780 return NULL;
3781}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003782
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003783#if defined(FEAT_LINEBREAK) || defined(PROTO)
3784/*
3785 * Process the new 'numberwidth' option value.
3786 */
3787 char *
3788did_set_numberwidth(optset_T *args UNUSED)
3789{
3790 char *errmsg = NULL;
3791
3792 // 'numberwidth' must be positive
3793 if (curwin->w_p_nuw < 1)
3794 {
3795 errmsg = e_argument_must_be_positive;
3796 curwin->w_p_nuw = 1;
3797 }
3798 if (curwin->w_p_nuw > 20)
3799 {
3800 errmsg = e_invalid_argument;
3801 curwin->w_p_nuw = 20;
3802 }
3803 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3804
3805 return errmsg;
3806}
3807#endif
3808
3809/*
3810 * Process the updated 'paste' option value. Called after p_paste was set or
3811 * reset. When 'paste' is set or reset also change other options.
3812 */
3813 char *
3814did_set_paste(optset_T *args UNUSED)
3815{
3816 static int old_p_paste = FALSE;
3817 static int save_sm = 0;
3818 static int save_sta = 0;
3819 static int save_ru = 0;
3820#ifdef FEAT_RIGHTLEFT
3821 static int save_ri = 0;
3822 static int save_hkmap = 0;
3823#endif
3824 buf_T *buf;
3825
3826 if (p_paste)
3827 {
3828 // Paste switched from off to on.
3829 // Save the current values, so they can be restored later.
3830 if (!old_p_paste)
3831 {
3832 // save options for each buffer
3833 FOR_ALL_BUFFERS(buf)
3834 {
3835 buf->b_p_tw_nopaste = buf->b_p_tw;
3836 buf->b_p_wm_nopaste = buf->b_p_wm;
3837 buf->b_p_sts_nopaste = buf->b_p_sts;
3838 buf->b_p_ai_nopaste = buf->b_p_ai;
3839 buf->b_p_et_nopaste = buf->b_p_et;
3840#ifdef FEAT_VARTABS
3841 if (buf->b_p_vsts_nopaste)
3842 vim_free(buf->b_p_vsts_nopaste);
3843 buf->b_p_vsts_nopaste =
3844 buf->b_p_vsts && buf->b_p_vsts != empty_option
3845 ? vim_strsave(buf->b_p_vsts) : NULL;
3846#endif
3847 }
3848
3849 // save global options
3850 save_sm = p_sm;
3851 save_sta = p_sta;
3852 save_ru = p_ru;
3853#ifdef FEAT_RIGHTLEFT
3854 save_ri = p_ri;
3855 save_hkmap = p_hkmap;
3856#endif
3857 // save global values for local buffer options
3858 p_ai_nopaste = p_ai;
3859 p_et_nopaste = p_et;
3860 p_sts_nopaste = p_sts;
3861 p_tw_nopaste = p_tw;
3862 p_wm_nopaste = p_wm;
3863#ifdef FEAT_VARTABS
3864 if (p_vsts_nopaste)
3865 vim_free(p_vsts_nopaste);
3866 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3867 ? vim_strsave(p_vsts) : NULL;
3868#endif
3869 }
3870
3871 // Always set the option values, also when 'paste' is set when it is
3872 // already on. Set options for each buffer.
3873 FOR_ALL_BUFFERS(buf)
3874 {
3875 buf->b_p_tw = 0; // textwidth is 0
3876 buf->b_p_wm = 0; // wrapmargin is 0
3877 buf->b_p_sts = 0; // softtabstop is 0
3878 buf->b_p_ai = 0; // no auto-indent
3879 buf->b_p_et = 0; // no expandtab
3880#ifdef FEAT_VARTABS
3881 if (buf->b_p_vsts)
3882 free_string_option(buf->b_p_vsts);
3883 buf->b_p_vsts = empty_option;
3884 VIM_CLEAR(buf->b_p_vsts_array);
3885#endif
3886 }
3887
3888 // set global options
3889 p_sm = 0; // no showmatch
3890 p_sta = 0; // no smarttab
3891 if (p_ru)
3892 status_redraw_all(); // redraw to remove the ruler
3893 p_ru = 0; // no ruler
3894#ifdef FEAT_RIGHTLEFT
3895 p_ri = 0; // no reverse insert
3896 p_hkmap = 0; // no Hebrew keyboard
3897#endif
3898 // set global values for local buffer options
3899 p_tw = 0;
3900 p_wm = 0;
3901 p_sts = 0;
3902 p_ai = 0;
Christian Brabandt757593c2023-08-22 21:44:10 +02003903 p_et = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003904#ifdef FEAT_VARTABS
3905 if (p_vsts)
3906 free_string_option(p_vsts);
3907 p_vsts = empty_option;
3908#endif
3909 }
3910
3911 // Paste switched from on to off: Restore saved values.
3912 else if (old_p_paste)
3913 {
3914 // restore options for each buffer
3915 FOR_ALL_BUFFERS(buf)
3916 {
3917 buf->b_p_tw = buf->b_p_tw_nopaste;
3918 buf->b_p_wm = buf->b_p_wm_nopaste;
3919 buf->b_p_sts = buf->b_p_sts_nopaste;
3920 buf->b_p_ai = buf->b_p_ai_nopaste;
3921 buf->b_p_et = buf->b_p_et_nopaste;
3922#ifdef FEAT_VARTABS
3923 if (buf->b_p_vsts)
3924 free_string_option(buf->b_p_vsts);
3925 buf->b_p_vsts = buf->b_p_vsts_nopaste
3926 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3927 vim_free(buf->b_p_vsts_array);
3928 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3929 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3930 else
3931 buf->b_p_vsts_array = NULL;
3932#endif
3933 }
3934
3935 // restore global options
3936 p_sm = save_sm;
3937 p_sta = save_sta;
3938 if (p_ru != save_ru)
3939 status_redraw_all(); // redraw to draw the ruler
3940 p_ru = save_ru;
3941#ifdef FEAT_RIGHTLEFT
3942 p_ri = save_ri;
3943 p_hkmap = save_hkmap;
3944#endif
3945 // set global values for local buffer options
3946 p_ai = p_ai_nopaste;
3947 p_et = p_et_nopaste;
3948 p_sts = p_sts_nopaste;
3949 p_tw = p_tw_nopaste;
3950 p_wm = p_wm_nopaste;
3951#ifdef FEAT_VARTABS
3952 if (p_vsts)
3953 free_string_option(p_vsts);
3954 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
3955#endif
3956 }
3957
3958 old_p_paste = p_paste;
3959
Christian Brabandt757593c2023-08-22 21:44:10 +02003960#if defined(FEAT_EVAL) || defined(PROTO)
3961 // Remember where the dependent options were reset
3962 didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
3963#endif
3964
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003965 return NULL;
3966}
3967
3968
3969#ifdef FEAT_QUICKFIX
3970/*
3971 * Process the updated 'previewwindow' option value.
3972 */
3973 char *
Yegappan Lakshmanan3ee25962023-12-04 20:31:14 +01003974did_set_previewwindow(optset_T *args UNUSED)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003975{
3976 if (!curwin->w_p_pvw)
3977 return NULL;
3978
3979 // There can be only one window with 'previewwindow' set.
3980 win_T *win;
3981
3982 FOR_ALL_WINDOWS(win)
3983 if (win->w_p_pvw && win != curwin)
3984 {
3985 curwin->w_p_pvw = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003986 return e_preview_window_already_exists;
3987 }
3988
3989 return NULL;
3990}
3991#endif
3992
3993#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
3994/*
3995 * Process the new 'pyxversion' option value.
3996 */
3997 char *
3998did_set_pyxversion(optset_T *args UNUSED)
3999{
4000 char *errmsg = NULL;
4001
4002 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4003 errmsg = e_invalid_argument;
4004
4005 return errmsg;
4006}
4007#endif
4008
4009/*
4010 * Process the updated 'readonly' option value.
4011 */
4012 char *
4013did_set_readonly(optset_T *args)
4014{
4015 // when 'readonly' is reset globally, also reset readonlymode
4016 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
4017 readonlymode = FALSE;
4018
4019 // when 'readonly' is set may give W10 again
4020 if (curbuf->b_p_ro)
4021 curbuf->b_did_warn = FALSE;
4022
4023 redraw_titles();
4024
4025 return NULL;
4026}
4027
4028/*
4029 * Process the updated 'scrollbind' option value.
4030 */
4031 char *
4032did_set_scrollbind(optset_T *args UNUSED)
4033{
4034 // when 'scrollbind' is set: snapshot the current position to avoid a jump
4035 // at the end of normal_cmd()
4036 if (!curwin->w_p_scb)
4037 return NULL;
4038
4039 do_check_scrollbind(FALSE);
4040 curwin->w_scbind_pos = curwin->w_topline;
4041 return NULL;
4042}
4043
4044#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
4045/*
4046 * Process the updated 'shellslash' option value.
4047 */
4048 char *
4049did_set_shellslash(optset_T *args UNUSED)
4050{
4051 if (p_ssl)
4052 {
4053 psepc = '/';
4054 psepcN = '\\';
4055 pseps[0] = '/';
4056 }
4057 else
4058 {
4059 psepc = '\\';
4060 psepcN = '/';
4061 pseps[0] = '\\';
4062 }
4063
4064 // need to adjust the file name arguments and buffer names.
4065 buflist_slash_adjust();
4066 alist_slash_adjust();
4067# ifdef FEAT_EVAL
4068 scriptnames_slash_adjust();
4069# endif
4070 return NULL;
4071}
4072#endif
4073
4074/*
4075 * Process the new 'shiftwidth' or the 'tabstop' option value.
4076 */
4077 char *
4078did_set_shiftwidth_tabstop(optset_T *args)
4079{
4080 long *pp = (long *)args->os_varp;
4081 char *errmsg = NULL;
4082
4083 if (curbuf->b_p_sw < 0)
4084 {
4085 errmsg = e_argument_must_be_positive;
4086#ifdef FEAT_VARTABS
4087 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4088 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4089 ? tabstop_first(curbuf->b_p_vts_array)
4090 : curbuf->b_p_ts;
4091#else
4092 curbuf->b_p_sw = curbuf->b_p_ts;
4093#endif
4094 }
4095
4096#ifdef FEAT_FOLDING
4097 if (foldmethodIsIndent(curwin))
4098 foldUpdateAll(curwin);
4099#endif
4100 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4101 // parse 'cinoptions'.
4102 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4103 parse_cino(curbuf);
4104
4105 return errmsg;
4106}
4107
4108/*
4109 * Process the new 'showtabline' option value.
4110 */
4111 char *
4112did_set_showtabline(optset_T *args UNUSED)
4113{
4114 shell_new_rows(); // recompute window positions and heights
4115 return NULL;
4116}
4117
4118/*
4119 * Process the updated 'smoothscroll' option value.
4120 */
4121 char *
4122did_set_smoothscroll(optset_T *args UNUSED)
4123{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004124 if (!curwin->w_p_sms)
4125 curwin->w_skipcol = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004126
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004127 return NULL;
4128}
4129
4130#if defined(FEAT_SPELL) || defined(PROTO)
4131/*
4132 * Process the updated 'spell' option value.
4133 */
4134 char *
4135did_set_spell(optset_T *args UNUSED)
4136{
4137 if (curwin->w_p_spell)
4138 return parse_spelllang(curwin);
4139
4140 return NULL;
4141}
4142#endif
4143
4144/*
4145 * Process the updated 'swapfile' option value.
4146 */
4147 char *
4148did_set_swapfile(optset_T *args UNUSED)
4149{
4150 // when 'swf' is set, create swapfile, when reset remove swapfile
4151 if (curbuf->b_p_swf && p_uc)
4152 ml_open_file(curbuf); // create the swap file
4153 else
4154 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4155 // buf->b_p_swf
4156 mf_close_file(curbuf, TRUE); // remove the swap file
4157 return NULL;
4158}
4159
4160#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004161 char *
4162did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004163{
4164# ifdef FEAT_VTP
4165 // Do not turn on 'tgc' when 24-bit colors are not supported.
4166 if (
4167# ifdef VIMDLL
4168 !gui.in_use && !gui.starting &&
4169# endif
4170 !has_vtp_working())
4171 {
4172 p_tgc = 0;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004173 return e_24_bit_colors_are_not_supported_on_this_environment;
4174 }
4175 if (is_term_win32())
4176 swap_tcap();
4177# endif
4178# ifdef FEAT_GUI
4179 if (!gui.in_use && !gui.starting)
4180# endif
4181 highlight_gui_started();
4182# ifdef FEAT_VTP
4183 // reset t_Co
4184 if (is_term_win32())
4185 {
4186 control_console_color_rgb();
4187 set_termname(T_NAME);
4188 init_highlight(TRUE, FALSE);
4189 }
4190# endif
4191# ifdef FEAT_TERMINAL
4192 term_update_colors_all();
4193 term_update_palette_all();
4194 term_update_wincolor_all();
4195# endif
4196
4197 return NULL;
4198}
4199#endif
4200
4201/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004202 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004204 char *
4205did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004207 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004209 // when 'terse' is set change 'shortmess'
4210 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004212 // insert 's' in p_shm
4213 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004214 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004215 STRCPY(IObuff, p_shm);
4216 STRCAT(IObuff, "s");
4217 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004218 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004219 // remove 's' from p_shm
4220 else if (!p_terse && p != NULL)
4221 STRMOVE(p, p + 1);
4222 return NULL;
4223}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004225/*
4226 * Process the updated 'textauto' option value.
4227 */
4228 char *
4229did_set_textauto(optset_T *args)
4230{
4231 // when 'textauto' is set or reset also change 'fileformats'
4232 set_string_option_direct((char_u *)"ffs", -1,
4233 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4234 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004235
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004236 return NULL;
4237}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004239/*
4240 * Process the updated 'textmode' option value.
4241 */
4242 char *
4243did_set_textmode(optset_T *args)
4244{
4245 // when 'textmode' is set or reset also change 'fileformat'
4246 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4247
4248 return NULL;
4249}
4250
4251/*
4252 * Process the new 'textwidth' option value.
4253 */
4254 char *
4255did_set_textwidth(optset_T *args UNUSED)
4256{
4257 char *errmsg = NULL;
4258
4259 if (curbuf->b_p_tw < 0)
4260 {
4261 errmsg = e_argument_must_be_positive;
4262 curbuf->b_p_tw = 0;
4263 }
4264#ifdef FEAT_SYN_HL
4265 {
4266 win_T *wp;
4267 tabpage_T *tp;
4268
4269 FOR_ALL_TAB_WINDOWS(tp, wp)
4270 check_colorcolumn(wp);
4271 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004272#endif
4273
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004274 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275}
4276
4277/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004278 * Process the updated 'title' or the 'icon' option value.
4279 */
4280 char *
4281did_set_title_icon(optset_T *args UNUSED)
4282{
4283 // when 'title' changed, may need to change the title; same for 'icon'
4284 did_set_title();
4285 return NULL;
4286}
4287
4288/*
4289 * Process the new 'titlelen' option value.
4290 */
4291 char *
4292did_set_titlelen(optset_T *args)
4293{
4294 long old_value = args->os_oldval.number;
4295 char *errmsg = NULL;
4296
4297 // if 'titlelen' has changed, redraw the title
4298 if (p_titlelen < 0)
4299 {
4300 errmsg = e_argument_must_be_positive;
4301 p_titlelen = 85;
4302 }
4303 if (starting != NO_SCREEN && old_value != p_titlelen)
4304 need_maketitle = TRUE;
4305
4306 return errmsg;
4307}
4308
4309#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4310/*
4311 * Process the updated 'undofile' option value.
4312 */
4313 char *
4314did_set_undofile(optset_T *args)
4315{
4316 // Only take action when the option was set.
4317 if (!curbuf->b_p_udf && !p_udf)
4318 return NULL;
4319
4320 // When reset we do not delete the undo file, the option may be set again
4321 // without making any changes in between.
4322 char_u hash[UNDO_HASH_SIZE];
4323 buf_T *save_curbuf = curbuf;
4324
4325 FOR_ALL_BUFFERS(curbuf)
4326 {
4327 // When 'undofile' is set globally: for every buffer, otherwise
4328 // only for the current buffer: Try to read in the undofile,
4329 // if one exists, the buffer wasn't changed and the buffer was
4330 // loaded
4331 if ((curbuf == save_curbuf
4332 || (args->os_flags & OPT_GLOBAL)
4333 || args->os_flags == 0)
4334 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4335 {
4336#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004337 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004338 continue;
4339#endif
4340 u_compute_hash(hash);
4341 u_read_undo(NULL, hash, curbuf->b_fname);
4342 }
4343 }
4344 curbuf = save_curbuf;
4345
4346 return NULL;
4347}
4348#endif
4349
4350/*
4351 * Process the new global 'undolevels' option value.
4352 */
4353 static void
4354update_global_undolevels(long value, long old_value)
4355{
4356 // sync undo before 'undolevels' changes
4357
4358 // use the old value, otherwise u_sync() may not work properly
4359 p_ul = old_value;
4360 u_sync(TRUE);
4361 p_ul = value;
4362}
4363
4364/*
4365 * Process the new buffer local 'undolevels' option value.
4366 */
4367 static void
4368update_buflocal_undolevels(long value, long old_value)
4369{
4370 // use the old value, otherwise u_sync() may not work properly
4371 curbuf->b_p_ul = old_value;
4372 u_sync(TRUE);
4373 curbuf->b_p_ul = value;
4374}
4375
4376/*
4377 * Process the new 'undolevels' option value.
4378 */
4379 char *
4380did_set_undolevels(optset_T *args)
4381{
4382 long *pp = (long *)args->os_varp;
4383
4384 if (pp == &p_ul) // global 'undolevels'
4385 update_global_undolevels(args->os_newval.number,
4386 args->os_oldval.number);
4387 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4388 update_buflocal_undolevels(args->os_newval.number,
4389 args->os_oldval.number);
4390
4391 return NULL;
4392}
4393
4394/*
4395 * Process the new 'updatecount' option value.
4396 */
4397 char *
4398did_set_updatecount(optset_T *args)
4399{
4400 long old_value = args->os_oldval.number;
4401 char *errmsg = NULL;
4402
4403 // when 'updatecount' changes from zero to non-zero, open swap files
4404 if (p_uc < 0)
4405 {
4406 errmsg = e_argument_must_be_positive;
4407 p_uc = 100;
4408 }
4409 if (p_uc && !old_value)
4410 ml_open_files();
4411
4412 return errmsg;
4413}
4414
4415/*
4416 * Process the updated 'weirdinvert' option value.
4417 */
4418 char *
4419did_set_weirdinvert(optset_T *args)
4420{
4421 // When 'weirdinvert' changed, set/reset 't_xs'.
4422 // Then set 'weirdinvert' according to value of 't_xs'.
4423 if (p_wiv && !args->os_oldval.boolean)
4424 T_XS = (char_u *)"y";
4425 else if (!p_wiv && args->os_oldval.boolean)
4426 T_XS = empty_option;
4427 p_wiv = (*T_XS != NUL);
4428
4429 return NULL;
4430}
4431
4432/*
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02004433 * Process the new 'wildchar' / 'wildcharm' option value.
4434 */
4435 char *
4436did_set_wildchar(optset_T *args)
4437{
4438 long c = *(long *)args->os_varp;
4439
4440 // Don't allow key values that wouldn't work as wildchar.
4441 if (c == Ctrl_C || c == '\n' || c == '\r' || c == K_KENTER)
4442 return e_invalid_argument;
4443
4444 return NULL;
4445}
4446
4447/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004448 * Process the new 'window' option value.
4449 */
4450 char *
4451did_set_window(optset_T *args UNUSED)
4452{
4453 if (p_window < 1)
4454 p_window = 1;
4455 else if (p_window >= Rows)
4456 p_window = Rows - 1;
4457 return NULL;
4458}
4459
4460/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004461 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004463 char *
4464did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004466 long *pp = (long *)args->os_varp;
4467 char *errmsg = NULL;
4468
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004469 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004471 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004472 p_wh = 1;
4473 }
4474 if (p_wmh > p_wh)
4475 {
4476 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4477 p_wh = p_wmh;
4478 }
4479 if (p_hh < 0)
4480 {
4481 errmsg = e_argument_must_be_positive;
4482 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
4484
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004485 // Change window height NOW
4486 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004488 if (pp == &p_wh && curwin->w_height < p_wh)
4489 win_setheight((int)p_wh);
4490 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4491 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004494 return errmsg;
4495}
4496
4497/*
4498 * Process the new 'winminheight' option value.
4499 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004500 char *
4501did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004502{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004503 char *errmsg = NULL;
4504
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004505 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004507 errmsg = e_argument_must_be_positive;
4508 p_wmh = 0;
4509 }
4510 if (p_wmh > p_wh)
4511 {
4512 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4513 p_wmh = p_wh;
4514 }
4515 win_setminheight();
4516
4517 return errmsg;
4518}
4519
4520/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004521 * Process the new 'winminwidth' option value.
4522 */
4523 char *
4524did_set_winminwidth(optset_T *args UNUSED)
4525{
4526 char *errmsg = NULL;
4527
4528 if (p_wmw < 0)
4529 {
4530 errmsg = e_argument_must_be_positive;
4531 p_wmw = 0;
4532 }
4533 if (p_wmw > p_wiw)
4534 {
4535 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4536 p_wmw = p_wiw;
4537 }
4538 win_setminwidth();
4539
4540 return errmsg;
4541}
4542
4543/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004544 * Process the new 'winwidth' option value.
4545 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004546 char *
4547did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004548{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004549 char *errmsg = NULL;
4550
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004551 if (p_wiw < 1)
4552 {
4553 errmsg = e_argument_must_be_positive;
4554 p_wiw = 1;
4555 }
4556 if (p_wmw > p_wiw)
4557 {
4558 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4559 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004561
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004562 // Change window width NOW
4563 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4564 win_setwidth((int)p_wiw);
4565
4566 return errmsg;
4567}
4568
4569/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004570 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004571 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004572 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004573did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004574{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004575 // Set w_leftcol or w_skipcol to zero.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004576 if (curwin->w_p_wrap)
4577 curwin->w_leftcol = 0;
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004578 else
4579 curwin->w_skipcol = 0;
4580
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004581 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004582}
4583
4584/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004585 * Set the value of a boolean option, and take care of side effects.
4586 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004587 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004588 static char *
4589set_bool_option(
4590 int opt_idx, // index in options[] table
4591 char_u *varp, // pointer to the option variable
4592 int value, // new value
4593 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004594{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004595 int old_value = *(int *)varp;
4596#if defined(FEAT_EVAL)
4597 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004599 char *errmsg = NULL;
4600
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004601 // Disallow changing some options from secure mode
4602 if ((secure
4603#ifdef HAVE_SANDBOX
4604 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004605#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004606 ) && (options[opt_idx].flags & P_SECURE))
4607 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004608
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004609#if defined(FEAT_EVAL)
4610 // Save the global value before changing anything. This is needed as for
4611 // a global-only option setting the "local value" in fact sets the global
4612 // value (since there is only one value).
4613 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4614 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4615 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004617
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004618 *(int *)varp = value; // set the new value
4619#ifdef FEAT_EVAL
4620 // Remember where the option was set.
4621 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004622#endif
4623
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004625 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004628 // May set global value for local option.
4629 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4630 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004631
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004632 // Handle side effects of changing a bool option.
4633 if (options[opt_idx].opt_did_set_cb != NULL)
4634 {
4635 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004636
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004637 CLEAR_FIELD(args);
4638 args.os_varp = varp;
4639 args.os_flags = opt_flags;
4640 args.os_oldval.boolean = old_value;
4641 args.os_newval.boolean = value;
4642 args.os_errbuf = NULL;
4643 errmsg = options[opt_idx].opt_did_set_cb(&args);
zeertzjqdeba02d2023-11-02 21:01:19 +01004644 if (errmsg != NULL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004645 return errmsg;
4646 }
4647
4648 // after handling side effects, call autocommand
4649
4650 options[opt_idx].flags |= P_WAS_SET;
4651
4652#if defined(FEAT_EVAL)
4653 apply_optionset_autocmd(opt_idx, opt_flags,
4654 (long)(old_value ? TRUE : FALSE),
4655 (long)(old_global_value ? TRUE : FALSE),
4656 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004657#endif
4658
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004659 comp_col(); // in case 'ruler' or 'showcmd' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01004660
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004661 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004662 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
4663 && (options[opt_idx].flags & P_HLONLY) == 0)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004664 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004665
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004666 if ((opt_flags & OPT_NO_REDRAW) == 0)
4667 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004668
4669 return errmsg;
4670}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004671
4672/*
4673 * Check the bounds of numeric options.
4674 */
4675 static char *
4676check_num_option_bounds(
4677 long *pp,
4678 long old_value,
4679 long old_Rows,
4680 long old_Columns,
4681 char *errbuf,
4682 size_t errbuflen,
4683 char *errmsg)
4684{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (Rows < min_rows() && full_screen)
4686 {
4687 if (errbuf != NULL)
4688 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004689 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004690 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 errmsg = errbuf;
4692 }
4693 Rows = min_rows();
4694 }
4695 if (Columns < MIN_COLUMNS && full_screen)
4696 {
4697 if (errbuf != NULL)
4698 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004699 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004700 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 errmsg = errbuf;
4702 }
4703 Columns = MIN_COLUMNS;
4704 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004705 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004707 // If the screen (shell) height has been changed, assume it is the
4708 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 if (old_Rows != Rows || old_Columns != Columns)
4710 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004711 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 if (updating_screen)
4713 *pp = old_value;
4714 else if (full_screen
4715#ifdef FEAT_GUI
4716 && !gui.starting
4717#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004718 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 set_shellsize((int)Columns, (int)Rows, TRUE);
4720 else
4721 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004722 // Postpone the resizing; check the size and cmdline position for
4723 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 check_shellsize();
4725 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4726 cmdline_row = Rows - p_ch;
4727 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004728 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004729 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 if (curbuf->b_p_ts <= 0)
4733 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004734 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 curbuf->b_p_ts = 8;
4736 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004737 else if (curbuf->b_p_ts > TABSTOP_MAX)
4738 {
4739 errmsg = e_invalid_argument;
4740 curbuf->b_p_ts = 8;
4741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 if (p_tm < 0)
4743 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004744 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 p_tm = 0;
4746 }
4747 if ((curwin->w_p_scr <= 0
4748 || (curwin->w_p_scr > curwin->w_height
4749 && curwin->w_height > 0))
4750 && full_screen)
4751 {
4752 if (pp == &(curwin->w_p_scr))
4753 {
4754 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004755 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 win_comp_scroll(curwin);
4757 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004758 // If 'scroll' became invalid because of a side effect silently adjust
4759 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 else if (curwin->w_p_scr <= 0)
4761 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004762 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 curwin->w_p_scr = curwin->w_height;
4764 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004765 if (p_hi < 0)
4766 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004767 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004768 p_hi = 0;
4769 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004770 else if (p_hi > 10000)
4771 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004772 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004773 p_hi = 10000;
4774 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004775 if (p_re < 0 || p_re > 2)
4776 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004777 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004778 p_re = 0;
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 if (p_report < 0)
4781 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004782 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 p_report = 1;
4784 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004785 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004787 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 p_sj = Rows / 2;
4789 else
4790 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004791 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 p_sj = 1;
4793 }
4794 }
4795 if (p_so < 0 && full_screen)
4796 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004797 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 p_so = 0;
4799 }
4800 if (p_siso < 0 && full_screen)
4801 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004802 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 p_siso = 0;
4804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (p_cwh < 1)
4806 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004807 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 p_cwh = 1;
4809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 if (p_ut < 0)
4811 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004812 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 p_ut = 2000;
4814 }
4815 if (p_ss < 0)
4816 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004817 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 p_ss = 0;
4819 }
4820
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004821 return errmsg;
4822}
4823
4824/*
4825 * Set the value of a number option, and take care of side effects.
4826 * Returns NULL for success, or an error message for an error.
4827 */
4828 static char *
4829set_num_option(
4830 int opt_idx, // index in options[] table
4831 char_u *varp, // pointer to the option variable
4832 long value, // new value
4833 char *errbuf, // buffer for error messages
4834 size_t errbuflen, // length of "errbuf"
4835 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4836 // OPT_MODELINE, etc.
4837{
4838 char *errmsg = NULL;
4839 long old_value = *(long *)varp;
4840#if defined(FEAT_EVAL)
4841 long old_global_value = 0; // only used when setting a local and
4842 // global option
4843#endif
4844 long old_Rows = Rows; // remember old Rows
4845 long old_Columns = Columns; // remember old Columns
4846 long *pp = (long *)varp;
4847
4848 // Disallow changing some options from secure mode.
4849 if ((secure
4850#ifdef HAVE_SANDBOX
4851 || sandbox != 0
4852#endif
4853 ) && (options[opt_idx].flags & P_SECURE))
4854 return e_not_allowed_here;
4855
4856#if defined(FEAT_EVAL)
4857 // Save the global value before changing anything. This is needed as for
4858 // a global-only option setting the "local value" in fact sets the global
4859 // value (since there is only one value).
4860 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4861 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4862 OPT_GLOBAL);
4863#endif
4864
4865 *pp = value;
4866#ifdef FEAT_EVAL
4867 // Remember where the option was set.
4868 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4869#endif
4870#ifdef FEAT_GUI
4871 need_mouse_correct = TRUE;
4872#endif
4873
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004874 // Invoke the option specific callback function to validate and apply the
4875 // new value.
4876 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004877 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004878 optset_T args;
4879
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004880 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004881 args.os_varp = varp;
4882 args.os_flags = opt_flags;
4883 args.os_oldval.number = old_value;
4884 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004885 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004886 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004887 }
4888
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004889 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004890 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4891 errbuf, errbuflen, errmsg);
4892
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004893 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4895 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4896
4897 options[opt_idx].flags |= P_WAS_SET;
4898
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004899#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004900 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4901 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004902#endif
4903
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004904 comp_col(); // in case 'columns' or 'ls' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01004905
Bram Moolenaar913077c2012-03-28 19:59:04 +02004906 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004907 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
4908 && (options[opt_idx].flags & P_HLONLY) == 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004909 curwin->w_set_curswant = TRUE;
zeertzjqfcaed6a2024-02-18 09:33:54 +01004910
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004911 if ((opt_flags & OPT_NO_REDRAW) == 0)
4912 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
4914 return errmsg;
4915}
4916
4917/*
4918 * Called after an option changed: check if something needs to be redrawn.
4919 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004920 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004921check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004923 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004924 int doclear = (flags & P_RCLR) == P_RCLR;
4925 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004927 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
4930 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
zeertzjqfcaed6a2024-02-18 09:33:54 +01004931 {
4932 if (flags & P_HLONLY)
4933 redraw_later(UPD_NOT_VALID);
4934 else
4935 changed_window_setting();
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004938 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004939 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004940 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004942 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943}
4944
4945/*
4946 * Find index for option 'arg'.
4947 * Return -1 if not found.
4948 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004949 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004950findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
4952 int opt_idx;
4953 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004954 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 int is_term_opt;
4956
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004957 // For first call: Initialize the quick-access table.
4958 // It contains the index for the first option that starts with a certain
4959 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 if (quick_tab[1] == 0)
4961 {
4962 p = options[0].fullname;
4963 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4964 {
4965 if (s[0] != p[0])
4966 {
4967 if (s[0] == 't' && s[1] == '_')
4968 quick_tab[26] = opt_idx;
4969 else
4970 quick_tab[CharOrdLow(s[0])] = opt_idx;
4971 }
4972 p = s;
4973 }
4974 }
4975
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004976 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 return -1;
4979
4980 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4981 if (is_term_opt)
4982 opt_idx = quick_tab[26];
4983 else
4984 opt_idx = quick_tab[CharOrdLow(arg[0])];
zeertzjqf48558e2023-12-08 21:34:31 +01004985 for (; (s = options[opt_idx].fullname) != NULL && s[0] == arg[0]; opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004987 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 break;
4989 }
zeertzjqf48558e2023-12-08 21:34:31 +01004990 if (s != NULL && s[0] != arg[0])
4991 s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (s == NULL && !is_term_opt)
4993 {
4994 opt_idx = quick_tab[CharOrdLow(arg[0])];
4995 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4996 {
4997 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004998 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 break;
5000 s = NULL;
5001 }
5002 }
5003 if (s == NULL)
5004 opt_idx = -1;
5005 return opt_idx;
5006}
5007
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005008#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
5009 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010/*
5011 * Get the value for an option.
5012 *
5013 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005014 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01005015 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005016 * String option: gov_string, *stringval gets allocated string.
5017 * Hidden Number option: gov_hidden_number.
5018 * Hidden Toggle option: gov_hidden_bool.
5019 * Hidden String option: gov_hidden_string.
5020 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005021 *
5022 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005024 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01005025get_option_value(
5026 char_u *name,
5027 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005028 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005029 int *flagsp,
5030 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031{
5032 int opt_idx;
5033 char_u *varp;
5034
5035 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005036 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01005037 {
5038 int key;
5039
5040 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005041 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005042 {
5043 char_u key_name[2];
5044 char_u *p;
5045
Bram Moolenaar10c75c42021-12-28 20:53:30 +00005046 if (flagsp != NULL)
5047 *flagsp = 0; // terminal option has no flags
5048
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005049 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01005050 if (key < 0)
5051 {
5052 key_name[0] = KEY2TERMCAP0(key);
5053 key_name[1] = KEY2TERMCAP1(key);
5054 }
5055 else
5056 {
5057 key_name[0] = KS_KEY;
5058 key_name[1] = (key & 0xff);
5059 }
5060 p = find_termcode(key_name);
5061 if (p != NULL)
5062 {
5063 if (stringval != NULL)
5064 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005065 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01005066 }
5067 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005068 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01005069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005071 varp = get_varp_scope(&(options[opt_idx]), scope);
5072
5073 if (flagsp != NULL)
5074 // Return the P_xxxx option flags.
5075 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076
5077 if (options[opt_idx].flags & P_STRING)
5078 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005079 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005080 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 if (stringval != NULL)
5082 {
zeertzjq51f0bc32022-05-09 13:33:39 +01005083 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005084 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5085 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005087 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005088 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005089 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005092 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 *stringval = vim_strsave(*(char_u **)(varp));
5094 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005095 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005098 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005099 return (options[opt_idx].flags & P_NUM)
5100 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if (options[opt_idx].flags & P_NUM)
5102 *numval = *(long *)varp;
5103 else
5104 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005105 // Special case: 'modified' is b_changed, but we also want to consider
5106 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if ((int *)varp == &curbuf->b_changed)
5108 *numval = curbufIsChanged();
5109 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005110 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005112 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113}
5114#endif
5115
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005116#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005117/*
5118 * Returns the option attributes and its value. Unlike the above function it
5119 * will return either global value or local value of the option depending on
5120 * what was requested, but it will never return global value if it was
5121 * requested to return local one and vice versa. Neither it will return
5122 * buffer-local value if it was requested to return window-local one.
5123 *
5124 * Pretends that option is absent if it is not present in the requested scope
5125 * (i.e. has no global, window-local or buffer-local value depending on
5126 * opt_type). Uses
5127 *
5128 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005129 * 0 hidden or unknown option, also option that does not have requested
5130 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005131 * see SOPT_* in vim.h for other flags
5132 *
5133 * Possible opt_type values: see SREQ_* in vim.h
5134 */
5135 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005136get_option_value_strict(
5137 char_u *name,
5138 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005139 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005140 int opt_type,
5141 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005142{
5143 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005144 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005145 struct vimoption *p;
5146 int r = 0;
5147
5148 opt_idx = findoption(name);
5149 if (opt_idx < 0)
5150 return 0;
5151
5152 p = &(options[opt_idx]);
5153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005154 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005155 if (p->var == NULL)
5156 return 0;
5157
5158 if (p->flags & P_BOOL)
5159 r |= SOPT_BOOL;
5160 else if (p->flags & P_NUM)
5161 r |= SOPT_NUM;
5162 else if (p->flags & P_STRING)
5163 r |= SOPT_STRING;
5164
5165 if (p->indir == PV_NONE)
5166 {
5167 if (opt_type == SREQ_GLOBAL)
5168 r |= SOPT_GLOBAL;
5169 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005170 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005171 }
5172 else
5173 {
5174 if (p->indir & PV_BOTH)
5175 r |= SOPT_GLOBAL;
5176 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005177 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005178
5179 if (p->indir & PV_WIN)
5180 {
5181 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005182 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005183 else
5184 r |= SOPT_WIN;
5185 }
5186 else if (p->indir & PV_BUF)
5187 {
5188 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005189 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005190 else
5191 r |= SOPT_BUF;
5192 }
5193 }
5194
5195 if (stringval == NULL)
5196 return r;
5197
5198 if (opt_type == SREQ_GLOBAL)
5199 varp = p->var;
5200 else
5201 {
5202 if (opt_type == SREQ_BUF)
5203 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005204 // Special case: 'modified' is b_changed, but we also want to
5205 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005206 if (p->indir == PV_MOD)
5207 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005208 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005209 varp = NULL;
5210 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005211# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005212 else if (p->indir == PV_KEY)
5213 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005214 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005215 *stringval = NULL;
5216 varp = NULL;
5217 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005218# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005219 else
5220 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005221 buf_T *save_curbuf = curbuf;
5222
5223 // only getting a pointer, no need to use aucmd_prepbuf()
5224 curbuf = (buf_T *)from;
5225 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005226 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005227 curbuf = save_curbuf;
5228 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005229 }
5230 }
5231 else if (opt_type == SREQ_WIN)
5232 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005233 win_T *save_curwin = curwin;
5234
5235 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005236 curbuf = curwin->w_buffer;
5237 varp = get_varp(p);
5238 curwin = save_curwin;
5239 curbuf = curwin->w_buffer;
5240 }
5241 if (varp == p->var)
5242 return (r | SOPT_UNSET);
5243 }
5244
5245 if (varp != NULL)
5246 {
5247 if (p->flags & P_STRING)
5248 *stringval = vim_strsave(*(char_u **)(varp));
5249 else if (p->flags & P_NUM)
5250 *numval = *(long *) varp;
5251 else
5252 *numval = *(int *)varp;
5253 }
5254
5255 return r;
5256}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005257
5258/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005259 * Iterate over options. First argument is a pointer to a pointer to a
5260 * structure inside options[] array, second is option type like in the above
5261 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005262 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005263 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005264 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005265 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005266 * first argument to NULL.
5267 *
5268 * Returns full option name for current option on each call.
5269 */
5270 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005271option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005272{
5273 struct vimoption *ret = NULL;
5274 do
5275 {
5276 if (*option == NULL)
5277 *option = (void *) options;
5278 else if (((struct vimoption *) (*option))->fullname == NULL)
5279 {
5280 *option = NULL;
5281 return NULL;
5282 }
5283 else
5284 *option = (void *) (((struct vimoption *) (*option)) + 1);
5285
5286 ret = ((struct vimoption *) (*option));
5287
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005288 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005289 if (ret->var == NULL)
5290 {
5291 ret = NULL;
5292 continue;
5293 }
5294
5295 switch (opt_type)
5296 {
5297 case SREQ_GLOBAL:
5298 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5299 ret = NULL;
5300 break;
5301 case SREQ_BUF:
5302 if (!(ret->indir & PV_BUF))
5303 ret = NULL;
5304 break;
5305 case SREQ_WIN:
5306 if (!(ret->indir & PV_WIN))
5307 ret = NULL;
5308 break;
5309 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005310 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005311 return NULL;
5312 }
5313 }
5314 while (ret == NULL);
5315
5316 return (char_u *)ret->fullname;
5317}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005318#endif
5319
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005321 * Return the flags for the option at 'opt_idx'.
5322 */
5323 long_u
5324get_option_flags(int opt_idx)
5325{
5326 return options[opt_idx].flags;
5327}
5328
5329/*
5330 * Set a flag for the option at 'opt_idx'.
5331 */
5332 void
5333set_option_flag(int opt_idx, long_u flag)
5334{
5335 options[opt_idx].flags |= flag;
5336}
5337
5338/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005339 * Returns TRUE if the option at 'opt_idx' is a global option
5340 */
5341 int
5342is_global_option(int opt_idx)
5343{
5344 return options[opt_idx].indir == PV_NONE;
5345}
5346
5347/*
5348 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5349 * local value.
5350 */
5351 int
5352is_global_local_option(int opt_idx)
5353{
5354 return options[opt_idx].indir & PV_BOTH;
5355}
5356
5357/*
5358 * Returns TRUE if the option at 'opt_idx' is a window-local option
5359 */
5360 int
5361is_window_local_option(int opt_idx)
5362{
5363 return options[opt_idx].var == VAR_WIN;
5364}
5365
5366/*
5367 * Returns TRUE if the option at 'opt_idx' is a hidden option
5368 */
5369 int
5370is_hidden_option(int opt_idx)
5371{
5372 return options[opt_idx].var == NULL;
5373}
5374
5375#if defined(FEAT_CRYPT) || defined(PROTO)
5376/*
5377 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5378 */
5379 int
5380is_crypt_key_option(int opt_idx)
5381{
5382 return options[opt_idx].indir == PV_KEY;
5383}
5384#endif
5385
5386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 * Set the value of option "name".
5388 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005389 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005390 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005392 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005393set_option_value(
5394 char_u *name,
5395 long number,
5396 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005397 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 int opt_idx;
5400 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005401 long_u flags;
Christian Brabandtb39b2402023-11-29 11:34:05 +01005402 static char errbuf[ERR_BUFLEN];
5403 int errbuflen = ERR_BUFLEN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404
5405 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005406 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005407 {
5408 int key;
5409
5410 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005411 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005412 {
5413 char_u key_name[2];
5414
5415 if (key < 0)
5416 {
5417 key_name[0] = KEY2TERMCAP0(key);
5418 key_name[1] = KEY2TERMCAP1(key);
5419 }
5420 else
5421 {
5422 key_name[0] = KS_KEY;
5423 key_name[1] = (key & 0xff);
5424 }
5425 add_termcode(key_name, string, FALSE);
5426 if (full_screen)
5427 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005428 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005429 return NULL;
5430 }
5431
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005432 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 else
5435 {
5436 flags = options[opt_idx].flags;
5437#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005438 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005440 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005441 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005442 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005445 if (flags & P_STRING)
Christian Brabandtb39b2402023-11-29 11:34:05 +01005446 return set_string_option(opt_idx, string, opt_flags, errbuf, errbuflen);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005447
5448 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5449 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005451 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005453 int idx;
5454
5455 // Either we are given a string or we are setting option
5456 // to zero.
5457 for (idx = 0; string[idx] == '0'; ++idx)
5458 ;
5459 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005460 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005461 // There's another character after zeros or the string
5462 // is empty. In both cases, we are trying to set a
5463 // num option using a string.
5464 semsg(_(e_number_required_after_str_equal_str),
5465 name, string);
5466 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005467
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005470 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005471 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005472 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005473 errbuf, sizeof(errbuf), opt_flags);
5474 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005475 else
5476 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005478
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005480 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481}
5482
5483/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005484 * Call set_option_value() and when an error is returned report it.
5485 */
5486 void
5487set_option_value_give_err(
5488 char_u *name,
5489 long number,
5490 char_u *string,
5491 int opt_flags) // OPT_LOCAL or 0 (both)
5492{
5493 char *errmsg = set_option_value(name, number, string, opt_flags);
5494
5495 if (errmsg != NULL)
5496 emsg(_(errmsg));
5497}
5498
5499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 * Get the terminal code for a terminal option.
5501 * Returns NULL when not found.
5502 */
5503 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005504get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506 int opt_idx;
5507 char_u *varp;
5508
5509 if (tname[0] != 't' || tname[1] != '_' ||
5510 tname[2] == NUL || tname[3] == NUL)
5511 return NULL;
5512 if ((opt_idx = findoption(tname)) >= 0)
5513 {
5514 varp = get_varp(&(options[opt_idx]));
5515 if (varp != NULL)
5516 varp = *(char_u **)(varp);
5517 return varp;
5518 }
5519 return find_termcode(tname + 2);
5520}
5521
5522 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005523get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
5525 int i;
5526
5527 i = findoption((char_u *)"hl");
5528 if (i >= 0)
5529 return options[i].def_val[VI_DEFAULT];
5530 return (char_u *)NULL;
5531}
5532
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005533 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005534get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005535{
5536 int i;
5537
5538 i = findoption((char_u *)"enc");
5539 if (i >= 0)
5540 return options[i].def_val[VI_DEFAULT];
5541 return (char_u *)NULL;
5542}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005543
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005544#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005545 int
5546is_option_allocated(char *name)
5547{
5548 int idx = findoption((char_u *)name);
5549
5550 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5551}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005552#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554/*
5555 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005556 * When "has_lt" is true there is a '<' before "*arg_arg".
5557 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 */
5559 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005560find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005562 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005564 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005566 // Don't use get_special_key_code() for t_xx, we don't want it to call
5567 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5569 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005570 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005572 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005574 key = find_special_key(&arg, &modifiers,
5575 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005576 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 key = 0;
5578 }
5579 return key;
5580}
5581
5582/*
5583 * if 'all' == 0: show changed options
5584 * if 'all' == 1: show all normal options
5585 * if 'all' == 2: show all terminal options
5586 */
5587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005588showoptions(
5589 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005590 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 struct vimoption *p;
5593 int col;
5594 int isterm;
5595 char_u *varp;
5596 struct vimoption **items;
5597 int item_count;
5598 int run;
5599 int row, rows;
5600 int cols;
5601 int i;
5602 int len;
5603
5604#define INC 20
5605#define GAP 3
5606
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005607 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 if (items == NULL)
5609 return;
5610
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005611 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005613 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005615 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005617 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005619 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005621 // Do the loop two times:
5622 // 1. display the short items
5623 // 2. display the long items (only strings and numbers)
5624 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 for (run = 1; run <= 2 && !got_int; ++run)
5626 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005627 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 item_count = 0;
5629 for (p = &options[0]; p->fullname != NULL; p++)
5630 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005631 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005632 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005633 continue;
5634
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 varp = NULL;
5636 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005637 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
5639 if (p->indir != PV_NONE && !isterm)
5640 varp = get_varp_scope(p, opt_flags);
5641 }
5642 else
5643 varp = get_varp(p);
5644 if (varp != NULL
5645 && ((all == 2 && isterm)
5646 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005647 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005649 if (opt_flags & OPT_ONECOLUMN)
5650 len = Columns;
5651 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005652 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 else
5654 {
5655 option_value2string(p, opt_flags);
5656 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5657 }
5658 if ((len <= INC - GAP && run == 1) ||
5659 (len > INC - GAP && run == 2))
5660 items[item_count++] = p;
5661 }
5662 }
5663
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005664 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 if (run == 1)
5666 {
5667 cols = (Columns + GAP - 3) / INC;
5668 if (cols == 0)
5669 cols = 1;
5670 rows = (item_count + cols - 1) / cols;
5671 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005672 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 rows = item_count;
5674 for (row = 0; row < rows && !got_int; ++row)
5675 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005676 msg_putchar('\n'); // go to next line
5677 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 break;
5679 col = 0;
5680 for (i = row; i < item_count; i += rows)
5681 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005682 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 showoneopt(items[i], opt_flags);
5684 col += INC;
5685 }
5686 out_flush();
5687 ui_breakcheck();
5688 }
5689 }
5690 vim_free(items);
5691}
5692
5693/*
5694 * Return TRUE if option "p" has its default value.
5695 */
5696 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005697optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698{
5699 int dvi;
5700
5701 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005702 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005703 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005705 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005707 // the cast to long is required for Manx C, long_i is
5708 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005709 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005710 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5712}
5713
5714/*
5715 * showoneopt: show the value of one option
5716 * must not be called with a hidden option!
5717 */
5718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005719showoneopt(
5720 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005721 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005723 char_u *varp;
5724 int save_silent = silent_mode;
5725
5726 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005727 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
5729 varp = get_varp_scope(p, opt_flags);
5730
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005731 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5733 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005734 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005736 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005738 msg_puts(" ");
5739 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 if (!(p->flags & P_BOOL))
5741 {
5742 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005743 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 option_value2string(p, opt_flags);
5745 msg_outtrans(NameBuff);
5746 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005747
5748 silent_mode = save_silent;
5749 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750}
5751
5752/*
5753 * Write modified options as ":set" commands to a file.
5754 *
5755 * There are three values for "opt_flags":
5756 * OPT_GLOBAL: Write global option values and fresh values of
5757 * buffer-local options (used for start of a session
5758 * file).
5759 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5760 * curwin (used for a vimrc file).
5761 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5762 * and local values for window-local options of
5763 * curwin. Local values are also written when at the
5764 * default value, because a modeline or autocommand
5765 * may have set them when doing ":edit file" and the
5766 * user has set them back at the default or fresh
5767 * value.
5768 * When "local_only" is TRUE, don't write fresh
5769 * values, only local values (for ":mkview").
5770 * (fresh value = value used for a new buffer or window for a local option).
5771 *
5772 * Return FAIL on error, OK otherwise.
5773 */
5774 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005775makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005778 char_u *varp; // currently used value
5779 char_u *varp_fresh; // local value
5780 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 char *cmd;
5782 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005783 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785 /*
5786 * The options that don't have a default (terminal name, columns, lines)
5787 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005788 * Do the loop over "options[]" twice: once for options with the
5789 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005791 for (pri = 1; pri >= 0; --pri)
5792 {
5793 for (p = &options[0]; !istermoption(p); p++)
5794 if (!(p->flags & P_NO_MKRC)
5795 && !istermoption(p)
5796 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005798 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5800 continue;
5801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005802 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5803 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5805 continue;
5806
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005807 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005809 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 continue;
5811
Bram Moolenaard23b7142021-04-17 21:04:34 +02005812 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5813 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005814 continue;
5815
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 round = 2;
5817 if (p->indir != PV_NONE)
5818 {
5819 if (p->var == VAR_WIN)
5820 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005821 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 if (!(opt_flags & OPT_LOCAL))
5823 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005824 // When fresh value of window-local option is not at the
5825 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5827 {
5828 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005829 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 {
5831 round = 1;
5832 varp_local = varp;
5833 varp = varp_fresh;
5834 }
5835 }
5836 }
5837 }
5838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005839 // Round 1: fresh value for window-local options.
5840 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 for ( ; round <= 2; varp = varp_local, ++round)
5842 {
5843 if (round == 1 || (opt_flags & OPT_GLOBAL))
5844 cmd = "set";
5845 else
5846 cmd = "setlocal";
5847
5848 if (p->flags & P_BOOL)
5849 {
5850 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5851 return FAIL;
5852 }
5853 else if (p->flags & P_NUM)
5854 {
5855 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5856 return FAIL;
5857 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005858 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005860 int do_endif = FALSE;
5861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005862 // Don't set 'syntax' and 'filetype' again if the value is
5863 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005864 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005865#if defined(FEAT_SYN_HL)
5866 p->indir == PV_SYN ||
5867#endif
5868 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 {
5870 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5871 *(char_u **)(varp)) < 0
5872 || put_eol(fd) < 0)
5873 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005874 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 }
5876 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005877 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005879 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
5881 if (put_line(fd, "endif") == FAIL)
5882 return FAIL;
5883 }
5884 }
5885 }
5886 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 return OK;
5889}
5890
5891#if defined(FEAT_FOLDING) || defined(PROTO)
5892/*
5893 * Generate set commands for the local fold options only. Used when
5894 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5895 */
5896 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005897makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005899 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005901 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 == FAIL
5903# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005904 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005906 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 == FAIL
5908 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5909 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5910 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5911 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5912 )
5913 return FAIL;
5914
5915 return OK;
5916}
5917#endif
5918
5919 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005920put_setstring(
5921 FILE *fd,
5922 char *cmd,
5923 char *name,
5924 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005925 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
5927 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005928 char_u *buf = NULL;
5929 char_u *part = NULL;
5930 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
5932 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5933 return FAIL;
5934 if (*valuep != NULL)
5935 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005936 // Output 'pastetoggle' as key names. For other
5937 // options some characters have to be escaped with
5938 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 if (valuep == &p_pt)
5940 {
5941 s = *valuep;
5942 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005943 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 return FAIL;
5945 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005946 // expand the option value, replace $HOME by ~
5947 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005949 int size = (int)STRLEN(*valuep) + 1;
5950
5951 // replace home directory in the whole option value into "buf"
5952 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005953 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005954 goto fail;
5955 home_replace(NULL, *valuep, buf, size, FALSE);
5956
5957 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005958 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005959 // can be expanded when read back.
5960 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5961 && vim_strchr(*valuep, ',') != NULL)
5962 {
5963 part = alloc(size);
5964 if (part == NULL)
5965 goto fail;
5966
5967 // write line break to clear the option, e.g. ':set rtp='
5968 if (put_eol(fd) == FAIL)
5969 goto fail;
5970
5971 p = buf;
5972 while (*p != NUL)
5973 {
5974 // for each comma separated option part, append value to
5975 // the option, :set rtp+=value
5976 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5977 goto fail;
5978 (void)copy_option_part(&p, part, size, ",");
5979 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5980 goto fail;
5981 }
5982 vim_free(buf);
5983 vim_free(part);
5984 return OK;
5985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005987 {
5988 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005990 }
5991 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 }
5993 else if (put_escstr(fd, *valuep, 2) == FAIL)
5994 return FAIL;
5995 }
5996 if (put_eol(fd) < 0)
5997 return FAIL;
5998 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005999fail:
6000 vim_free(buf);
6001 vim_free(part);
6002 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003}
6004
6005 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006006put_setnum(
6007 FILE *fd,
6008 char *cmd,
6009 char *name,
6010 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011{
6012 long wc;
6013
6014 if (fprintf(fd, "%s %s=", cmd, name) < 0)
6015 return FAIL;
6016 if (wc_use_keyname((char_u *)valuep, &wc))
6017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006018 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
6020 return FAIL;
6021 }
6022 else if (fprintf(fd, "%ld", *valuep) < 0)
6023 return FAIL;
6024 if (put_eol(fd) < 0)
6025 return FAIL;
6026 return OK;
6027}
6028
6029 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006030put_setbool(
6031 FILE *fd,
6032 char *cmd,
6033 char *name,
6034 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006036 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00006037 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
6039 || put_eol(fd) < 0)
6040 return FAIL;
6041 return OK;
6042}
6043
6044/*
6045 * Clear all the terminal options.
6046 * If the option has been allocated, free the memory.
6047 * Terminal options are never hidden or indirect.
6048 */
6049 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006050clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006052 // Reset a few things before clearing the old options. This may cause
6053 // outputting a few things that the terminal doesn't understand, but the
6054 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006055 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006056 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006058 // When starting the GUI close the display opened for the clipboard.
6059 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 if (gui.starting)
6061 clear_xterm_clip();
6062#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006063 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006065 free_termoptions();
6066}
6067
6068 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006069free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006070{
6071 struct vimoption *p;
6072
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006073 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 if (istermoption(p))
6075 {
6076 if (p->flags & P_ALLOCED)
6077 free_string_option(*(char_u **)(p->var));
6078 if (p->flags & P_DEF_ALLOCED)
6079 free_string_option(p->def_val[VI_DEFAULT]);
6080 *(char_u **)(p->var) = empty_option;
6081 p->def_val[VI_DEFAULT] = empty_option;
6082 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006083#ifdef FEAT_EVAL
6084 // remember where the option was cleared
6085 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 }
6088 clear_termcodes();
6089}
6090
6091/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006092 * Free the string for one term option, if it was allocated.
6093 * Set the string to empty_option and clear allocated flag.
6094 * "var" points to the option value.
6095 */
6096 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006097free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006098{
6099 struct vimoption *p;
6100
6101 for (p = &options[0]; p->fullname != NULL; p++)
6102 if (p->var == var)
6103 {
6104 if (p->flags & P_ALLOCED)
6105 free_string_option(*(char_u **)(p->var));
6106 *(char_u **)(p->var) = empty_option;
6107 p->flags &= ~P_ALLOCED;
6108 break;
6109 }
6110}
6111
6112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 * Set the terminal option defaults to the current value.
6114 * Used after setting the terminal name.
6115 */
6116 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006117set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118{
6119 struct vimoption *p;
6120
6121 for (p = &options[0]; p->fullname != NULL; p++)
6122 {
6123 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6124 {
6125 if (p->flags & P_DEF_ALLOCED)
6126 {
6127 free_string_option(p->def_val[VI_DEFAULT]);
6128 p->flags &= ~P_DEF_ALLOCED;
6129 }
6130 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6131 if (p->flags & P_ALLOCED)
6132 {
6133 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006134 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 }
6136 }
6137 }
6138}
6139
6140/*
6141 * return TRUE if 'p' starts with 't_'
6142 */
6143 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006144istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145{
6146 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6147}
6148
Bram Moolenaardac13472019-09-16 21:06:21 +02006149/*
6150 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6151 */
6152 int
6153istermoption_idx(int opt_idx)
6154{
6155 return istermoption(&options[opt_idx]);
6156}
6157
Bram Moolenaar113e1072019-01-20 15:30:40 +01006158#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006160 * Unset local option value, similar to ":set opt<".
6161 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006162 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006163unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006164{
6165 struct vimoption *p;
6166 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006167 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006168
6169 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006170 if (opt_idx < 0)
6171 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006172 p = &(options[opt_idx]);
6173
6174 switch ((int)p->indir)
6175 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006176 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006177 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006178 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006179 break;
6180 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006181 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006182 break;
6183 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006184 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006185 break;
6186 case PV_AR:
6187 buf->b_p_ar = -1;
6188 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006189 case PV_BKC:
6190 clear_string_option(&buf->b_p_bkc);
6191 buf->b_bkc_flags = 0;
6192 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006193 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006194 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006195 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006196 case PV_TC:
6197 clear_string_option(&buf->b_p_tc);
6198 buf->b_tc_flags = 0;
6199 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006200 case PV_SISO:
6201 curwin->w_p_siso = -1;
6202 break;
6203 case PV_SO:
6204 curwin->w_p_so = -1;
6205 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006206# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006207 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006208 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006209 break;
6210 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006211 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006212 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006213# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006214 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006215 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006216 break;
6217 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006218 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006219 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006220# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006221 case PV_TSRFU:
6222 clear_string_option(&buf->b_p_tsrfu);
6223 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006224# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006225 case PV_FP:
6226 clear_string_option(&buf->b_p_fp);
6227 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006228# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006229 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006230 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006231 break;
6232 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006233 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006234 break;
6235 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006236 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006237 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006238# endif
6239# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006240 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006241 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006242 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006243# endif
6244# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006245 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006246 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006247 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006248# endif
6249# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006250 case PV_SBR:
6251 clear_string_option(&((win_T *)from)->w_p_sbr);
6252 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006253# endif
6254# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006255 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006256 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006257 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006258# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006259 case PV_UL:
6260 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6261 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006262 case PV_LW:
6263 clear_string_option(&buf->b_p_lw);
6264 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006265 case PV_MENC:
6266 clear_string_option(&buf->b_p_menc);
6267 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006268 case PV_LCS:
6269 clear_string_option(&((win_T *)from)->w_p_lcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006270 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE,
6271 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006272 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006273 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006274 case PV_FCS:
6275 clear_string_option(&((win_T *)from)->w_p_fcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006276 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE,
6277 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006278 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006279 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006280 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006281 clear_string_option(&((win_T *)from)->w_p_ve);
6282 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006283 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006284 }
6285}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006286#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006287
6288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006290 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 */
6292 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006293get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006295 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 {
6297 if (p->var == VAR_WIN)
6298 return (char_u *)GLOBAL_WO(get_varp(p));
6299 return p->var;
6300 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006301 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 {
6303 switch ((int)p->indir)
6304 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006305 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006307 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6308 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6309 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006311 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6312 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6313 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006314 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006315 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006316 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006317 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6318 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006320 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6321 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006323 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6324 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006325#ifdef FEAT_COMPL_FUNC
6326 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6327#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006328#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6329 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6330#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006331#if defined(FEAT_CRYPT)
6332 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6333#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006334#ifdef FEAT_LINEBREAK
6335 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6336#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006337#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006338 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006339#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006340 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006341 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006342 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006343 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006344 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006345 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006346 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006347
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006349 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 }
6351 return get_varp(p);
6352}
6353
6354/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006355 * Get pointer to option variable at 'opt_idx', depending on local or global
6356 * scope.
6357 */
6358 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006359get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006360{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006361 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006362}
6363
6364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 * Get pointer to option variable.
6366 */
6367 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006368get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006370 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 if (p->var == NULL)
6372 return NULL;
6373
6374 switch ((int)p->indir)
6375 {
6376 case PV_NONE: return p->var;
6377
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006378 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006379 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006381 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006383 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006385 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006387 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006389 case PV_TC: return *curbuf->b_p_tc != NUL
6390 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006391 case PV_BKC: return *curbuf->b_p_bkc != NUL
6392 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006393 case PV_SISO: return curwin->w_p_siso >= 0
6394 ? (char_u *)&(curwin->w_p_siso) : p->var;
6395 case PV_SO: return curwin->w_p_so >= 0
6396 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006398 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006400 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6402#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006403 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006405 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006407#ifdef FEAT_COMPL_FUNC
6408 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6409 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6410#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006411 case PV_FP: return *curbuf->b_p_fp != NUL
6412 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006414 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006416 case PV_GP: return *curbuf->b_p_gp != NUL
6417 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6418 case PV_MP: return *curbuf->b_p_mp != NUL
6419 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006421#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6422 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6423 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6424#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006425#if defined(FEAT_CRYPT)
6426 case PV_CM: return *curbuf->b_p_cm != NUL
6427 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6428#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006429#ifdef FEAT_LINEBREAK
6430 case PV_SBR: return *curwin->w_p_sbr != NUL
6431 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6432#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006433#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006434 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006435 ? (char_u *)&(curwin->w_p_stl) : p->var;
6436#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006437 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6438 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006439 case PV_LW: return *curbuf->b_p_lw != NUL
6440 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006441 case PV_MENC: return *curbuf->b_p_menc != NUL
6442 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443#ifdef FEAT_ARABIC
6444 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6445#endif
6446 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006447 case PV_LCS: return *curwin->w_p_lcs != NUL
6448 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006449 case PV_FCS: return *curwin->w_p_fcs != NUL
6450 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006451 case PV_VE: return *curwin->w_p_ve != NUL
6452 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006453#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006454 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6455#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006456#ifdef FEAT_SYN_HL
6457 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6458 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006459 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006460 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462#ifdef FEAT_DIFF
6463 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6464#endif
6465#ifdef FEAT_FOLDING
6466 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6467 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6468 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6469 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6470 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6471 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6472 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6473# ifdef FEAT_EVAL
6474 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6475 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6476# endif
6477 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6478#endif
6479 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006480 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006481#ifdef FEAT_LINEBREAK
6482 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6483#endif
Colin Kennedy21570352024-03-03 16:16:47 +01006484 case PV_WFB: return (char_u *)&(curwin->w_p_wfb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006486 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006487#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6489#endif
6490#ifdef FEAT_RIGHTLEFT
6491 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6492 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6493#endif
6494 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006495 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6497#ifdef FEAT_LINEBREAK
6498 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006499 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6500 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006502 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006504 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006505#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006506 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6507 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6508#endif
6509#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006510 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6511 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6512 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514
6515 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6516 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6519 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6521 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6523 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6524 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006525 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528#ifdef FEAT_FOLDING
6529 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006532#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006533 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006535#ifdef FEAT_COMPL_FUNC
6536 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006537 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006538#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006539#ifdef FEAT_EVAL
6540 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6541#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006542 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006544 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006550 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6552 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6553 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6554 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6555#ifdef FEAT_FIND_ID
6556# ifdef FEAT_EVAL
6557 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6558# endif
6559#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006560#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6562 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6563#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006564#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006565 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567#ifdef FEAT_CRYPT
6568 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006571 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6573 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6574 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6575 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6576 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006578 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6585#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006586 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006587 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6588#endif
6589#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006590 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6591 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6592 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006593 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594#endif
6595 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6596 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6597 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6598 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006599#ifdef FEAT_PERSISTENT_UNDO
6600 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6603#ifdef FEAT_KEYMAP
6604 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6605#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006606#ifdef FEAT_SIGNS
6607 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6608#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006609#ifdef FEAT_VARTABS
6610 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6611 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6612#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006613 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006615 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 return (char_u *)&(curbuf->b_p_wm);
6617}
6618
6619/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006620 * Return a pointer to the variable for option at 'opt_idx'
6621 */
6622 char_u *
6623get_option_var(int opt_idx)
6624{
6625 return options[opt_idx].var;
6626}
6627
Dominique Pelle748b3082022-01-08 12:41:16 +00006628#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006629/*
6630 * Return the full name of the option at 'opt_idx'
6631 */
6632 char_u *
6633get_option_fullname(int opt_idx)
6634{
6635 return (char_u *)options[opt_idx].fullname;
6636}
Dominique Pelle748b3082022-01-08 12:41:16 +00006637#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006638
6639/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006640 * Return the did_set callback function for the option at 'opt_idx'
6641 */
6642 opt_did_set_cb_T
6643get_option_did_set_cb(int opt_idx)
6644{
6645 return options[opt_idx].opt_did_set_cb;
6646}
6647
6648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 * Get the value of 'equalprg', either the buffer-local one or the global one.
6650 */
6651 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006652get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653{
6654 if (*curbuf->b_p_ep == NUL)
6655 return p_ep;
6656 return curbuf->b_p_ep;
6657}
6658
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659/*
6660 * Copy options from one window to another.
6661 * Used when splitting a window.
6662 */
6663 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006664win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
6666 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6667 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006668 after_copy_winopt(wp_to);
6669}
6670
6671/*
6672 * After copying window options: update variables depending on options.
6673 */
6674 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006675after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006676{
6677#ifdef FEAT_LINEBREAK
6678 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006679#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006680#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006681 fill_culopt_flags(NULL, wp);
6682 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006683#endif
zeertzjq6a8d2e12024-01-17 20:54:49 +01006684 set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
6685 set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006686}
6687
6688 static char_u *
6689copy_option_val(char_u *val)
6690{
6691 if (val == empty_option)
6692 return empty_option; // no need to allocate memory
6693 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
6696/*
6697 * Copy the options from one winopt_T to another.
6698 * Doesn't free the old option values in "to", use clear_winopt() for that.
6699 * The 'scroll' option is not copied, because it depends on the window height.
6700 * The 'previewwindow' option is reset, there can be only one preview window.
6701 */
6702 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006703copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704{
6705#ifdef FEAT_ARABIC
6706 to->wo_arab = from->wo_arab;
6707#endif
6708 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006709 to->wo_lcs = copy_option_val(from->wo_lcs);
6710 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006712 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006713 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006714 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006715#ifdef FEAT_LINEBREAK
6716 to->wo_nuw = from->wo_nuw;
6717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718#ifdef FEAT_RIGHTLEFT
6719 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006720 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006722#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006723 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006724#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006725#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006726 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006729#ifdef FEAT_DIFF
6730 to->wo_wrap_save = from->wo_wrap_save;
6731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732#ifdef FEAT_LINEBREAK
6733 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006734 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006735 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006737 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006739 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006740 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006741 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006742 to->wo_crb_save = from->wo_crb_save;
Christian Brabandt4a8eb6e2023-08-13 19:43:42 +02006743 to->wo_siso = from->wo_siso;
6744 to->wo_so = from->wo_so;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006745#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006746 to->wo_spell = from->wo_spell;
6747#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006748#ifdef FEAT_SYN_HL
6749 to->wo_cuc = from->wo_cuc;
6750 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006751 to->wo_culopt = copy_option_val(from->wo_culopt);
6752 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754#ifdef FEAT_DIFF
6755 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006756 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006758#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006759 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006760 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006761#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006762#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006763 to->wo_twk = copy_option_val(from->wo_twk);
6764 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766#ifdef FEAT_FOLDING
6767 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006768 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006770 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006771 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 to->wo_fml = from->wo_fml;
6773 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006774 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006775 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006776 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006777 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 to->wo_fdn = from->wo_fdn;
6779# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006780 to->wo_fde = copy_option_val(from->wo_fde);
6781 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006783 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006785#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006786 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006787#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006788
6789#ifdef FEAT_EVAL
6790 // Copy the script context so that we know where the value was last set.
6791 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6792 sizeof(to->wo_script_ctx));
6793#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006794 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795}
6796
6797/*
6798 * Check string options in a window for a NULL value.
6799 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006800 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006801check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802{
6803 check_winopt(&win->w_onebuf_opt);
6804 check_winopt(&win->w_allbuf_opt);
6805}
6806
6807/*
6808 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6809 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006810 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006811check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
6813#ifdef FEAT_FOLDING
6814 check_string_option(&wop->wo_fdi);
6815 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006816 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817# ifdef FEAT_EVAL
6818 check_string_option(&wop->wo_fde);
6819 check_string_option(&wop->wo_fdt);
6820# endif
6821 check_string_option(&wop->wo_fmr);
6822#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006823#ifdef FEAT_SIGNS
6824 check_string_option(&wop->wo_scl);
6825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826#ifdef FEAT_RIGHTLEFT
6827 check_string_option(&wop->wo_rlc);
6828#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006829#ifdef FEAT_LINEBREAK
6830 check_string_option(&wop->wo_sbr);
6831#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006832#ifdef FEAT_STL_OPT
6833 check_string_option(&wop->wo_stl);
6834#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006835#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006836 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006837 check_string_option(&wop->wo_cc);
6838#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006839#ifdef FEAT_CONCEAL
6840 check_string_option(&wop->wo_cocu);
6841#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006842#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006843 check_string_option(&wop->wo_twk);
6844 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006845#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006846#ifdef FEAT_LINEBREAK
6847 check_string_option(&wop->wo_briopt);
6848#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006849 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006850 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006851 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006852 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853}
6854
6855/*
6856 * Free the allocated memory inside a winopt_T.
6857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006859clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860{
6861#ifdef FEAT_FOLDING
6862 clear_string_option(&wop->wo_fdi);
6863 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006864 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865# ifdef FEAT_EVAL
6866 clear_string_option(&wop->wo_fde);
6867 clear_string_option(&wop->wo_fdt);
6868# endif
6869 clear_string_option(&wop->wo_fmr);
6870#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006871#ifdef FEAT_SIGNS
6872 clear_string_option(&wop->wo_scl);
6873#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006874#ifdef FEAT_LINEBREAK
6875 clear_string_option(&wop->wo_briopt);
6876#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006877 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878#ifdef FEAT_RIGHTLEFT
6879 clear_string_option(&wop->wo_rlc);
6880#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006881#ifdef FEAT_LINEBREAK
6882 clear_string_option(&wop->wo_sbr);
6883#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006884#ifdef FEAT_STL_OPT
6885 clear_string_option(&wop->wo_stl);
6886#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006887#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006888 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006889 clear_string_option(&wop->wo_cc);
6890#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006891#ifdef FEAT_CONCEAL
6892 clear_string_option(&wop->wo_cocu);
6893#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006894#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006895 clear_string_option(&wop->wo_twk);
6896 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006897#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006898 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006899 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006900 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901}
6902
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006903#ifdef FEAT_EVAL
6904// Index into the options table for a buffer-local option enum.
6905static int buf_opt_idx[BV_COUNT];
6906# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6907
6908/*
6909 * Initialize buf_opt_idx[] if not done already.
6910 */
6911 static void
6912init_buf_opt_idx(void)
6913{
6914 static int did_init_buf_opt_idx = FALSE;
6915 int i;
6916
6917 if (did_init_buf_opt_idx)
6918 return;
6919 did_init_buf_opt_idx = TRUE;
6920 for (i = 0; !istermoption_idx(i); i++)
6921 if (options[i].indir & PV_BUF)
6922 buf_opt_idx[options[i].indir & PV_MASK] = i;
6923}
6924#else
6925# define COPY_OPT_SCTX(buf, bv)
6926#endif
6927
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928/*
6929 * Copy global option values to local options for one buffer.
6930 * Used when creating a new buffer and sometimes when entering a buffer.
6931 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006932 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6934 * appropriate.
6935 * BCO_NOHELP Don't copy the values to a help buffer.
6936 */
6937 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006938buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939{
6940 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006941 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 int dont_do_help;
6943 int did_isk = FALSE;
6944
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006945 // Skip this when the option defaults have not been set yet. Happens when
6946 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 if (p_cpo != NULL)
6948 {
6949 /*
6950 * Always copy when entering and 'cpo' contains 'S'.
6951 * Don't copy when already initialized.
6952 * Don't copy when 'cpo' contains 's' and not entering.
6953 * 'S' BCO_ENTER initialized 's' should_copy
6954 * yes yes X X TRUE
6955 * yes no yes X FALSE
6956 * no X yes X FALSE
6957 * X no no yes FALSE
6958 * X no no no TRUE
6959 * no yes no X TRUE
6960 */
6961 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6962 && (buf->b_p_initialized
6963 || (!(flags & BCO_ENTER)
6964 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6965 should_copy = FALSE;
6966
6967 if (should_copy || (flags & BCO_ALWAYS))
6968 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006969#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006970 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006971 init_buf_opt_idx();
6972#endif
6973 // Don't copy the options specific to a help buffer when
6974 // BCO_NOHELP is given or the options were initialized already
6975 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6977 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006978 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {
6980 save_p_isk = buf->b_p_isk;
6981 buf->b_p_isk = NULL;
6982 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006983 // Always free the allocated strings. If not already initialized,
6984 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 if (!buf->b_p_initialized)
6986 {
6987 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006988 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006991 switch (*p_ffs)
6992 {
6993 case 'm':
6994 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6995 case 'd':
6996 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6997 case 'u':
6998 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6999 default:
7000 buf->b_p_ff = vim_strsave(p_ff);
7001 }
7002 if (buf->b_p_ff != NULL)
7003 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 buf->b_p_bh = empty_option;
7005 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 }
7007 else
7008 free_buf_options(buf, FALSE);
7009
7010 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007011 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 buf->b_p_ai_nopaste = p_ai_nopaste;
7013 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007014 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007016 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 buf->b_p_tw_nopaste = p_tw_nopaste;
7018 buf->b_p_tw_nobin = p_tw_nobin;
7019 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007020 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 buf->b_p_wm_nopaste = p_wm_nopaste;
7022 buf->b_p_wm_nobin = p_wm_nobin;
7023 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007024 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00007025 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007026 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02007027 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007028 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007030 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007032 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007034 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 buf->b_p_ml_nobin = p_ml_nobin;
7036 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007037 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02007038 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007039 buf->b_p_swf = FALSE;
7040 else
7041 {
7042 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00007043 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007046 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02007047#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02007048 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007049 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007051#ifdef FEAT_COMPL_FUNC
7052 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007053 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007054 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00007055 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007056 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007057 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007058#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007059#ifdef FEAT_EVAL
7060 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007061 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007062 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007065 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007067#ifdef FEAT_VARTABS
7068 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007069 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007070 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007071 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007072 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007073 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007074 buf->b_p_vsts_nopaste = p_vsts_nopaste
7075 ? vim_strsave(p_vsts_nopaste) : NULL;
7076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007078 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007080 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081#ifdef FEAT_FOLDING
7082 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007083 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084#endif
7085 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007086 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007087 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007088 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007089 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7090 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007092 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007094 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007096 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007098 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007099
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007101 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007103 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007105 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007106 buf->b_p_cinsd = vim_strsave(p_cinsd);
7107 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007108 buf->b_p_lop = vim_strsave(p_lop);
7109 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007110
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007111 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007114 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007116 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007118 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007120 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007122 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007123 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007124 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007125#endif
7126#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007127 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007128 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007129 (void)compile_cap_prog(&buf->b_s);
7130 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007131 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007132 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007133 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007134 buf->b_s.b_p_spo = vim_strsave(p_spo);
7135 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007137#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007139 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007141 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007143 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007144#if defined(FEAT_EVAL)
7145 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007146 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148#ifdef FEAT_CRYPT
7149 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007150 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007153 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154#ifdef FEAT_KEYMAP
7155 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007156 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 buf->b_kmap_state |= KEYMAP_INIT;
7158#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007159#ifdef FEAT_TERMINAL
7160 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007161 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007162#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007163 // This isn't really an option, but copying the langmap and IME
7164 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007166 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007168 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007170 // options that are normally global but also have a local value
7171 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007173 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007174 buf->b_p_bkc = empty_option;
7175 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176#ifdef FEAT_QUICKFIX
7177 buf->b_p_gp = empty_option;
7178 buf->b_p_mp = empty_option;
7179 buf->b_p_efm = empty_option;
7180#endif
7181 buf->b_p_ep = empty_option;
7182 buf->b_p_kp = empty_option;
7183 buf->b_p_path = empty_option;
7184 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007185 buf->b_p_tc = empty_option;
7186 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187#ifdef FEAT_FIND_ID
7188 buf->b_p_def = empty_option;
7189 buf->b_p_inc = empty_option;
7190# ifdef FEAT_EVAL
7191 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007192 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193# endif
7194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 buf->b_p_dict = empty_option;
7196 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007197#ifdef FEAT_COMPL_FUNC
7198 buf->b_p_tsrfu = empty_option;
7199#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007200 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007201 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007202#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7203 buf->b_p_bexpr = empty_option;
7204#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007205#if defined(FEAT_CRYPT)
7206 buf->b_p_cm = empty_option;
7207#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007208#ifdef FEAT_PERSISTENT_UNDO
7209 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007210 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007211#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007212 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007213 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007215 // Don't copy the options set by ex_help(), use the saved values,
7216 // when going from a help buffer to a non-help buffer.
7217 // Don't touch these at all when BCO_NOHELP is used and going from
7218 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007222#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007223 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007224 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007225 else
7226 buf->b_p_vts_array = NULL;
7227#endif
7228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 else
7230 {
7231 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007232 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 did_isk = TRUE;
7234 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007235 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007236#ifdef FEAT_VARTABS
7237 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007238 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007239 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007240 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007241 else
7242 buf->b_p_vts_array = NULL;
7243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 if (buf->b_p_bt[0] == 'h')
7246 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007248 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 }
7250 }
7251
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007252 // When the options should be copied (ignoring BCO_ALWAYS), set the
7253 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 if (should_copy)
7255 buf->b_p_initialized = TRUE;
7256 }
7257
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007258 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 if (did_isk)
7260 (void)buf_init_chartab(buf, FALSE);
7261}
7262
7263/*
7264 * Reset the 'modifiable' option and its default value.
7265 */
7266 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007267reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
7269 int opt_idx;
7270
7271 curbuf->b_p_ma = FALSE;
7272 p_ma = FALSE;
7273 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007274 if (opt_idx >= 0)
7275 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276}
7277
7278/*
7279 * Set the global value for 'iminsert' to the local value.
7280 */
7281 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007282set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283{
7284 p_iminsert = curbuf->b_p_iminsert;
7285}
7286
7287/*
7288 * Set the global value for 'imsearch' to the local value.
7289 */
7290 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007291set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292{
7293 p_imsearch = curbuf->b_p_imsearch;
7294}
7295
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296static int expand_option_idx = -1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007297static int expand_option_start_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7299static int expand_option_flags = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007300static int expand_option_append = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301
7302 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007303set_context_in_set_cmd(
7304 expand_T *xp,
7305 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007306 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
7308 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007309 long_u flags = 0; // init for GCC
7310 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 char_u *p;
7312 char_u *s;
7313 int is_term_option = FALSE;
7314 int key;
7315
7316 expand_option_flags = opt_flags;
7317
7318 xp->xp_context = EXPAND_SETTINGS;
7319 if (*arg == NUL)
7320 {
7321 xp->xp_pattern = arg;
7322 return;
7323 }
7324 p = arg + STRLEN(arg) - 1;
7325 if (*p == ' ' && *(p - 1) != '\\')
7326 {
7327 xp->xp_pattern = p + 1;
7328 return;
7329 }
7330 while (p > arg)
7331 {
7332 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007333 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 if (*p == ' ' || *p == ',')
7335 {
7336 while (s > arg && *(s - 1) == '\\')
7337 --s;
7338 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007339 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 if (*p == ' ' && ((p - s) & 1) == 0)
7341 {
7342 ++p;
7343 break;
7344 }
7345 --p;
7346 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007347 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 {
7349 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007350 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 p += 2;
7352 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007353 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 {
7355 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007356 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 p += 3;
7358 }
7359 xp->xp_pattern = arg = p;
7360 if (*arg == '<')
7361 {
7362 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007363 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 return;
7365 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007366 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 {
7368 xp->xp_context = EXPAND_NOTHING;
7369 return;
7370 }
7371 nextchar = *++p;
7372 is_term_option = TRUE;
7373 expand_option_name[2] = KEY2TERMCAP0(key);
7374 expand_option_name[3] = KEY2TERMCAP1(key);
7375 }
7376 else
7377 {
7378 if (p[0] == 't' && p[1] == '_')
7379 {
7380 p += 2;
7381 if (*p != NUL)
7382 ++p;
7383 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007384 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 nextchar = *++p;
7386 is_term_option = TRUE;
7387 expand_option_name[2] = p[-2];
7388 expand_option_name[3] = p[-1];
7389 }
7390 else
7391 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007392 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7394 p++;
7395 if (*p == NUL)
7396 return;
7397 nextchar = *p;
7398 *p = NUL;
7399 opt_idx = findoption(arg);
7400 *p = nextchar;
7401 if (opt_idx == -1 || options[opt_idx].var == NULL)
7402 {
7403 xp->xp_context = EXPAND_NOTHING;
7404 return;
7405 }
7406 flags = options[opt_idx].flags;
7407 if (flags & P_BOOL)
7408 {
7409 xp->xp_context = EXPAND_NOTHING;
7410 return;
7411 }
7412 }
7413 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007414 // handle "-=" and "+="
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007415 expand_option_append = FALSE;
7416 int expand_option_subtract = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7418 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007419 if (nextchar == '-')
7420 expand_option_subtract = TRUE;
7421 if (nextchar == '+' || nextchar == '^')
7422 expand_option_append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 ++p;
7424 nextchar = '=';
7425 }
7426 if ((nextchar != '=' && nextchar != ':')
7427 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7428 {
7429 xp->xp_context = EXPAND_UNSUCCESSFUL;
7430 return;
7431 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007432
7433 // Below are for handling expanding a specific option's value after the '='
7434 // or ':'
7435
7436 if (is_term_option)
7437 expand_option_idx = -1;
7438 else
7439 expand_option_idx = opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02007441 if (!is_term_option)
7442 {
7443 if (options[opt_idx].flags & P_NO_CMD_EXPAND)
7444 {
7445 xp->xp_context=EXPAND_UNSUCCESSFUL;
7446 return;
7447 }
7448 }
7449
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 xp->xp_pattern = p + 1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007451 expand_option_start_col = (int)(p + 1 - xp->xp_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007453 // Certain options currently have special case handling to reuse the
7454 // expansion logic with other commands.
Doug Kearns6dfdff32023-08-27 18:48:51 +02007455#ifdef FEAT_SYN_HL
7456 if (options[opt_idx].var == (char_u *)&p_syn)
7457 {
7458 xp->xp_context = EXPAND_OWNSYNTAX;
7459 return;
7460 }
7461#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007462 if (options[opt_idx].var == (char_u *)&p_ft)
7463 {
7464 xp->xp_context = EXPAND_FILETYPE;
7465 return;
7466 }
Doug Kearns81642d92024-01-04 22:37:44 +01007467#ifdef FEAT_KEYMAP
7468 if (options[opt_idx].var == (char_u *)&p_keymap)
7469 {
7470 xp->xp_context = EXPAND_KEYMAP;
7471 return;
7472 }
7473#endif
Doug Kearns6dfdff32023-08-27 18:48:51 +02007474
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007475 // Now pick. If the option has a custom expander, use that. Otherwise, just
7476 // fill with the existing option value.
7477 if (expand_option_subtract)
7478 {
7479 xp->xp_context = EXPAND_SETTING_SUBTRACT;
7480 return;
7481 }
7482 else if (expand_option_idx >= 0 &&
7483 options[expand_option_idx].opt_expand_cb != NULL)
7484 {
7485 xp->xp_context = EXPAND_STRING_SETTING;
7486 }
7487 else if (*xp->xp_pattern == NUL)
7488 {
7489 xp->xp_context = EXPAND_OLD_SETTING;
7490 return;
7491 }
7492 else
7493 xp->xp_context = EXPAND_NOTHING;
7494
7495 if (is_term_option || (flags & P_NUM))
7496 return;
7497
7498 // Only string options below
7499
7500 // Options that have P_EXPAND are considered to all use file/dir expansion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 if (flags & P_EXPAND)
7502 {
7503 p = options[opt_idx].var;
7504 if (p == (char_u *)&p_bdir
7505 || p == (char_u *)&p_dir
7506 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007507 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510#ifdef FEAT_SESSION
7511 || p == (char_u *)&p_vdir
7512#endif
7513 )
7514 {
7515 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007516 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 xp->xp_backslash = XP_BS_THREE;
7518 else
7519 xp->xp_backslash = XP_BS_ONE;
7520 }
7521 else
7522 {
7523 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007524 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 if (p == (char_u *)&p_tags)
7526 xp->xp_backslash = XP_BS_THREE;
7527 else
7528 xp->xp_backslash = XP_BS_ONE;
7529 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02007530 if (flags & P_COMMA)
7531 xp->xp_backslash |= XP_BS_COMMA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 }
7533
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007534 // For an option that is a list of file names, or comma/colon-separated
7535 // values, split it by the delimiter and find the start of the current
7536 // pattern, while accounting for backslash-escaped space/commas/colons.
7537 // Triple-backslashed escaped file names (e.g. 'path') can also be
7538 // delimited by space.
7539 if ((flags & P_EXPAND) || (flags & P_COMMA) || (flags & P_COLON))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007541 for (p = arg + STRLEN(arg) - 1; p >= xp->xp_pattern; --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007543 // count number of backslashes before ' ' or ',' or ':'
7544 if (*p == ' ' || *p == ',' ||
7545 (*p == ':' && (flags & P_COLON)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007547 s = p;
7548 while (s > xp->xp_pattern && *(s - 1) == '\\')
7549 --s;
Yee Cheng Chin54844852023-10-09 18:12:31 +02007550 if ((*p == ' ' && ((xp->xp_backslash & XP_BS_THREE) && (p - s) < 3))
7551#if defined(BACKSLASH_IN_FILENAME)
7552 || (*p == ',' && (flags & P_COMMA) && (p - s) < 1)
7553#else
7554 || (*p == ',' && (flags & P_COMMA) && (p - s) < 2)
7555#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007556 || (*p == ':' && (flags & P_COLON)))
7557 {
7558 xp->xp_pattern = p + 1;
7559 break;
7560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 }
7562 }
7563 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007564
7565 // An option that is a list of single-character flags should always start
7566 // at the end as we don't complete words.
7567 if (flags & P_FLAGLIST)
7568 xp->xp_pattern = arg + STRLEN(arg);
7569
7570 // Some options can either be using file/dir expansions, or custom value
7571 // expansion depending on what the user typed. Unfortunately we have to
7572 // manually handle it here to make sure we have the correct xp_context set.
7573#ifdef FEAT_SPELL
7574 if (options[opt_idx].var == (char_u *)&p_sps)
7575 {
7576 if (STRNCMP(xp->xp_pattern, "file:", 5) == 0)
7577 {
7578 xp->xp_pattern += 5;
7579 return;
7580 }
7581 else if (options[expand_option_idx].opt_expand_cb != NULL)
7582 {
7583 xp->xp_context = EXPAND_STRING_SETTING;
7584 }
7585 }
7586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587}
7588
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007589/*
7590 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7591 *
7592 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7593 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7594 *
7595 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7596 * regular expression 'regmatch', then stores the match in matches[idx] and
7597 * returns TRUE.
7598 *
7599 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7600 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7601 *
7602 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7603 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7604 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02007605 static int
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007606match_str(
7607 char_u *str,
7608 regmatch_T *regmatch,
7609 char_u **matches,
7610 int idx,
7611 int test_only,
7612 int fuzzy,
7613 char_u *fuzzystr,
7614 fuzmatch_str_T *fuzmatch)
7615{
7616 if (!fuzzy)
7617 {
7618 if (vim_regexec(regmatch, str, (colnr_T)0))
7619 {
7620 if (!test_only)
7621 matches[idx] = vim_strsave(str);
7622 return TRUE;
7623 }
7624 }
7625 else
7626 {
7627 int score;
7628
7629 score = fuzzy_match_str(str, fuzzystr);
7630 if (score != 0)
7631 {
7632 if (!test_only)
7633 {
7634 fuzmatch[idx].idx = idx;
7635 fuzmatch[idx].str = vim_strsave(str);
7636 fuzmatch[idx].score = score;
7637 }
7638
7639 return TRUE;
7640 }
7641 }
7642
7643 return FALSE;
7644}
7645
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007647ExpandSettings(
7648 expand_T *xp,
7649 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007650 char_u *fuzzystr,
7651 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007652 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007653 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007655 int num_normal = 0; // Nr of matching non-term-code settings
7656 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 int opt_idx;
7658 int match;
7659 int count = 0;
7660 char_u *str;
7661 int loop;
7662 int is_term_opt;
7663 char_u name_buf[MAX_KEY_NAME_LEN];
7664 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007665 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007666 int fuzzy;
7667 fuzmatch_str_T *fuzmatch = NULL;
7668
Christian Brabandtcb747892022-05-08 21:10:56 +01007669 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007671 // do this loop twice:
7672 // loop == 0: count the number of matching options
7673 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 for (loop = 0; loop <= 1; ++loop)
7675 {
7676 regmatch->rm_ic = ic;
7677 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7678 {
K.Takataeeec2542021-06-02 13:28:16 +02007679 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007680 {
7681 if (match_str((char_u *)names[match], regmatch, *matches,
7682 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 {
7684 if (loop == 0)
7685 num_normal++;
7686 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007687 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 }
7691 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7692 opt_idx++)
7693 {
7694 if (options[opt_idx].var == NULL)
7695 continue;
7696 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007697 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007699 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 if (is_term_opt && num_normal > 0)
7701 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007702
7703 if (match_str(str, regmatch, *matches, count, (loop == 0),
7704 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {
7706 if (loop == 0)
7707 {
7708 if (is_term_opt)
7709 num_term++;
7710 else
7711 num_normal++;
7712 }
7713 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007714 count++;
7715 }
7716 else if (!fuzzy && options[opt_idx].shortname != NULL
7717 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007718 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007719 {
7720 // Compare against the abbreviated option name (for regular
7721 // expression match). Fuzzy matching (previous if) already
7722 // matches against both the expanded and abbreviated names.
7723 if (loop == 0)
7724 {
7725 if (is_term_opt)
7726 num_term++;
7727 else
7728 num_normal++;
7729 }
7730 else
7731 (*matches)[count++] = vim_strsave(str);
7732 }
7733 else if (is_term_opt)
7734 {
7735 name_buf[0] = '<';
7736 name_buf[1] = 't';
7737 name_buf[2] = '_';
7738 name_buf[3] = str[2];
7739 name_buf[4] = str[3];
7740 name_buf[5] = '>';
7741 name_buf[6] = NUL;
7742
7743 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7744 fuzzy, fuzzystr, fuzmatch))
7745 {
7746 if (loop == 0)
7747 num_term++;
7748 else
7749 count++;
7750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 }
7752 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007753
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007754 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7756 {
7757 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7758 {
Keith Thompson184f71c2024-01-04 21:19:04 +01007759 if (!SAFE_isprint(str[0]) || !SAFE_isprint(str[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 continue;
7761
7762 name_buf[0] = 't';
7763 name_buf[1] = '_';
7764 name_buf[2] = str[0];
7765 name_buf[3] = str[1];
7766 name_buf[4] = NUL;
7767
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007768 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007769 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007770 {
7771 if (loop == 0)
7772 num_term++;
7773 else
7774 count++;
7775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 else
7777 {
7778 name_buf[0] = '<';
7779 name_buf[1] = 't';
7780 name_buf[2] = '_';
7781 name_buf[3] = str[0];
7782 name_buf[4] = str[1];
7783 name_buf[5] = '>';
7784 name_buf[6] = NUL;
7785
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007786 if (match_str(name_buf, regmatch, *matches, count,
7787 (loop == 0), fuzzy, fuzzystr,
7788 fuzmatch))
7789 {
7790 if (loop == 0)
7791 num_term++;
7792 else
7793 count++;
7794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 }
7796 }
7797
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007798 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007799 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7801 {
7802 name_buf[0] = '<';
7803 STRCPY(name_buf + 1, str);
7804 STRCAT(name_buf, ">");
7805
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007806 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7807 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 {
7809 if (loop == 0)
7810 num_term++;
7811 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007812 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 }
7814 }
7815 }
7816 if (loop == 0)
7817 {
7818 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007819 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007821 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 else
7823 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007824 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007826 *matches = ALLOC_MULT(char_u *, *numMatches);
7827 if (*matches == NULL)
7828 {
7829 *matches = (char_u **)"";
7830 return FAIL;
7831 }
7832 }
7833 else
7834 {
7835 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7836 if (fuzmatch == NULL)
7837 {
7838 *matches = (char_u **)"";
7839 return FAIL;
7840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 }
7842 }
7843 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007844
7845 if (fuzzy &&
7846 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7847 return FAIL;
7848
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 return OK;
7850}
7851
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007852// Escape an option value that can be used on the command-line with :set.
7853// Caller needs to free the returned string, unless NULL is returned.
7854 static char_u*
7855escape_option_str_cmdline(char_u *var)
7856{
7857 char_u *buf;
7858
7859 // A backslash is required before some characters. This is the reverse of
7860 // what happens in do_set().
7861 buf = vim_strsave_escaped(var, escape_chars);
7862 if (buf == NULL)
7863 return NULL;
7864
7865#ifdef BACKSLASH_IN_FILENAME
7866 // For MS-Windows et al. we don't double backslashes at the start and
7867 // before a file name character.
7868 // The reverse is found at stropt_copy_value().
7869 for (var = buf; *var != NUL; MB_PTR_ADV(var))
7870 if (var[0] == '\\' && var[1] == '\\'
7871 && expand_option_idx >= 0
7872 && (options[expand_option_idx].flags & P_EXPAND)
7873 && vim_isfilec(var[2])
7874 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
7875 STRMOVE(var, var + 1);
7876#endif
7877 return buf;
7878}
7879
7880/*
7881 * Expansion handler for :set= when we just want to fill in with the existing value.
7882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007884ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007886 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 char_u *buf;
7888
zeertzjqb0d45ec2023-01-25 15:04:22 +00007889 *numMatches = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007890 *matches = ALLOC_MULT(char_u *, 1);
zeertzjqb0d45ec2023-01-25 15:04:22 +00007891 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 return FAIL;
7893
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007894 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 if (expand_option_idx < 0)
7896 {
7897 var = find_termcode(expand_option_name + 2);
7898 if (var == NULL)
7899 expand_option_idx = findoption(expand_option_name);
7900 }
7901
7902 if (expand_option_idx >= 0)
7903 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007904 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 option_value2string(&options[expand_option_idx], expand_option_flags);
7906 var = NameBuff;
7907 }
7908 else if (var == NULL)
7909 var = (char_u *)"";
7910
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007911 buf = escape_option_str_cmdline(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 if (buf == NULL)
7913 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007914 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 return FAIL;
7916 }
7917
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007918 (*matches)[0] = buf;
zeertzjqb0d45ec2023-01-25 15:04:22 +00007919 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 return OK;
7921}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922
7923/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007924 * Expansion handler for :set=/:set+= when the option has a custom expansion handler.
7925 */
7926 int
7927ExpandStringSetting(
7928 expand_T *xp,
7929 regmatch_T *regmatch,
7930 int *numMatches,
7931 char_u ***matches)
7932{
7933 char_u *var = NULL; // init for GCC
7934 char_u *buf;
7935
7936 if (expand_option_idx < 0 ||
7937 options[expand_option_idx].opt_expand_cb == NULL)
7938 {
7939 // Not supposed to reach this. This function is only for options with
7940 // custom expansion callbacks.
7941 return FAIL;
7942 }
7943
7944 optexpand_T args;
7945 args.oe_varp = get_varp_scope(&options[expand_option_idx], expand_option_flags);
7946 args.oe_append = expand_option_append;
7947 args.oe_regmatch = regmatch;
7948 args.oe_xp = xp;
7949 args.oe_set_arg = xp->xp_line + expand_option_start_col;
7950 args.oe_include_orig_val =
7951 !expand_option_append &&
7952 (*args.oe_set_arg == NUL);
7953
7954 // Retrieve the existing value, but escape it as a reverse of setting it.
7955 // We technically only need to do this when oe_append or
7956 // oe_include_orig_val is true.
7957 option_value2string(&options[expand_option_idx], expand_option_flags);
7958 var = NameBuff;
7959 buf = escape_option_str_cmdline(var);
7960 if (buf == NULL)
7961 return FAIL;
7962
7963 args.oe_opt_value = buf;
7964
7965 int num_ret = options[expand_option_idx].opt_expand_cb(&args, numMatches, matches);
7966
7967 vim_free(buf);
7968 return num_ret;
7969}
7970
7971/*
7972 * Expansion handler for :set-=
7973 */
7974 int
7975ExpandSettingSubtract(
7976 expand_T *xp,
7977 regmatch_T *regmatch,
7978 int *numMatches,
7979 char_u ***matches)
7980{
7981 if (expand_option_idx < 0)
7982 // term option
7983 return ExpandOldSetting(numMatches, matches);
7984
7985 char_u *option_val = *(char_u**)get_option_varp_scope(
7986 expand_option_idx, expand_option_flags);
7987
7988 long_u option_flags = options[expand_option_idx].flags;
7989
7990 if (option_flags & P_NUM)
7991 return ExpandOldSetting(numMatches, matches);
7992 else if (option_flags & P_COMMA)
7993 {
7994 // Split the option by comma, then present each option to the user if
7995 // it matches the pattern.
7996 // This condition needs to go first, because 'whichwrap' has both
7997 // P_COMMA and P_FLAGLIST.
7998 garray_T ga;
7999
8000 char_u *item;
8001 char_u *option_copy;
8002 char_u *next_val;
8003 char_u *comma;
8004
8005 if (*option_val == NUL)
8006 return FAIL;
8007
8008 // Make a copy as we need to inject null characters destructively.
8009 option_copy = vim_strsave(option_val);
8010 if (option_copy == NULL)
8011 return FAIL;
8012 next_val = option_copy;
8013
8014 ga_init2(&ga, sizeof(char_u *), 10);
8015
8016 do
8017 {
8018 item = next_val;
8019 comma = vim_strchr(next_val, ',');
8020 while (comma != NULL && comma != next_val && *(comma - 1) == '\\')
8021 {
8022 // "\," is interpreted as a literal comma rather than option
8023 // separator when reading options in copy_option_part(). Skip
8024 // it.
8025 comma = vim_strchr(comma + 1, ',');
8026 }
8027 if (comma != NULL)
8028 {
8029 *comma = NUL; // null-terminate this value, required by later functions
8030 next_val = comma + 1;
8031 }
8032 else
8033 next_val = NULL;
8034
8035 if (*item == NUL)
8036 // empty value, don't add to list
8037 continue;
8038
8039 if (!vim_regexec(regmatch, item, (colnr_T)0))
8040 continue;
8041
8042 char_u *buf = escape_option_str_cmdline(item);
8043 if (buf == NULL)
8044 {
8045 vim_free(option_copy);
8046 ga_clear_strings(&ga);
8047 return FAIL;
8048 }
8049 if (ga_add_string(&ga, buf) != OK)
8050 {
8051 vim_free(buf);
8052 break;
8053 }
8054 } while (next_val != NULL);
8055
8056 vim_free(option_copy);
8057
8058 *matches = ga.ga_data;
8059 *numMatches = ga.ga_len;
8060 return OK;
8061 }
8062 else if (option_flags & P_FLAGLIST)
8063 {
8064 // Only present the flags that are set on the option as the other flags
8065 // are not meaningful to do set-= on.
8066
8067 if (*xp->xp_pattern != NUL)
8068 {
8069 // Don't suggest anything if cmdline is non-empty. Vim's set-=
8070 // behavior requires consecutive strings and it's usually
Viktor Szépedbf749b2023-10-16 09:53:37 +02008071 // unintuitive to users if they try to subtract multiple flags at
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008072 // once.
8073 return FAIL;
8074 }
8075
Yee Cheng Chin6d113472023-10-02 21:38:39 +02008076 size_t num_flags = STRLEN(option_val);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008077 if (num_flags == 0)
8078 return FAIL;
8079
8080 *matches = ALLOC_MULT(char_u *, num_flags + 1);
8081 if (*matches == NULL)
8082 return FAIL;
8083
8084 int count = 0;
8085 char_u *p;
8086
8087 p = vim_strsave(option_val);
8088 if (p == NULL)
8089 {
8090 VIM_CLEAR(*matches);
8091 return FAIL;
8092 }
8093 (*matches)[count++] = p;
8094
8095 if (num_flags > 1)
8096 {
8097 // If more than one flags, split the flags up and expose each
8098 // character as individual choice.
8099 for (char_u *flag = option_val; *flag != NUL; flag++)
8100 {
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02008101 p = vim_strnsave(flag, 1);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008102 if (p == NULL)
8103 break;
8104 (*matches)[count++] = p;
8105 }
8106 }
8107
8108 *numMatches = count;
8109 return OK;
8110 }
8111
8112 return ExpandOldSetting(numMatches, matches);
8113}
8114
8115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 * Get the value for the numeric or string option *opp in a nice format into
8117 * NameBuff[]. Must not be called with a hidden option!
8118 */
8119 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008120option_value2string(
8121 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008122 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123{
8124 char_u *varp;
8125
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008126 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127
8128 if (opp->flags & P_NUM)
8129 {
8130 long wc = 0;
8131
8132 if (wc_use_keyname(varp, &wc))
8133 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
8134 else if (wc != 0)
8135 STRCPY(NameBuff, transchar((int)wc));
8136 else
8137 sprintf((char *)NameBuff, "%ld", *(long *)varp);
8138 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008139 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {
8141 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008142 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 NameBuff[0] = NUL;
8144#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008145 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008146 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 STRCPY(NameBuff, "*****");
8148#endif
8149 else if (opp->flags & P_EXPAND)
8150 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008151 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 else if ((char_u **)opp->var == &p_pt)
8153 str2specialbuf(p_pt, NameBuff, MAXPATHL);
8154 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008155 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 }
8157}
8158
8159/*
8160 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
8161 * printed as a keyname.
8162 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
8163 */
8164 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008165wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166{
8167 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
8168 {
8169 *wcp = *(long *)varp;
8170 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
8171 return TRUE;
8172 }
8173 return FALSE;
8174}
8175
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 * Return TRUE if "x" is present in 'shortmess' option, or
8178 * 'shortmess' contains 'a' and "x" is present in SHM_A.
8179 */
8180 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008181shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01008183 return p_shm != NULL &&
8184 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 || (vim_strchr(p_shm, 'a') != NULL
8186 && vim_strchr((char_u *)SHM_A, x) != NULL));
8187}
8188
8189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
8191 *
8192 * Reset 'compatible' and set the values for options that didn't get set yet
8193 * to the Vim defaults.
8194 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008195 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 */
8197 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008198vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008200 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00008201 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008202 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203
8204 if (!option_was_set((char_u *)"cp"))
8205 {
8206 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02008207 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
8209 set_option_default(opt_idx, OPT_FREE, FALSE);
8210 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008211 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008213
8214 if (fname != NULL)
8215 {
8216 p = vim_getenv(envname, &dofree);
8217 if (p == NULL)
8218 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008219 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008220 p = FullName_save(fname, FALSE);
8221 if (p != NULL)
8222 {
8223 vim_setenv(envname, p);
8224 vim_free(p);
8225 }
8226 }
8227 else if (dofree)
8228 vim_free(p);
8229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230}
8231
8232/*
8233 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
8234 */
8235 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008236change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008238 int opt_idx;
8239
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 if (p_cp != on)
8241 {
8242 p_cp = on;
8243 compatible_set();
8244 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008245 opt_idx = findoption((char_u *)"cp");
8246 if (opt_idx >= 0)
8247 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248}
8249
8250/*
8251 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008252 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 */
8254 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008255option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256{
8257 int idx;
8258
8259 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008260 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 return FALSE;
8262 if (options[idx].flags & P_WAS_SET)
8263 return TRUE;
8264 return FALSE;
8265}
8266
8267/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008268 * Reset the flag indicating option "name" was set.
8269 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008270 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008271reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008272{
8273 int idx = findoption(name);
8274
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008275 if (idx < 0)
8276 return FAIL;
8277
8278 options[idx].flags &= ~P_WAS_SET;
8279 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008280}
8281
8282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 * compatible_set() - Called when 'compatible' has been set or unset.
8284 *
8285 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8286 * flag) to a Vi compatible value.
8287 * When 'compatible' is unset: Set all options that have a different default
8288 * for Vim (without the P_VI_DEF flag) to that default.
8289 */
8290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008291compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292{
8293 int opt_idx;
8294
Bram Moolenaardac13472019-09-16 21:06:21 +02008295 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8297 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8298 set_option_default(opt_idx, OPT_FREE, p_cp);
8299 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008300 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301}
8302
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 * Check if backspacing over something is allowed.
8305 */
8306 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008307can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008308 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008310#ifdef FEAT_JOB_CHANNEL
8311 if (what == BS_START && bt_prompt(curbuf))
8312 return FALSE;
8313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 switch (*p_bs)
8315 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008316 case '3': return TRUE;
8317 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 case '1': return (what != BS_START);
8319 case '0': return FALSE;
8320 }
8321 return vim_strchr(p_bs, what) != NULL;
8322}
8323
8324/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008325 * Return the effective 'scrolloff' value for the current window, using the
8326 * global value when appropriate.
8327 */
8328 long
8329get_scrolloff_value(void)
8330{
8331 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8332}
8333
8334/*
8335 * Return the effective 'sidescrolloff' value for the current window, using the
8336 * global value when appropriate.
8337 */
8338 long
8339get_sidescrolloff_value(void)
8340{
8341 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8342}
8343
8344/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008345 * Get the local or global value of 'backupcopy'.
8346 */
8347 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008348get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008349{
8350 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8351}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008352
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008353#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008354/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008355 * Get the local or global value of 'formatlistpat'.
8356 */
8357 char_u *
8358get_flp_value(buf_T *buf)
8359{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008360 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8361 return p_flp;
8362 return buf->b_p_flp;
8363}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008364#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008365
8366/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008367 * Get the local or global value of the 'virtualedit' flags.
8368 */
8369 unsigned int
8370get_ve_flags(void)
8371{
Gary Johnson51ad8502021-08-03 18:33:08 +02008372 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8373 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008374}
8375
Bram Moolenaaree857022019-11-09 23:26:40 +01008376#if defined(FEAT_LINEBREAK) || defined(PROTO)
8377/*
8378 * Get the local or global value of 'showbreak'.
8379 */
8380 char_u *
8381get_showbreak_value(win_T *win)
8382{
8383 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8384 return p_sbr;
8385 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8386 return empty_option;
8387 return win->w_p_sbr;
8388}
8389#endif
8390
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008391#if defined(FEAT_EVAL) || defined(PROTO)
8392/*
8393 * Get window or buffer local options.
8394 */
8395 dict_T *
8396get_winbuf_options(int bufopt)
8397{
8398 dict_T *d;
8399 int opt_idx;
8400
8401 d = dict_alloc();
8402 if (d == NULL)
8403 return NULL;
8404
Bram Moolenaardac13472019-09-16 21:06:21 +02008405 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008406 {
8407 struct vimoption *opt = &options[opt_idx];
8408
8409 if ((bufopt && (opt->indir & PV_BUF))
8410 || (!bufopt && (opt->indir & PV_WIN)))
8411 {
8412 char_u *varp = get_varp(opt);
8413
8414 if (varp != NULL)
8415 {
8416 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008417 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008418 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008419 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008420 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008421 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008422 }
8423 }
8424 }
8425
8426 return d;
8427}
8428#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008429
Bram Moolenaardac13472019-09-16 21:06:21 +02008430#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008431/*
8432 * This is called when 'culopt' is changed
8433 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008434 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008435fill_culopt_flags(char_u *val, win_T *wp)
8436{
8437 char_u *p;
8438 char_u culopt_flags_new = 0;
8439
8440 if (val == NULL)
8441 p = wp->w_p_culopt;
8442 else
8443 p = val;
8444 while (*p != NUL)
8445 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008446 // Note: Keep this in sync with p_culopt_values.
Bram Moolenaar017ba072019-09-14 21:01:23 +02008447 if (STRNCMP(p, "line", 4) == 0)
8448 {
8449 p += 4;
8450 culopt_flags_new |= CULOPT_LINE;
8451 }
8452 else if (STRNCMP(p, "both", 4) == 0)
8453 {
8454 p += 4;
8455 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8456 }
8457 else if (STRNCMP(p, "number", 6) == 0)
8458 {
8459 p += 6;
8460 culopt_flags_new |= CULOPT_NBR;
8461 }
8462 else if (STRNCMP(p, "screenline", 10) == 0)
8463 {
8464 p += 10;
8465 culopt_flags_new |= CULOPT_SCRLINE;
8466 }
8467
8468 if (*p != ',' && *p != NUL)
8469 return FAIL;
8470 if (*p == ',')
8471 ++p;
8472 }
8473
8474 // Can't have both "line" and "screenline".
8475 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8476 return FAIL;
8477 wp->w_p_culopt_flags = culopt_flags_new;
8478
8479 return OK;
8480}
8481#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008482
8483/*
8484 * Get the value of 'magic' adjusted for Vim9 script.
8485 */
8486 int
8487magic_isset(void)
8488{
8489 switch (magic_overruled)
8490 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008491 case OPTION_MAGIC_ON: return TRUE;
8492 case OPTION_MAGIC_OFF: return FALSE;
8493 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008494 }
8495#ifdef FEAT_EVAL
8496 if (in_vim9script())
8497 return TRUE;
8498#endif
8499 return p_magic;
8500}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008501
8502/*
8503 * Set the callback function value for an option that accepts a function name,
8504 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8505 * Returns OK if the option is successfully set to a function, otherwise
8506 * returns FAIL.
8507 */
8508 int
8509option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8510{
8511#ifdef FEAT_EVAL
8512 typval_T *tv;
8513 callback_T cb;
8514
8515 if (optval == NULL || *optval == NUL)
8516 {
8517 free_callback(optcb);
8518 return OK;
8519 }
8520
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008521 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008522 || (STRNCMP(optval, "function(", 9) == 0)
8523 || (STRNCMP(optval, "funcref(", 8) == 0))
8524 // Lambda expression or a funcref
8525 tv = eval_expr(optval, NULL);
8526 else
8527 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008528 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008529 if (tv == NULL)
8530 return FAIL;
8531
8532 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008533 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008534 {
8535 free_tv(tv);
8536 return FAIL;
8537 }
8538
8539 free_callback(optcb);
8540 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008541 if (cb.cb_free_name)
8542 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008543 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008544
8545 // when using Vim9 style "import.funcname" it needs to be expanded to
8546 // "import#funcname".
8547 expand_autload_callback(optcb);
8548
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008549 return OK;
8550#else
8551 return FAIL;
8552#endif
8553}
Christian Brabandt757593c2023-08-22 21:44:10 +02008554
8555#if defined(FEAT_EVAL) || defined(PROTO)
8556 static void
8557didset_options_sctx(int opt_flags, char **buf)
8558{
8559 for (int i = 0; ; ++i)
8560 {
8561 if (buf[i] == NULL)
8562 break;
8563
8564 int idx = findoption((char_u *)buf[i]);
8565 if (idx >= 0)
8566 set_option_sctx_idx(idx, opt_flags, current_sctx);
8567 }
8568}
8569#endif